Combining automations?

I’m trying to figure out if is it possible to combine some of my automations to simplify things. For example I have an automation that sends a notification when someone arrives to a zone:

alias: arrival home notification
description: ''
trigger:
  - platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller
    zone: zone.home
    event: enter
condition: []
action:
  - service: notify.mobile_app_ignacio_s_phone
    data:
      message: '{{ trigger.to_state.name }} is arriving home'
mode: queued
max: 3

My problem is that I have to create one for notification for entering and one for leaving, and then do the same for each zone. Is there a way to do this more programmatically so I have a single automation for entering, and leaving different zones?

Thanks!

I’ve been trying to figure this out for 3 months and this weekend I finally had my aha moment. For months, automations worked fine separately but every time I combined them, I’d get undesirable behavior.

I know what I was doing wrong and I cut my automations by 50% this weekend.

You can have multiple triggers in an automation and then use the choose functionality to set your parameters of each. I tried to pick a simple one you could see:

alias: Laundry Room Auto-On/Off
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 124da39445f8d20d4fc7ee6248ae2209
    entity_id: binary_sensor.laundry_room_motion_sensor
    domain: binary_sensor
  - type: no_motion
    platform: device
    device_id: 124da39445f8d20d4fc7ee6248ae2209
    entity_id: binary_sensor.laundry_room_motion_sensor
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
condition: []
action:
  - choose:
      - conditions:
          - type: is_motion
            condition: device
            device_id: 124da39445f8d20d4fc7ee6248ae2209
            entity_id: binary_sensor.laundry_room_motion_sensor
            domain: binary_sensor
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.laundry_room_lights
      - conditions:
          - type: is_no_motion
            condition: device
            device_id: 124da39445f8d20d4fc7ee6248ae2209
            entity_id: binary_sensor.laundry_room_motion_sensor
            domain: binary_sensor
            for:
              hours: 0
              minutes: 1
              seconds: 0
        sequence:
          - service: light.turn_off
            data:
              transition: 5
            target:
              entity_id: light.laundry_room_lights
    default: []
mode: single

The mistake I kept making was I kept adding new ‘chooses’ to incorrect sections so it was doublestacking the conditions. This is partially my error but also, I think the page could be designed better where it’s easier to tell.

When adding ADDITIONAL choose sections for DIFFERENT triggers in the same automation, make sure you scroll all the way to the bottom to add the action/choose section. This adds a NEW choose vs appending previous ones.

Here is an example of mine where the message is customised based on the state.

- alias: "Notify If Internet Status Changed"
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.internet_connection
  condition:
    - condition: state
      entity_id: binary_sensor.pieter_present
      state: "on"
    - condition: time
      after: "06:00:00"
      before: "23:00:00"
  action:
    - service: notify.mobile_app_ceres
      data:
        title: "Home"
        message: "The Internet is {{ 'available' if is_state('binary_sensor.internet_connection', 'on') else 'unavailable' }}."
        data:
          group: "home-internet"
          url: homeassistant://navigate/lovelace/internet

You’re already on the right path as you are identifying the person in the message. You can also get the event data to determine whether the message should use “arriving” or “leaving”. Print trigger.event.data to your message or use the event listener under the developer tools to see what’s available. I don’t have a zone example so haven’t seen it, but if you can get the data I can help with the rest.

I am trying to establish an automation for 2 separate input Booleans. Boolean 5 and Boolean 6.

5 Opens a traffic announcement automation for my commute. Boolean 6 should also turns on and off another traffic announcement. I am trying to combine them in one single automation but only the first 2 “choose function” works correctly. The other 2 does not react. Is this even possible ?

alias: Boolean - Trafik (SİL)
description: ''
trigger:
  - platform: state
    entity_id:
      - input_boolean.notify_home5
      - input_boolean.notify_home6
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home5
            state: 'on'
        sequence:
          - service: automation.turn_on
            target:
              entity_id: automation.new_automation_2
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home5
            state: 'off'
        sequence:
          - service: automation.turn_off
            target:
              entity_id: automation.new_automation_2
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home6
            state: 'on'
        sequence:
          - service: automation.turn_on
            target:
              entity_id: automation.trafik_45_dakika_duyuru
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home6
            state: 'off'
        sequence:
          - service: automation.turn_off
            target:
              entity_id: automation.trafik_45_dakika_duyuru
    default: []
mode: single

It executes only the first actions because of the input_boolean would be on or off, one state of them it would be in every case.
You can use an id for the trigger like that:

  - platform: state
    entity_id: input_boolean.notify_home5
    id: 'home5'
  - platform: state
    entity_id: input_boolean.notify_home6
    id: 'home6'

and use this too in your choose conditions:

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home5
            state: 'on'
          - condition: trigger
            id: 'home5'
        sequence:
          - service: automation.turn_on
            target:
              entity_id: automation.new_automation_2
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home5
            state: 'off'
          - condition: trigger
            id: 'home5'
        sequence:
          - service: automation.turn_off
            target:
              entity_id: automation.new_automation_2
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home6
            state: 'on'
          - condition: trigger
            id: 'home6'
        sequence:
          - service: automation.turn_on
            target:
              entity_id: automation.trafik_45_dakika_duyuru
      - conditions:
          - condition: state
            entity_id: input_boolean.notify_home6
            state: 'off'
          - condition: trigger
            id: 'home6'
        sequence:
          - service: automation.turn_off
            target:
              entity_id: automation.trafik_45_dakika_duyuru
    default: []

Does this seems right?

alias: arrival and departure home notification
description: ''
mode: queued
trigger:
  - platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.home
    event: enter
    id: Enter
  - platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.home
    event: leave
    id: Leave
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Enter
        sequence:
          - service: notify.mobile_app_ignacio_s_phone
            data:
              message: '{{ trigger.to_state.name }} is arriving home'
          - service: notify.mobile_app_pixel_4a
            data:
              message: '{{ trigger.to_state.name }} is arriving home'
    default:
      - condition: trigger
        id: Leave
      - service: notify.mobile_app_ignacio_s_phone
        data:
          message: '{{ trigger.to_state.name }} is leaving home'
      - service: notify.mobile_app_pixel_4a
        data:
          message: '{{ trigger.to_state.name }} is leaving home'
max: 3

Then I can add two more triggers per zone and to more conditions to send departure and arrival notifications for that zone?

Is there a way of streamlining this further so I have message: '{{ trigger.to_state.name }} is {{ leaving/arriving} {{ zone }}' instead of copy and pasting and slightly modifying the notifications?

Thanks!

alias: arrival and departure home notification
mode: queued
trigger:
  - id: arriving
    platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.home
    event: enter
  - id: leaving
    platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.home
    event: leave
condition: []
action:
  - variables:
      msg: '{{ trigger.to_state.name }} is {{ trigger.id }} home'
  - service: notify.mobile_app_ignacio_s_phone
    data:
      message: '{{ msg }}'
  - service: notify.mobile_app_pixel_4a
    data:
      message: '{{ msg }}'
1 Like

thanks @123 . Is it possible to expand this to work on multiple zones? for example if have zone.home, zone.shool, zone.work, etc? I imagine I would have to change the id for the triggers to things that will be less readable so {{ trigger.id }} would not work as well and then i would need something similar for the zones. Thanks again for your help!

alias: arrival and departure home notification
mode: queued
trigger:
  - platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.home
  - platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.school
  - platform: zone
    entity_id: person.ignacio,person.emilie,person.stroller,person.tesla
    zone: zone.work
condition: []
action:
  - variables:
      msg: "{{ trigger.to_state.name }} is {{ iif('trigger.event == 'enter', 'arriving at', 'leaving') }} {{ trigger.zone.attributes.friendly_name }}"
  - service: notify.mobile_app_ignacio_s_phone
    data:
      message: '{{ msg }}'
  - service: notify.mobile_app_pixel_4a
    data:
      message: '{{ msg }}'
1 Like

@123 it seems like the automation is only working when someone arrives but not when someone leaves. Any ideas for how to debug and solve this?

This is my action block:

action:
  - variables:
      msg: "{{ trigger.to_state.name }} is {{ iif('trigger.event' == 'enter', 'arriving at', 'leaving') }} {{ trigger.zone.attributes.friendly_name }}"
  - service: notify.mobile_app_ignacio_s_phone
    data:
      message: '{{ msg }}'
  - service: notify.mobile_app_pixel_4a
    data:
      message: '{{ msg }}'

According to the documentation for Zone Trigger:

Zone trigger fires when an entity is entering or leaving the zone.

The template for msg uses an immediate-if statement to check if the triggering event was enter and then reports arriving at. If it’s not enter then it reports leaving.

Each of the three Zone Triggers in the example I posted do not include an event: option. That means they trigger when the value of event is either enter or leave.

Sorry, this is new to me and I’m having troubles understanding. If I follow you correctly, the triggers as I have them setup bellow should work for when someone enters or leaves a zone. If this is the case, how can i debug the reason for this problem and solve it?

That worked. Thank you very much.

Check the automation’s trace to understand what’s happening.

This is my first time looking at a trace in home assistant. I can see that the automation only gets triggered when someone enters a zone but not when they exit. I’m not sure how I can use that information: