Actually, that’s not the reason. It should work, but doesn’t because a bug was introduced a year ago when they started introducing code to track the context of events, etc. See PR #16415. The bug is on this line:
When the notification is dismissed, to_s
is None
, and so to_s.context
causes an exception.
Here is the corresponding state_changed
event (where to_s
in the code corresponds to new_state
in the event):
{
"event_type": "state_changed",
"data": {
"entity_id": "persistent_notification.test",
"old_state": {
"entity_id": "persistent_notification.test",
"state": "notifying",
"attributes": {
"message": "test"
},
"last_changed": "2019-09-05T18:16:06.111857+00:00",
"last_updated": "2019-09-05T18:16:06.111857+00:00",
"context": {
"id": "d305a43ea31843b1a45e8f0c735e206e",
"parent_id": null,
"user_id": null
}
},
"new_state": null
},
"origin": "LOCAL",
"time_fired": "2019-09-05T18:16:10.736375+00:00",
"context": {
"id": "ff1c09780ca146deb383dae314d06082",
"parent_id": null,
"user_id": null
}
}
I just tried it and got this exception:
2019-09-05 13:16:10 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=persistent_notification, service=dismiss, service_data=notification_id=test>
2019-09-05 13:16:10 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=persistent_notification.test, old_state=<state persistent_notification.test=notifying; message=test @ 2019-09-05T13:16:06.111857-05:00>, new_state=None>
2019-09-05 13:16:10 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event persistent_notifications_updated[L]>
2019-09-05 13:16:10 ERROR (MainThread) [homeassistant.core] Error doing job: Exception in callback async_track_state_change.<locals>.state_change_listener(<Event state_...ew_state=None>) at /home/phil/repos/home-assistant/homeassistant/helpers/event.py:77
Traceback (most recent call last):
File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
self._context.run(self._callback, *self._args)
File "/home/phil/repos/home-assistant/homeassistant/helpers/event.py", line 96, in state_change_listener
event.data.get("new_state"),
File "/home/phil/repos/home-assistant/homeassistant/core.py", line 372, in async_run_job
target(*args)
File "/home/phil/repos/home-assistant/homeassistant/components/automation/state.py", line 83, in state_automation_listener
call_action()
File "/home/phil/repos/home-assistant/homeassistant/components/automation/state.py", line 69, in call_action
context=to_s.context,
AttributeError: 'NoneType' object has no attribute 'context'
So, your instinct was correct, it’s just that the bug is preventing it from working.
I think you can achieve the same thing with something like this:
trigger:
platform: event
event_type: state_changed
event_data:
entity_id: persistent_notification.test
condition:
condition: template
value_template: >
{{ trigger.event.data.old_state and
trigger.event.data.old_state.state == 'notifying' }}
I just gave it a try and it seems to work.
Oops, my bad. @123’s suggestions, of course, are another perfectly fine way to solve the problem. I was more commenting on the bug that prevented your first attempt from working.