diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 3bedee10c32babec6dc77eb76390ceb403244e24..75f272f8c5c5c2088cb24cc33018560c862f50a9 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -3177,6 +3177,14 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
       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 a4cef4dcaebf8b8db25ad9e6ee8e29f6a35562d8..f9c757a1880de449fdef18b8759a5417fb58ff1b 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -74,7 +74,7 @@ ModelTree::copyHelper(const ModelTree &m)
   for (const auto &it : m.temporary_terms)
     temporary_terms.insert(f(it));
   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 b3502c365636c123c4ad37ea6f5384a73ca0ecb1..2b0c519158c78680d519a4cb483d50fa12546705 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -136,7 +136,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 7609e1560435a9b4ea7cd268882dfc20b0a939e9..e4ac721283dfb4aa11c30ac095c5e7dc73b5f44c 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -2107,6 +2107,14 @@ StaticModel::writeOutput(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;