Help me understand triggers / conditions

Hello everyone,

I am at a loss. Here is what I want to do:
turn on the christmas tree when one of two lights are turned on. but only if the TV isn’t playing.
If the TV starts playing, turn off the christmas tree and turn it on when the TV stops playing.
I want to put everything in one Automation (because I want to deactiveate it when Christmas is over).

My problems are: I can only trigger an automation on state change (is turned on / is turned off) but I would need to check for “is the light turned on” and “is the tv off” if yes, christmastree → on, if not christmastree → off.
What am I missing?

Thank you ever so much for your help!

when is about triggering.
if is about condition.

Trigger:
. state change in any of two lights
. state change in tv
Condition:
. nothing
Actions:
Choose
. condition 1: if tv is not playing and any of two lights are on
. action:turn on Christmas lights

. condition 2: if tv is playing
. action: turn off Christmas lights

Looks ok to me, try and let me know

1 Like

Oh my god! YES!
that makes absolute sense! I was kind of fixated on writing the TV into the conditions, and not the triggers.
Thank you!!!

1 Like

Don’t forget about trigger IDs, they allow you to put a condition based on the source that triggered the automation. For instance, you could have a trigger ID for when the lights change to ON and another for when they change to OFF.

To use them, click the 3 dots on a trigger and click “Edit ID”. You can enter a name like “lights-off” and then put a condition for “Triggered by: lights-off”.

Hopefully this might help you now or in the future!

You can use variables in each trigger to help make decisions prior to executing the automation’s action.

In the following example:

  1. The first State Trigger detects when either of the two lights is turned on. It sets the variable is_true to true if the TV is not playing (otherwise, false). The tree variable is set to on.

  2. The second State Trigger detects when the TV’s state changes to playing. It sets the variable is_true to true if the tree is on (otherwise, false). The tree variable is set to off.

  3. The third State Trigger detects when the TV’s state changes to off. You may need to change this value because not all media_players report off when they’re not playing (could be idle or even unavailable). It sets the variable is_true to true if the the tree is off (otherwise, false). The tree variable is set to on.

  • The automation’s condition simple confirms the value of is_true is true. If it’s false then the automation ends without executing its action.

  • The automation’s action contains one service call that either turns the switch on or off, depending on the value of the tree variable set by the trigger.

alias: example
trigger:
 - platform: state
   entity_id:
     - light.first
     - light.second
   from: 'off'
   to: 'on'
   variables:
     is_true: "{{ states('media_player.tv') != 'playing' }}"
     tree: 'on'
 - platform: state
   entity_id: media_player.tv
   to: 'playing'
   variables:
     is_true: "{{ is_state('switch.tree', 'on') }}"
     tree: 'off'
 - platform: state
   entity_id: media_player.tv
   to: 'off'
   variables:
     is_true: "{{ is_state('switch.tree', 'off') }}"
     tree: 'on'
condition:
  - condition: template
    value_template: "{{ is_true }}"
action:
 - service: 'switch.turn_{{ tree }}'
   target:
     entity_id: switch.tree