Ok. I’m trying to think of a more elegant way to achieve this, but can’t. This script kind of acts as a listener on a 10 minute interval, switching lamps off if it’s after 22.30 and the TV (the media player entity is not switched on).
I want to switch the lights off after 22.30, but if the TV is on check every 10 minutes thereafter, so the lights automatically switch off once the TV is off
Here’s the YAML:
alias: Bedtime - can we turn lights off?
description: “”
triggers:
trigger: time_pattern
minutes: /10
conditions:
condition: state
entity_id: media_player.kdl_50wf663
state:
The following example turns off the light when either of these two occurs:
At 22:30 and the TV is off.
When the TV is turned off after 22:30.
alias: example
description: "Turn off light after 22:30 and TV is off"
triggers:
- trigger: time
at: "22:30:00"
variables:
is_true: "{{ states('media_player.tv') in ['off', 'unavailable', 'unknown'] }}"
- trigger: state
entity_id: media_player.tv
to:
- 'off'
- unknown
- unavailable
variables:
is_true: "{{ now() > today_at('22:30') }}"
conditions:
- condition: template
value_template: "{{ is_true }}"
actions:
- if: "{{ trigger.platform == 'state' }}"
then:
- delay:
minutes: 1
- action: light.turn_off
target:
entity_id: light.your_light
mode: single
NOTES
Replace light.your_light with the entity_id of your actual light.
As a courtesy to whoever turns off the TV after 22:30, the automation waits 1 minute before turning off the light (to give them time to exit the room).
Home Assistant is event-based so there’s no need to poll the TV’s state every 10 minutes. This example detects when the TV is turned off (or its state changes to unknown or unavailable then checks if the current time is past 22:30.
My wife acceptance factor was very low surrounding the “lights out” automation at night. No time-based or presence-based triggers seemed to be foolproof. I finally added a button by the bedroom door that triggers a scene as the last person goes to bed and that has solved it for me. I know, not what you asked but my experience anyway.
I have generally found that the HAF (Human** Acceptance Factor) is generally far more lenient for turning something on vs turning something off - since having a light on unnecessarily is far less annoying than finding oneself in the dark - I realize there are some exceptions such as a light coming on whilst someone is trying to sleep.
** - Human - because I find it annoying too, when an automation screws up.