I’ve spent quite a while to get the state_changed
event to record data from an automation and then a script, but still no luck. Here’s my full script:
alias: Test State Changed Event
sequence:
- event: state_changed
event_data:
entity_id: input_number.test_number
new_state:
entity_id: input_number.test_number
state: "3.22"
attributes: {}
last_changed: "2023-12-19T04:22:00.123400+00:00"
last_updated: "2023-12-19T04:22:00.123400+00:00"
context:
id: "{{ context.id }}"
parent_id: null
user_id: null
old_state:
entity_id: input_number.test_number
state: "1.1"
attributes: {}
last_changed: "2023-12-19T21:29:00.003517+00:00"
last_updated: "2023-12-19T21:29:00.003517+00:00"
context:
id: "{{ context.id }}"
parent_id: null
user_id: null
mode: single
Running that script shows this error in the logs:
2023-12-19 16:46:53.170 ERROR (MainThread) [homeassistant.core] Error running job: <Job listen state_changed HassJobType.Callback <function handle_subscribe_entities.<locals>.forward_entity_changes at 0x7ff189cf00e0>>
Traceback (most recent call last):
File "/home/dale/.pyenv/versions/3.11.2/envs/homeassistant/lib/python3.11/site-packages/homeassistant/core.py", line 1061, in async_fire
job.target(event)
File "/home/dale/.pyenv/versions/3.11.2/envs/homeassistant/lib/python3.11/site-packages/homeassistant/components/websocket_api/commands.py", line 310, in forward_entity_changes
connection.send_message(messages.cached_state_diff_message(msg["id"], event))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dale/.pyenv/versions/3.11.2/envs/homeassistant/lib/python3.11/site-packages/homeassistant/components/websocket_api/messages.py", line 109, in cached_state_diff_message
return _cached_state_diff_message(event).replace(IDEN_JSON_TEMPLATE, str(iden), 1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dale/.pyenv/versions/3.11.2/envs/homeassistant/lib/python3.11/site-packages/homeassistant/components/websocket_api/messages.py", line 120, in _cached_state_diff_message
{"id": IDEN_TEMPLATE, "type": "event", "event": _state_diff_event(event)}
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dale/.pyenv/versions/3.11.2/envs/homeassistant/lib/python3.11/site-packages/homeassistant/components/websocket_api/messages.py", line 147, in _state_diff_event
return _state_diff(event_old_state, event_new_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/dale/.pyenv/versions/3.11.2/envs/homeassistant/lib/python3.11/site-packages/homeassistant/components/websocket_api/messages.py", line 157, in _state_diff
new_state_context = new_state.context
^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'context'
Note that I do have a context in my new_state, but the problem is that the code is expecting a State object, and this new_state is just a dict. Also for the record, I tried it without any old_state
payload and just got a different, but similar error where the code was failing to parse this dict that it expects to be a State object.
I added a logger to the code to print the objects and you can see the difference in my objects and the ones HA creates.
{'entity_id': 'input_number.test_number', 'state': '3.22', 'attributes': {}, 'last_changed': '2023-12-19T04:22:00.123400+00:00', 'last_updated': '2023-12-19T04:22:00.123400+00:00', 'context': {'id': '01HJ20QHA5A7WM2J18PC2GEZA3', 'parent_id': None, 'user_id': None}}
<state input_number.test_number=0.5; initial=None, editable=True, min=0.0, max=100.0, step=0.01, mode=slider, unit_of_measurement=ft, friendly_name=Test Number @ 2023-12-19T17:39:29.431559-05:00>
You can see the type of the object is just different, and yet I can’t figure out what I could do to change that, since all I have access to is the YAML I’m putting in the event_data
payload of the script above.