I have a small python script running on the broker side to force an mqtt update every time sensors get refreshed from the alarm system, even is the sensors have not changed value.
The sensor get updated every 10 seconds and the script is triggered by this automation:
#pass entity_id as argument from call
sensor = data.get('entity_id')
#read old state
oldstate = hass.states.get(sensor)
#write old state to entity and force update to record database
hass.states.set(sensor, oldstate.state , oldstate.attributes, force_update=True)
the scripts stops working after a few hours and I see this error message,
RecursionError: maximum recursion depth exceeded in comparison
Seems that a few have had this problem, but I cannot find a cure…
No I only read from sensor.alarm_last_update.
The full error message is this:
Sun Jun 02 2019 07:07:51 GMT+0200 (centraleuropeisk sommartid)
Error executing script: maximum recursion depth exceeded in comparison
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/homeassistant/components/python_script/__init__.py", line 166, in execute
exec(compiled.code, restricted_globals, local)
File "force_update_state.py", line 8, in <module>
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 877, in set
context,
File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/local/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/local/lib/python3.7/site-packages/homeassistant/util/async_.py", line 195, in run_callback
future.set_result(callback(*args))
File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 905, in async_set
same_attr = old_state.attributes == attributes
RecursionError: maximum recursion depth exceeded in comparison
My guess is that HA doesnt wait for the first call to python script finish to call the next python script. what if you put a “wait” of 5 seconds between each call?
Unfortunately, adding 5 second delay between service calls does not resolve the problem… For now i ended up with two seperate automations… Will see…
Two seperate automations also lead to recursion error. Created issue on the github.
UPD.
After a little thought, and based on the fact that an error occurs when checking the condition (in HA core.py), it seems to me that I was able to get around the problem by changing the attributes along with the state update (added last_forced_update attribute with current datetime). There is no errors for more than a 24h. I look further.
now = datetime.datetime.now()
nowstr = "%02d:%02d:%02d %02d.%02d.%04d" % (now.hour, now.minute, now.second, now.day, now.month, now.year)
#pass entity_id as argument from call
sensor = data.get('entity_id')
#read old state
oldState = hass.states.get(sensor)
oldAttributes = oldState.attributes.copy()
oldAttributes['last_forced_update'] = nowstr
#write old state to entity and force update to record database
hass.states.set(sensor, oldState.state , oldAttributes, force_update=True)
For anyone running into the same issue: I tested this solution and it seems to fix the problem. If you use the following date format, Home Assistant even shows you the formatted date instead of the date string: