Trigger on automation enabled?

Is there any way for an automation to trigger when it is enabled?

I know about the automation_reloaded event, but it does not seem to be fired on enabled. As this is for a blueprint there is no way for me to know ahead of time what the automation’s own entity id is, so a state trigger couldn’t be used at all.

The only thing I could come up with was the trigger below for a state_changed event, but it does not seem to work either. I am guessing the issue in this case is that the corresponding event gets sent before the trigger is enabled in the state machine.

- trigger: event
  event_type: state_changed
  event_data:
    entity_id: "{{ this.entity_id }}"
    old_state:
      state: "off"
    new_state:
      state: "on"

If there is no way to currently do this, certainly a new automation_enabled event would be useful if added to Home Assistant?

I suppose…

If you put the stuff you want the automation to do in a script, you could have a second automation triggered by the state of the first one calling the same script…?

Sounds a bit “by his own bootstraps”.

Again, it’s an automation blueprint. It has many other state triggers and a whole set of conditions. If the automation is disabled and then enabled, I’d like it to immediately trigger so that the conditions can determine whether to move on to the actions.

Is this an edge case? For sure. But my OCD likes to try and cover all bases. This is to work around the issue with the state triggers (which in this case monitors states and attributes of a media player) only firing on explicit state changes.

You’d have to use a template trigger, or omit old_state and new_state. Event triggers only trigger when the entire object is comparable. I.e. you’d need to provide old_state and all it’s properties, not just state.

Ahh, I will give that a shot and add the state to my conditions. Though doing it that way I guess will be filling up the traces with one extra aborted run (due to conditions not fulfilled) every time the automation’s last_triggered attribute changes…

Yep, it will, but automations only change state rarely, so it won’t be huge overhead.

Also, automations restore state (main state), so if you use a simple template trigger, it should work the way you want without checking the previous state.

{{ is_state(this.entity_id, 'on') }}

At least they should restore main state. I’d verify that this doesn’t trigger at startup.

Template trigger (is_state) seems to have done the trick. I thought that would’ve had the same issue as a state trigger, where it would never fire because its initial state would already be true, but it does seem to work just as you describe.

ah yep, meant is_state