Spend a bit of time working through this and thought I would share my work in the hope it will help someone else in time who might be working through the same sort of thing. I got stuck on automations calling automations as the triggers and conditions are ignored (found out the hard way).
Scenario: Turn the lounge lights on when the light level is below a certain point (1600 lux), but also between a certain time (10:00 and sunset -45 mins) and also when there was someone home.
Initially, I had an automation (one) with a trigger of numeric state below 1600 for the light sensor and conditions of the time and presence state. The actions were then to turn the lights on. Then, I had a secondary automation with a state trigger for the presence from away to home which called the first automation.
There were two issues with this:
- If the light level is below 1600 and 10:00 rolls around then automation one is not triggered
- When presence changes to home, automation two called automation one but discovered triggers and conditions don’t apply for the called automation – THIS was the one that got me stuck!
So findings (all documented and in the community posts):
- Calling an automation bypasses the triggers and conditions and simply runs the actions
- Triggers on automation are OR whereas the Conditions are AND
My solution was to move conditions to a script which could be called by both automations:
lights_lounge_on_low_light:
alias: Lights - Lounge - On - Low Light
mode: single
sequence:
- condition: and
conditions:
- below: '1601'
condition: numeric_state
entity_id: sensor.lounge_light_level
- condition: state
entity_id: automation.system_at_home
state: 'on'
- before: sunset
before_offset: -00:45
condition: sun
- after: '10:00'
condition: time
- data: {}
entity_id: script.lights_lounge_on_bright
service: script.turn_on
So this script when called will check 1) the light level is low, 2) someone is home, 3) its before sunset and 4) its after 10:00 – if all true then calls the script to turn the lights on bright.
My automations then call this (with some duplication of the conditions but being used as triggers):
- id: '1599247532179'
alias: Day - Lounge Low Light
description: ''
trigger:
- below: '1601'
entity_id: sensor.lounge_light_level
for: 0:05:00
platform: numeric_state
- at: '10:00'
platform: time
condition: []
action:
- data: {}
entity_id: script.lights_lounge_on_low_light
service: script.turn_on
This automation is fired when EITHER the light level is below 1600 OR the time is 10:00 – both of these triggers are duplicated as conditions in the called script but as they triggered here they will pass the condition. This addresses my initial failing that at 10:00 if the light sensor was below 1600 already it wouldn’t be called.
My second automation is:
- id: '1599337567851'
alias: System - At Home
description: ''
trigger:
- entity_id: binary_sensor.at_home
from: 'off'
platform: state
to: 'on'
condition: []
action:
- data: {}
entity_id: script.lights_lounge_on_low_light
service: script.turn_on
mode: single
When the state of presence (binary_sensor.at_home) changes to on (home) then the script is called – again the trigger from this automation in the in the conditions of the script but will pass. Now I could add this into state change as an OR trigger in my first automation. However, I will be adding additional actions in specific to the arriving home which this is one of them, felt cleaner separating these for longer term maintenance.