Script to disable/enable all automations right after Startup / Boot?

I have a weird behaviour that when I reboot my supervisor (TrueNAS Scale) or update the HA docker container that automations with physical switches do not work.

I first have to disable the automation via GUI and then enable them and the switch works again as intended. I have to do that for each affected automation individually.

Previously that was ok-ish because the GUI had an iPhone-like toggle button but they changed the GUI so now much more steps are involved (right click > disable, right click > enable … for each).

I was wondering how some sort of (Boot/Start) script would look like that first disables all (!) automations and then enables them right after?

alias: Toggle Automations
trigger:
  - platform: homeassistant
    event: start
action:
  - variables:
      all: >
        {{ states.automation | map(attribute='entity_id')
          | reject('eq', this.entity_id) | list }}
  - service: automation.turn_off
    target:
      entity_id: '{{ all }}'
  - delay: '00:00:02'
  - service: automation.turn_on
    target:
      entity_id: '{{ all }}'

EDIT #1

Be advised that your idea, to turn off/on all automations on startup, effectively means you lose the ability to manually disable an automation (because on startup any disabled automation will be automatically re-enabled).

EDIT #2

Correction. Omits itself from the list of automations to be turned off/on.

EDIT #3

Correction. Added missing | to the template for the all variable.

EDIT #4

Correction. Added missing 'eq' to the reject filter.

Be advised that your idea, to turn off/on all automations on startup, effectively means you lose the ability to manually disable an automation (because on startup any disabled automation will be automatically re-enabled).

Yes! Good observation … I actually thought about this beeing an issue as well. But currently all of my automations are “enabled” by default so that is not an issue.

But you are right … this is only considered a “quick fix” until I figure out the root cause.

Thanks a lot!

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

I tried to past this in as a script - but received an error. I can past it in as an automation but that does not work because it only disables all automations - including itself! - and therefore does not run the enable action anymore.

I had this too with the Deconz integration (my physical switches were mostly Aqara Opple). The logs showed that Deconz device events could not be coupled because the deconz service wasn’t ready yet (paraphrased).

My workaround was an an automation to reload all automations 30 seconds after startup. I posted a bug report with the Deconz integration. It was investigated and acknowledged but never solvedin months to come. In the end I gave up and switched to ZHA and haven’t had the problems since.

This was all before automations checked for changes on relead, so the workaround may not work anymore. But if you use Deconz too, my original bug report may need to be reopened/revived.

Interesting … so you would recommend ZHA over Deconz? I saw their Conbee II is also supported.

Currently I run DeconZ as a docker container as well … so to switch to ZHA do I also have to run a different container in parallel?

I’m satisfied with ZHA, but it does depend on supported devices what is best for you. I liked the fact that Deconz was independent from HA, and I probably wouldn’t have switched if the bug would have been fixed. I chose ZHA because it is what Nabu Casa is maintaining it and it is a part if HA itself. No added containers or addons needed.

What I still lack in ZHA is directly linking buttons to lights through groups. ZHA should be able to do that too but I can’t get it to work and with Deconz it was easy. So I’m hesitant to pick a winner.

I corrected the example posted above. It omits itself from the list of automations to be turned off/on.

I tried out the ZHA … I looks really cool but I encountered an issue where some lights do not respond if for example I press my on remote (all lights go on) and off remote (all lights off) … sometimes a few lights stay on and do not react.

I felt deconz was more responsive and always executed correctly…

Still it is a pity that a known bug (posted 7 months ago) is not fixed and weird workarounds like this are needed. I’v pointed out to the developer I was not alone, and replied on several topics just like this one. My issue was auto closed and locked:

Still not accepted?

Thwarted by the lack of a single | in the template. :man_facepalming:

I’ve corrected the posted example.

missing pipe

I found that in the meantime as well - but if I run the actions … nothing happens?

I think it may have something to do with “this.entity_id”? I read that some people said it is deprecated …

this.entity is valid when the automation is triggered by its trigger.

If 'Run Actions ’ is something you want to do with this, or any other automation, you can’t reference the trigger or this objects within the automation’s action. If this is how you intend to use this automation (why I don’t know) you should replace this.entity_id with the automation’s actual entity_id (in quotes).
For example:

  - variables:
      all: >
        {{ states.automation | map(attribute='entity_id')
          | reject('automation.toggle_automations') | list }}

Reference: Testing your automation

Where exactly did you read this?

When I run this (EDITED!) code it triggers all automations disabled. It just not continues because the initiating automation is also disabled so it does not re-anable all automations.

If I trigger this code nothing happens:

If I put this in the template editor I get an error:

…but without the entity rejection it works:

That is why I believe there is some syntax error…

And this also does not work:

alias: Toggle Automations
trigger:
  - platform: homeassistant
    event: start
action:
  - variables:
      all: >
        {{ states.automation | map(attribute='entity_id')
          | reject('eq', 'automation.toggle_automations') | list }}
  - service: automation.turn_off
    target:
      entity_id: '{{ all }}'
  - delay: '00:00:02'
  - service: automation.turn_on
    target:
      entity_id: '{{ all }}'
1 Like

That works! Great! Thanks a lot!

1 Like