diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 629b3ec351291b7980bcb9727fd2bfbd9ae98796..7558c70acbbe538c9e80aa1dbbae39783ba88159 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -2575,6 +2575,14 @@ DynamicModel::writeDriverOutput(ostream &output, const string &basename, bool bl
       for (size_t i = 0; i < temporary_terms_derivatives.size(); i++)
         output << temporary_terms_derivatives[i].size() + (i == 0 ? temporary_terms_mlv.size() : 0) << "; ";
       output << "];" << endl;
+
+      /* Write mapping between model local variables and indices in the temporary
+         terms vector (dynare#1722) */
+      output << modstruct << "model_local_variables_dynamic_tt_idxs = {" << endl;
+      for (auto [mlv, value] : temporary_terms_mlv)
+        output << "  '" << symbol_table.getName(mlv->symb_id) << "', "
+               << temporary_terms_idxs.at(mlv)+1 << ';' << endl;
+      output << "};" << endl;
     }
 
   // Write equation tags
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 5fbd7fd889e3dea593344c16d0c15fe52ff9fd07..a958bf081bbbd00c7b0dbd0f2cd2491788a32336 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -70,7 +70,7 @@ ModelTree::copyHelper(const ModelTree &m)
 
   // Temporary terms
   for (const auto &it : m.temporary_terms_mlv)
-    temporary_terms_mlv[f(it.first)] = f(it.second);
+    temporary_terms_mlv[dynamic_cast<VariableNode *>(f(it.first))] = f(it.second);
   for (const auto &it : m.temporary_terms_derivatives)
     temporary_terms_derivatives.push_back(convert_temporary_terms_t(it));
   for (const auto &it : m.temporary_terms_idxs)
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index a5cc30c8cfdd98dd47b4ad54f03b784618acad71..1b3eb22de6bf8e25f9025fb5c392cbc7fa44c5a9 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -123,7 +123,7 @@ protected:
 
   //! Used model local variables, that will be treated as temporary terms
   /*! See the comments in ModelTree::computeTemporaryTerms() */
-  map<expr_t, expr_t, ExprNodeLess> temporary_terms_mlv;
+  map<VariableNode *, expr_t, ExprNodeLess> temporary_terms_mlv;
 
   //! Temporary terms for residuals and derivatives
   /*! Index 0 is temp. terms of residuals, index 1 for first derivatives, ... */
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index ad5fd35dc41d3d52b67eedcd039d0f6a5e30de83..1c347c3bb3631796652c18ec229dfde4236e09c3 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -1728,6 +1728,14 @@ StaticModel::writeDriverOutput(ostream &output, bool block) const
     output << temporary_terms_derivative.size() << "; ";
   output << "];" << endl;
 
+  /* Write mapping between model local variables and indices in the temporary
+     terms vector (dynare#1722) */
+  output << "M_.model_local_variables_static_tt_idxs = {" << endl;
+  for (auto [mlv, value] : temporary_terms_mlv)
+    output << "  '" << symbol_table.getName(mlv->symb_id) << "', "
+           << temporary_terms_idxs.at(mlv)+1 << ';' << endl;
+  output << "};" << endl;
+
   if (!block)
     return;