Best practice for automations that have two conditions

Hi,

I’m starting to automate some of my lighting.
I often come across situations where I need two conditions to be true to trigger an automation action, e.g.
“switch on light X WHEN the sun sets AND the tv is on”.
The goal is to have the light turn on when I’m watching tv and the sun sets and also if it’s already dark and I turn on the tv.
So in these cases I always end up with two automation scrips for basically the same thing, one being

  • trigger: sunset, condition: tv is on
    and the other one being
  • trigger: tv turns on, condition: after sunset

Is there a more elegant way to handle these kinds of cases?
I could imagine putting both conditions into the condition: section of an automation script, but how to trigger it then?
There’s probably a way to fire a trigger event every minute, but that does not sound very efficient to me…

Sebastian

Try using the state ‘below_horizon’ for the condition.

You can have two triggers:
trigger: below_horizon, tv turns on
condition: tv is on, below_horizon

Ok, to be more specific, this is how I’m doing it currently.
I’m using the state of my Kodi box here instead of the state of the TV.

First automation:

alias: 'Lights On: It is dark + Kodi turns on'
trigger:
  platform: state
  entity_id: media_player.kodi_wetek
  from: 'off'
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: light.desklamp
        state: 'off'
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
action:
  service: scene.turn_on
  entity_id: scene.wz_watching_movie

Second automation:

alias: 'Lights On: Movie + Sun sets'
trigger:
  platform: sun
  event: sunset
  offset: '+00:05:00'
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: light.desklamp
        state: 'off'
      - condition: state
        entity_id: light.floorlamp
        state: 'off'
      - condition: or
        conditions:
          - condition: state
            entity_id: media_player.kodi_wetek
            state: 'playing'
          - condition: state
            entity_id: media_player.kodi_wetek
            state: 'idle'
          - condition: state
            entity_id: media_player.kodi_wetek
            state: 'paused'
action:
  service: scene.turn_on
  entity_id: scene.wz_watching_movie

I find it a bit cumbersome that I have to always script two automations for basically the same thing with the trigger<->condition swapped.
I was hoping that there was a more elegant way of expressing these cases.

Sebastian

2 Likes

I’m doing something similar with the harmony-api (so I use switch states as triggers); not sure if this will help:

# MQTT/Harmony
- alias: 'Theater Lights'
  trigger:
    - platform: state
      entity_id: switch.watch_tv
      state: 'on'
    - platform: state
      entity_id: switch.firetvplex
      state: 'on'
    - platform: state
      entity_id: switch.chromecast
      state: 'on'
    - platform: state
      entity_id: switch.bluray
      state: 'on'
  condition:
    condition: sun
    after: sunset
    after_offset: "-00:45:00"
  action:
    - service: scene.turn_on
      entity_id: scene.sunset
    - delay: 00:01:30
    - service: scene.turn_on
      entity_id: scene.sunset_dimmed

Ah! Thanks!
You can have multiple triggers in the same automation script!
(Now I’m getting, what you were trying to tell me, Daniel :wink:

I think that will help!
Again, thanks!

2 Likes