Python script "RecursionError: maximum recursion depth exceeded in comparison"

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:

- alias: 'Uppdate alarm sensor'
  trigger:
    platform: state
    entity_id: sensor.alarm_last_update
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:  
    - data:
        entity_id: sensor.alarm_active
      service: python_script.force_update_state  
    - data:
        entity_id: sensor.alarm_connected
      service: python_script.force_update_state 
    - data:
        entity_id: sensor.alarm_ready
      service: python_script.force_update_state 
    - data:
        entity_id: sensor.alarm_state
      service: python_script.force_update_state

The script looks like this:

#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…

Are one of those scripts making changes that affect sensor.alarm_last_update? If so, you have an endless loop there.

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

I have exactly same problem using exactly same simple script (for some sensors state force-update every 1 minute). Did you find a reason or solution?

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?

I ended up skipping python script and used this auomation instead.

- alias: 'Uppdate alarm sensor'
  trigger:
    platform: state
    entity_id: sensor.alarm_last_update
  condition:
    condition: template
    value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
  action:  
    - service: mqtt.publish
      data_template:
        topic: "alarm/visonic/alarmpanel/active"
        payload: "{{ states('sensor.alarm_active') }}"
        retain: true

However I dont use this setup anymore since I got a direct connection to the alarm instead.

Just thought about it too ) Added 5 second delay between actions in automation. I will write about the result.

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)

1 Like

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:

nowstr = "%04d-%02d-%02dT%02d:%02d:%02d+00:00" % (now.year, now.month, now.day, now.hour, now.minute, now.second)