diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index b391dcd5397..040059276d3 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -477,11 +477,12 @@ class _ScriptRun: try: handler = f"_async_{action}_step" await getattr(self, handler)() - trace_element.update_variables(self._variables) except Exception as ex: # pylint: disable=broad-except self._handle_exception( ex, continue_on_error, self._log_exceptions or log_exceptions ) + finally: + trace_element.update_variables(self._variables) def _finish(self) -> None: self._script._runs.remove(self) # pylint: disable=protected-access diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 1a506b88d39..57c1b2dd473 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -1337,14 +1337,13 @@ async def test_wait_continue_on_timeout( result_wait = {"wait": {"trigger": None, "remaining": 0.0}} variable_wait = dict(result_wait) expected_trace = { - "0": [{"result": result_wait}], + "0": [{"result": result_wait, "variables": variable_wait}], } if continue_on_timeout is False: expected_trace["0"][0]["result"]["timeout"] = True expected_trace["0"][0]["error"] = "" expected_script_execution = "aborted" else: - expected_trace["0"][0]["variables"] = variable_wait expected_trace["1"] = [{"result": {"event": "test_event", "event_data": {}}}] expected_script_execution = "finished" assert_action_trace(expected_trace, expected_script_execution)