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?
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:
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 ?
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'
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 @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!
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 enterorleave.
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?
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: