Store Automation on/off state

When HA starts, I don’t like it when automations fire while initialization is occurring. To get around this, I use this script:

customstartup:
  sequence:
    - service: automation.turn_off
      entity_id: group.all_automations

When HA is finished loading, I then run this:

system_cleanup:
sequence:
- service: homekit.start
- service: automation.turn_on
entity_id: group.all_automations

The problem I’m having is I now want to save the state of an automation when I turn it on or off from the GUI, and have that reflected in system_cleanup.

I was thinking that I could create an automation that adds every automation that is “off” to a new group anytime an automation is turned on or off, and then use this group in system_cleanup to turn these automations off.

How can I create an automation that fires anytime an automation is turned on or off?

you might be able to use the light_store python script that @pnbruckner made and modify it for use with automations.

But are there just a couple of automations that are a problem running on restart? if that’s the case then it might just as easy to turn those to "initial_state: ‘off’ " then turn those back on after HA completes it’s restart. I do that for some low battery notifications that erroneously report low batteries at restart.

Are you referring to Z-Wave nodes?

If so, one simple way to fix it is to include a condition in the automations that requires the zwave network to be ready.
That way you can leave out the initial_state code all together such that the automations will just restore to their previous states on boot.

1 Like

There were some good suggestions already, but to answer your specific question, just like any other entity:

trigger:
  - platform: state
    entity_id: automation.xyz
    to: 'on'
  - platform: state
    entity_id: automation.xyz
    to: 'off'

etc.

Or to be more generic:

trigger:
  platform: event
  event_type: state_changed
condition:
  condition: template
  value_template: >
    {{ trigger.event.data.entity_id.startswith('automation.') and
       trigger.event.data.new_state.state != trigger.event.data.old_state.state }}

etc.