How to trigger on event call service for automations

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

however, this doesnt do anything…

Cant we trigger using events for automations?

please have a look, thanks.

The event_type for automation triggers is automation_triggered not call_service. I just tested it quickly:

image

1 Like

thanks!
keep forgetting to do that, and I always read this: https://www.home-assistant.io/docs/configuration/events/
where event_type automation_triggered isn’t mentioned…
had to weed out the

          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?

is old_state or new_state inside that event data?

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 :wink:

well then you answered your own question!

ha, no I didnt…

I did know I couldnt use the old_state and new_state…
I dont know yet, if I need to guard against ‘none’, and if yes, how.

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?

guess you’re right. Have to get used to my first

    trigger:
      platform: event
      event_type: automation_triggered

automation.
Which is cool btw, because now I can safely re-instate it, without compromising the HA 115.+ instance.
thanks for the confirmation.