Automation's calling Automation's, Triggers and Conditions

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.

Just add the time 10:00 into the trigger as well, using the whole lot as an automation rather than script


automation:
  - alias: Lights - Lounge - On - Low Light
    trigger:
      - platform: numeric_state
        entity_id: sensor.lounge_light_level
        below: '1601'
      - platform: time
        at: '10:00:00'
    condition:
      - 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
    action:
      - data: {}
        entity_id: script.lights_lounge_on_bright
        service: script.turn_on

…to be continued, work is calling

Hi @sparkydave - yes, indeed I added the 10:00 trigger in the automation. The reason I went with a script is that I can reuse that for my returning home presence check. Again, I could add that as third trigger into the automation…but the intention is to have a number of actions that could occur when I returned home.

If I have an automation for returning home, I can then clearly see the scripts this is firing off rather than having to check all my automations to see if they have the returning home trigger in them.

Guess its different ways to cut the same thing - for me this was a bit of learning and the key take away was automation to automation skipped the triggers and conditions!! Cheers

Have a look at using chooser: in your automation, or a single automation to fire off multiple scripts each with conditions. Getting an automation to trigger another automation isn’t the right way to do it, it’s messy.

1 Like

The purpose of the original post was to share with newbie’s (like myself) the trap I fell into with automations calling automations, and the triggers and conditions being bypassed.

It kinda evolved a bit, originally went down a route of thinking I was very clever reusing scripts with multiple automations calling them, then realised the triggers on an automation are OR, so I could consolidate these down.

Took onboard Dave’s comments about consolidating these down using the choose.

Also ventured into sensors – one around was the light level and instead of checking the actual values in the automation handed this off to a sensor.

The second more exciting one was a new find of TOD – this did tosimplify things because now I didn’t need to check if the time was after my start window (09:00) and before my end window (sunset -00:45). Using TOD I’ve now got a sensor which is on turning these hours and off otherwise – this did make the automation simpler (sharing this one as I stumbled upon it by accident and think its great):

binary_sensor:
  - platform: tod
    name: daytime hours
    after: '09:00'
    before: sunset
    before_offset: '-00:45'

and the end result being:

- id: '1599247532179'
  alias: Day - Lounge Light Levels
  description: Multiple triggers drive this, condition makes this a single automation
  trigger:
  - entity_id: binary_sensor.lounge_low_light
    for: 00:05
    platform: state
    to: 'on'
  - entity_id: binary_sensor.daytime_hours
    platform: state
    to: 'on'
  - entity_id: group.home_occupied
    platform: state
  - entity_id: binary_sensor.lounge_low_light
    for: 00:05
    platform: state
    to: 'off'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.lounge_low_light
        state: 'on'
      - condition: state
        entity_id: group.home_occupied
        state: home
      - condition: state
        entity_id: binary_sensor.daytime_hours
        state: 'on'
      sequence:
      - scene: scene.lights_lounge_on_bright
    default:
    - condition: state
      entity_id: binary_sensor.daytime_hours
      state: 'on'
    - data: {}
      entity_id: group.lounge
      service: homeassistant.turn_off
  mode: single

My minor annoyance currently is the trigger of state change, to use the For element it must have a To value, annoying for me as I don’t care what it changes to (On or Off), I just want it in that state for 5 minutes.

Hope my journey helps others at some point in the future…