Trying to get my first automation happening. I want my light to turn on when the TV is turned off… but for some reason it isn’t working and I’m struggling to see why. Any ideas? Syntax seems right to me?
#Automations
automation:
alias: Turn on light when TV turned off
# name: hallway_light_on
trigger:
- platform: state
entity_id: media_player.sony_bravia_tv
hide_entity: False
to: 'off'
- platform: sun
after: sunset
action:
service: light.turn_on
entity_id: light.main_light
hide_entity: False
for:
minutes: 1
#Automations
automation:
alias: Turn on light when TV turned off
# name: hallway_light_on
trigger:
- platform: state
entity_id: media_player.sony_bravia_tv
to: 'off'
- platform: sun
after: sunset
action:
service: light.turn_on
entity_id: light.main_light
Alright, I didnt have time to check it though much yesterday, sorry.
In short, it works like this: (any trigger) + (all conditions) → (actions)
Longer version: An automation has three parts, triggers, conditions and actions.
Triggers
If any of the parts in the trigger section becomes true, the automation will move on to check if there is a condition section, if not then it moves on to the action section.
Conditions
Per default, all of the condition parts must be true for the action to run. You can change that so that only one of the conditions must be true by using condition: or
Actions
The action(s) to be preformed.
In your automation it looks like you want to move the sunset part to a condition.
Also: If you only want to turn on the light for one minute, you can do it like this:
# Pseudo code
trigger:
- tv changes state to off
conditions:
- after sunset
- main_light is off
actions:
- turn on main light
- delay for one minute
- turn main light off again
This way the main light isn’t turned off if it was on to start with.
So I don’t think I need the ‘main light is off’ bit… but I’ve tried the bare bones and it’s still not working, furstratingly. Haven’t added the ‘for one minute’ part yet either because I wanna keep it simple.
#Automations
automation:
- alias: Turn on light when TV turned off
# name: hallway_light_on
trigger:
- platform: state
entity_id: media_player.sony_bravia_tv
to: 'off'
conditions:
- condition: state
entity_id: sun.sun
state: below_horizon
action:
service: light.turn_on
entity_id: light.main_light
That was it! Thanks. For posterity, here’s my final value… I’ll test it tonight! If I don’t post, assume it works
#Automations
automation:
- alias: Turn on hall light when TV turned off after dark
# name: hallway_light_on
trigger:
- platform: state
entity_id: media_player.sony_bravia_tv
to: 'off'
condition:
- condition: state
entity_id: sun.sun
state: "below_horizon"
action:
service: light.turn_on
entity_id: light.main_light