Managing multiple triggers into the same automation

Managing multiple triggers into the same automation

Welcome to a new episode of my Tips & Tricks newsletter on Home Assistant.

If you get a new e-mail about the availability of this article it's not spam! Please open it, react and comment.

It will be appreciated and will give me a much better idea of who is actually reading all this stuff.

Thank you so much! 🙏

Automations in Home Assistant can greatly improve efficiency by taking over repetitive tasks but it's important to strike a balance.

Having too many automations can lead to a high maintenance workload and potentially asking you to put more effort to debug or modify each single automation.

One simple great way to reduce the number of automations is combining multiple triggers into a single one so that, in case any changes as well as improvements are needed, you can focus your effort to a single automation only.

Use case

You want to set the ring level of the smartphones of your family members (4) as soon as they leave home.

If you don't combine multiple triggers into the same automation you have to write and maintain 4 different automations.

Let's see how creating a single automation to cover all the family members at the same time!

Software configuration

  1. Home Assistant. I'm currently running version 2022.12.7.
  2. Home zone defined.
  3. Mobile App integration configured in Home Assistant. This integration allows Home Assistant mobile APP (Android / IOS) to be linked with your Home Assistant server.
  4. Mobile APP installed on your smartphone to know if you are at home or not.

Home Assistant code (yaml) / procedure

This is the code you have to write as Automation:

alias: Smartphones - Set ring stream level if away
description: Smartphones - Set ring stream level if away
trigger:
  - platform: zone
    entity_id: person.A
    zone: zone.home
    event: leave
    id: SetRingStreamLevel_A
  - platform: zone
    entity_id: person.B
    zone: zone.home
    event: leave
    id: SetRingStreamLevel_B
  - platform: zone
    entity_id: person.C
    zone: zone.home
    event: leave
    id: SetRingStreamLevel_C
  - platform: zone
    entity_id: person.D
    zone: zone.home
    event: leave
    id: SetRingStreamLevel_D
condition: null
action:
  - choose:
      - conditions:
          - condition: trigger
            id: SetRingStreamLevel_A
        sequence:
          - service: notify.mobile_app_smartphone_A
            message: command_volume_level
            data:
              media_stream: ring_stream
              command: "6"
      - conditions:
          - condition: trigger
            id: SetRingStreamLevel_B
        sequence:
          - service: notify.mobile_app_smartphone_B
            message: command_volume_level
            data:
              media_stream: ring_stream
              command: "6"
      - conditions:
          - condition: trigger
            id: SetRingStreamLevel_C
        sequence:
          - service: notify.mobile_app_smartphone_C
            message: command_volume_level
            data:
              media_stream: ring_stream
              command: "6"
      - conditions:
          - condition: trigger
            id: SetRingStreamLevel_D
        sequence:
          - service: notify.mobile_app_smartphone_D
            message: command_volume_level
            data:
              media_stream: ring_stream
              command: "6"
mode: parallel        

Some specifics about the code above

  1. The trigger section is composed of several triggers, one of each person you want to monitor. A trigger ID is associated with each trigger.
  2. The action section is in charge to execute the same task (setting the ring level) for each smartphone choose according to the trigger fired (trigger ID).
  3. The automation's running mode parallel allows to start a new independent run in parallel with the previous one. Having multiple triggers you could have another trigger is fired when the previous automation is still running.

Thank you for reading. 🙏 I hope you have enjoyed this article!

I'm available for any feedback so, feel free to contact me or share your thoughts in the comments section below.

Your feedbacks will be very appreciated!

Meanwhile, if you liked this article share it or subscribe to my newsletter about Home Assistant to read all previous episodes and still up to date on latest news.

To view or add a comment, sign in

More articles by Emiliano Guerzoni

Insights from the community

Others also viewed

Explore topics