Turn lights off after bedtime as long as TV is off

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:
    • unavailable
    • unknown
    • “off”
  • condition: time
    after: “22:30:00”
    actions:
  • action: automation.trigger
    metadata: {}
    target:
    entity_id: automation.lights_off_at_bedtime_if_tv_off
    data:
    skip_condition: true
    mode: single

Usually if two things need to be checked you need both triggers and conditions, lets start with the conditions:

Conditions

You are basically saying that to proceed with the automation (turn off the lights) two things need to be true:

  • The time must be between 22:29 and 02:00 (pick your own end time).
    AND
  • The TV must be off.

Once you have defined the conditions (put a conditions section in your automation), then you can define the triggers.

Triggers

These can be much simpler because we know that the automation won’t happen unless the conditions (stated previously are also true).

  • The time is 22:30
  • The TV is turned off (State changes On->Off).

How it works

Except during the specific time window (22:29 - …) turning the TV off does nothing because the conditions will prevent the automation from running.

If in the window the condition will not prevent the automation from running if the TV is turned off.

Alternatively if the time is 22:30 the automation will only happen if the TV is already off.


PS Obligatory - Please format any code snippets you post correctly…

Simple Example:

triggers:
  - trigger: time
    at: "22:30:00"
  - trigger: state
    entity_id: media_player.kdl_50wf663
    to:
      - "off"
conditions:
  - condition: time
    after: "22:29:59"
  - condition: state
    entity_id: media_player.kdl_50wf663
    state:
      - "off"
actions:
  - action: light.turn_off
    target:
      entity_id: light.some_light
description: ""
mode: single
triggers:
  - trigger: time
    at: "22:30:00"
    id: time_trigger_2230
  - trigger: state
    entity_id:
      - media_player.tv
    to:
      - "off"
      - unknown
      - unavailable
    id: tv_trigger_off
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - time_trigger_2230
          - condition: state
            entity_id: media_player.tv
            state:
              - "off"
              - unknown
              - unavailable
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
      - conditions:
          - condition: trigger
            id:
              - tv_trigger_off
          - condition: time
            after: "22:30:00"
        sequence:
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - action: light.turn_off
            metadata: {}
            data: {}

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.