Create a script which enables / disables automation based by name

I’m not sure if it’s even possible but is there a way to make script which ENABLES all automations which have SUMMER in it’s name while disabling any automiation which has WINTER in it’s name. And opposite to that create a second script which enables every automation which has WINTER in it’s name while disabling any automation which has SUMMER in it’s name?

Thing is I have a shitload of automations that apply to summer or winter only. I want to switch them by one button (using script) depending of current season.

Yes it is possible. However, it is usually better practice to construct automations so that their conditional logic properly manages execution instead of enabling/disabling automations. I tried using the enable/disable method when I first started using Home Assistant and it led to so much frustration. It is too easy to forget how automations are dependant on each other. If you decide to go down this path I would advise that you create all your automations and scripts in the UI or manually assign them unique IDs so that you can use the “Related” entities tool.

That being said… a basic script is as follows:

alias: "Automations: Winter On/Summer Off"
sequence:
  - variables:
      winter: >
        {{ states.automation
        | selectattr('name', 'search', 'WINTER')
        | map(attribute='entity_id') | list }}
      summer: >
        {{ states.automation
        | selectattr('name', 'search', 'SUMMER')
        | map(attribute='entity_id') | list }}
  - service: automation.turn_on
    target:
      entity_id: "{{ winter }}"
  - service: automation.turn_off
    target:
      entity_id: "{{ summer }}"
mode: single

For that you will need to incorporate a Choose action and determine how you want to define the “current season”. There is a Seasons integration built into HA, but you may prefer to use Input datetime helpers or some other method.

I agree with @Didgeridrew that it would be better to have your automations taking care of itself instead of enabling/disabling automations.

In the past I’ve tried this for some automations that should be disabled when I have visitors at home, but in the end it became a nightmare, specially because automations become unavailable for some time when reloading automations and that was messing up my whole system, to the point I lost control.
In the end I’ve created a helper (toggle) which is set only when I have visitors and then all the other automations use that as conditions or inside the actions to behave as I’m expecting. Later I’ve automated the detection of some of my frequent visitors and it was much simpler to just set a helper when they are detected, and I still can set it manually (with a button right on the side of my entry door, or from a dashboard).

I agree with both of you that making automations with proper logic is better sollution. Thing is that at this moment I have no bloody idea how this logic should work like :slight_smile:

I tried your script, slightly modified:

alias: Enable winter automations
  sequence:
  - variables:
      winter: >
        {{ states.automation
        | selectattr('name', 'search', 'WINTER')
        | map(attribute='entity_id') | list }}
      summer: >
        {{ states.automation
        | selectattr('name', 'search', 'SUMMER')
        | map(attribute='entity_id') | list }}
  - service: automation.turn_on
    data: {}
    target:
      entity_id: "{{ winter }}"
  - service: automation.turn_off
    data: {}
    target:
      entity_id: "{{ summer }}"
mode: single

but it gives me error like this:

Message malformed: value should be a string for dictionary value @ data['alias']

The hell he wants from me? Changing alias name to enable_winter_automations doesn’t change anything. I compared structure of this script with other scripts I have and it looks the same. Like this one for example:

alias: MiBox turn [OFF]
sequence:
  - service: media_player.turn_off
    data: {}
    target:
      device_id: bd5148d383bdae5cdba9df94a02d9c99
mode: single

If you want to use the real season values to make the automations season aware without disabeling them, there is this:

But you can just as easily create sensor or input helper to have a slightly different interpretation of winter.

Once you have the sensor, you can test its state in any automation you like by adding one extra condition that the state of this sensor must be Winter.

1 Like

That is also true. Before I knew how to use helpers (now I know) I used dummy automations for that - meaning automation which contains nothing inside except name. Then I put such automation in my dashboard to switch it on or off and other automations were checking if this dummy automation is on or off (it was one of conditions for them to start). In other words I’ve made myself helper without using helpers :slight_smile:

1 Like

And BTW I know why this script didn’t work. I think it’s a bug or something. You have to name an script in visual editor and then switch to YAML.

I guess it needed some kind of escaping for [OFF]? (or just quotes around the title?)

The error says data['alias']. That’s where you should be looking.

And from experience, I can tell you it’s because you don’t have alias’s value in quotes and you’re using special characters. i.e. [ and ] tell yaml to treat the value differently. You want to assert that this is a string.

alias: "MiBox turn [OFF]"

Also the indenting of sequence: was off, if you compare with your other example.

Secondly, you should just have conditions in your winter and your summer automations that check the time of year.

condition:
- condition: state
  entity_id: sensor.season
  state: summer
1 Like

I think you misunderstood me. I said that this Mi Box script with [OFF] actually works.

the alias field is complaining that it’s not a string. Everything you’ve posted has a string accept what I pointed out. So you’re either looking at an old error or the wrong error or the wrong automation.

Also, make sure that you did what @Edwin_D pointed out with the spacing on alias

Guys - which of your answers should I mark as Sollution since only one can be marked? xD

make a new post with everything correctly formatted and mark that.