sorry if I repost, but cant find the original, would love to understand this:
have an automation which tracks last triggered automations:
- alias: Last Automation
id: Last Automation
initial_state: false
trigger:
platform: event
event_type: state_changed
however, since this creates listeners on all states known HA115, which results in heavy traffic and system overload, I hoped to be able to write it using the event call_service, just as I do with an identical automation for scripts:
- alias: Last Script
id: Last Script
trigger:
platform: event
event_type: call_service
event_data:
domain: script
which would translate to something like:
- alias: Last Automation
id: Last Automation
initial_state: false
trigger:
platform: event
event_type: call_service
event_data:
domain: automation
service: trigger
trigger.event.data.old_state is not none and
trigger.event.data.new_state is not none and
trigger.event.data.new_state.attributes.last_triggered !=
trigger.event.data.old_state.attributes.last_triggered
conditions… no old_state and new_state are available.
not really sure how to repack those though, to catch the uneventful errors. Maybe, using automation_triggered, I dont need those?
no, if you check an event automation_triggered, it’s quite short:
{
"event_type": "automation_triggered",
"data": {
"name": "Script ran filed",
"entity_id": "automation.script_ran_filed",
"source": "state of sensor.last_script"
},
"origin": "LOCAL",
"time_fired": "2020-09-28T13:12:29.819164+00:00",
"context": {
"id": "43redacted641e",
"parent_id": "431fcredacted5137a",
"user_id": null
}
}
I cant even use
{{not trigger.event.data.object_id.startswith('sense_') and
trigger.event.data.object_id not in skip_list}}
but explicitly need
{{not trigger.event.data.entity_id.split('.')[1].startswith('sense_') and
trigger.event.data.entity_id.split('.')[1] not in skip_list}}
as said, I might not need the guard for none or old!=new but was so used to that using the state_changed config for this automation, I was somewhat insecure about letting it out.
this is a part of the old state_changed automation:
...
{{trigger.event.data.entity_id.startswith('automation.') and
trigger.event.data.old_state is not none and
trigger.event.data.new_state is not none and
trigger.event.data.entity_id.split('.')[1] not in skip_list and
trigger.event.data.entity_id.split('.')[1] not in summary_list}}
mode: queued
max: 50
action:
- condition: template
value_template: >
{{trigger.event.data.new_state.attributes.last_triggered !=
trigger.event.data.old_state.attributes.last_triggered}}
which I now have replaced with:
{{not trigger.event.data.entity_id.endswith('_summary') and
not trigger.event.data.entity_id.split('.')[1].startswith('sense_') and
trigger.event.data.entity_id.split('.')[1] not in skip_list}}
works fine, but of course, I havent yet ran into an issue
And when would an event fire with None from an automation trigger?
From a state we have startups or shutdown events where to_state is none or from_state is none. But for an event when an automation triggers, when would it be populated with none?