Create a single automation for multiple entities

Hi friends,
I’m a HA newbie and in the last weeks I tried out to configure my home automations.
I’ve got a question. My garden has 8 sprinklers but I need to switch off all of them except the one I’ve asked to activate. This comes from the water pressure which is not enough to let my sprinklers working together.
Sprinklers are grouped in a group named ‘Garden’.
Right now I’ve created 8 static automations, one for each sprinkler, which turns off the other ones, this approach is not obviously the best one.
So the question is, starting from my automation, posted as follows, how can I make it dynamic? I’ve searched in the forum, found some guidelines but I wasn’t able to adapt the suggestions to my needs.

alias: turno off other springlers when sp1 is activated
description: Turns off the springlers before activating the current one
trigger:
  - platform: state
    entity_id: switch.irrigatore_1
    from: 'off'
    to: 'on'
condition: []
action:
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_2
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_3
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_4
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_5
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_6
  - service: homeassistant.turn_off
    target:
      entity_id: switch.gocciolatoio_1
  - service: homeassistant.turn_off
    target:
      entity_id: switch.gocciolatoio_2
mode: single
alias: turno off other springlers when sp1 is activated
description: Turns off the springlers before activating the current one
trigger:
  - platform: state
    entity_id: switch.irrigatore_1
    from: 'off'
    to: 'on'
condition: []
action:
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_2
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_3
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_4
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_5
  - service: homeassistant.turn_off
    target:
      entity_id: switch.irrigatore_6
  - service: homeassistant.turn_off
    target:
      entity_id: switch.gocciolatoio_1
  - service: homeassistant.turn_off
    target:
      entity_id: switch.gocciolatoio_2
mode: single

The script definition is very simple.
Thank you very much!
Dariush.

You seem to have pasted the same automation twice.

Are the sprinklers in an actual group?

If they are, surely you can do:

action:
  - service: homeassistant.turn_off
    target:
      entity_id: group.garden

?

thanks for your reply,
yes sorry, pasted twice.
Turning off the whole group I think I’d turn off even the one I asked to activate, am i wrong?
BR, Dariush

You are correct. However, you could turn off the whole group and then just turn on the one you want on.

Thanks Russel for the suggestion, but I’m not sure how doing that…
From the lovelaceui i click on the sprinkler[x], which would be the correct way to turn off the whole garden group and then turning on the sprinkler[x] I clicked on?
hope my doubt is clear :wink:
BR, Dariush

I believe this is what you want. This single automation is triggered when any of the eight switches (sprinklers) is turned on. It then turns off any other switch (sprinkler) that is currently on. If no other switches are on it does nothing.

alias: turn off all other sprinklers when one is activated
description: Turns off all sprinklers except the current one
trigger:
 - platform: state
   entity_id:
     - switch.irrigatore_1
     - switch.irrigatore_2
     - switch.irrigatore_3
     - switch.irrigatore_4
     - switch.irrigatore_5
     - switch.irrigatore_6
     - switch.gocciolatoio_1
     - switch.gocciolatoio_2
   from: 'off'
   to: 'on'
action:
 - variables:
     sprinklers:
       - switch.irrigatore_1
       - switch.irrigatore_2
       - switch.irrigatore_3
       - switch.irrigatore_4
       - switch.irrigatore_5
       - switch.irrigatore_6
       - switch.gocciolatoio_1
       - switch.gocciolatoio_2
     sprinklers_to_close: >
       {{ expand(sprinklers) | selectattr('state', 'eq', 'on')
          | rejectattr('entity_id', 'eq', trigger.entity_id)
          | map(attribute='entity_id') | list }}
 - condition: template
   value_template: '{{ sprinklers_to_close | count > 0 }}'
 - service: switch.turn_off
   target:
     entity_id: '{{ sprinklers_to_close }}'
6 Likes

Wow Taras, it works perfectly!!! Thank you very much.
May I ask for another hint? right now the sequence of actions to be done is defined with a script as follows:

alias: Irrigazione standard
sequence:
  - service: browser_mod.toast
    data:
      message: Programma di irrigazione standard avviato
  - service: homeassistant.turn_on
    target:
      entity_id: switch.irrigatore_1
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.irrigatore_2
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.irrigatore_3
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.irrigatore_4
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.irrigatore_5
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.irrigatore_6
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.gocciolatoio_1
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_on
    target:
      entity_id: switch.gocciolatoio_2
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: homeassistant.turn_off
    target:
      entity_id: group.garden_spinklers
  - service: browser_mod.toast
    data:
      message: Irrigazione standard terminata
mode: single

My wife would define by herself new sequences based on the season (during spring activate sprinklers for let’s say 5 mins, during summer for 15 mins etc), using the script editor would not be the best option (she has no tech skills).
Is there any guideline to give her a user interface within a card to create the whole sequence, or perhaps creating a new sequence starting from a template and define how much time sprinklers should be activated?

BR, Dariush

In your example, the delay for each sprinkler is the same (10 seconds). If the delay is always the same amount for each sprinkler (whether it’s 10 seconds, or 5 minutes, or 15 minutes), you can use a single input_datetime or input_number to set the delay’s duration.

Here’s a simple example of setting delay (whose value must be in seconds) using an input_number that represents time in minutes.

  - delay: "{{ states('input_number.sprinkler_duration') | int * 60 }}"

Controlling the system via the UI is simple: set the desired sprinkler time using the input_number or input_datetime and then execute the script.


NOTE

If you are satisfied that the automation I posted resolves your original request, please consider marking my post 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. It will also place a link below your first post that leads to the Solution post. All of this helps users find answers to similar questions.

hi Taras,
yes this could be a quick solution, i could even put more input_numbers to control each single sprinkler duration.
I’m curious about how a new automation could be created starting from a card config as required by ‘the boss’ :wink:
BR, Dariush

Color me confused. My yaml works flawlessly to mirror the state and brightness attribute of two light switches, and the file editor has a green check indicating there are no errors in the code, but then when I run a Check Configuration it returns a duplicate anchor error.

Yaml:

- id: mirror_master_bedroom_lights
  alias: Mirror Master Bedroom Lights
  trigger:
    - entity_id: &entities
        - light.master_bedroom_lights
        - light.master_hall_light
      platform: state
  action:
    - service_template: light.turn_{{trigger.to_state.state}}
      data:
        entity_id: *entities
        brightness: "{{ trigger.to_state.attributes.brightness }}"

Check Configuration Error:

Error: found duplicate anchor 'entities' in line 91

Line 91 is the trigger entity, - entity_id: &entities, but I’ve used a similar code in the past with no issues, so I’m not sure what I’m missing here. I know I can list out the entities in the trigger & actions, but I prefer a minimalist approach to writing code. Thoughts?