Turn off TV between certain time when no motion detected

Hi, I’m trying to create an automation that will run between 12:30am and 3:30am to turn off the TV if it’s already on or has been turned on during that time and if motion has not been detected for the past 30 mins. It will then send an announcement that the TV will turn off and if motion is detected within 15 mins, action will be canceled. I want it to continue to check for these conditions until the end time in case some turns the TV back on again.

Currently I have the code below. I tried to have it trigger between a certain time but can’t seem to figure it out so I’m having it check every half an hour.

Appreciate any tips. Thank you.


alias: Turn off TV when Asleep
description: ""
trigger:
  - platform: time
    at:
      - "00:30:00"
      - "01:00:00"
      - "01:30:00"
      - "02:00:00"
      - "02:30:00"
      - "03:00:00"
      - "03:30:00"
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: media_player.samsung_cu7000_65
        state: "on"
      - condition: state
        entity_id: binary_sensor.living_room_pir_occupancy
        state: "off"
        for:
          minutes: 30
action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.google_nest_mini
    data:
      volume_level: 0.4
  - service: tts.speak
    metadata: {}
    data:
      cache: true
      media_player_entity_id: media_player.google_nest_mini
      message: TV will turn off.
    target:
      entity_id: tts.google_en_com
  - condition: state
    entity_id: binary_sensor.living_room_pir_occupancy
    state: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 15
  - service: media_player.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: media_player.samsung_cu7000_65

I’m fairly new myself, but I don’t think that will ever work, if it were me, I would trigger of the motion sensor being no movement and then check to see if the time condition. Does the TV need to be checked to be on to turn it off, just turn it off (unless you can only toggle the power state)

I would consider something like this as an idea, please dont take this as face value, just to give you an work flow to get to your solution you need.

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_sensor_motion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 30
      seconds: 0
condition:
  - condition: time
    after: "00:30:00"
    before: "03:30:00"

I think the time matters, so be aware that the order is correct. Hope it starts you off, unless someone might come along with some more to add :slight_smile:

thanks for the help. I hadn’t thought of approaching it this way.

welcome to both of you…

@Roxy, you’re close. but i think it’d be better with a few tweaks.

  • personally i’d remove the from: "on" because if it goes from unavailable to off, i’d still want to trigger. if it goes from anything to off for 30 minutes, i’d want to trigger.
  • in any automation where you want to restrict the timeframe, you almost certainly also then need to add the start of the timeframe as a trigger. remember that triggers happen right when the event happens… so if motion stopped at 23:59, then at 00:29, it would try to trigger. the condition would block it. then it will not trigger at 00:30. ha does not keep re-trying unless the trigger happens again.
  • so below i added 00:30 as a trigger. but, that means it will try to trigger every 00:30, so that means i need to add the motion state as a condition.

@Xenfinityx try this:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_sensor_motion
    to: "off"
    for:
      minutes: 30
  - platform: time
    at: "00:30:00"
condition:
  - condition: time
    after: "00:30:00"
    before: "03:30:00"
  - condition: state
    entity_id: sensor.motion_sensor_motion
    for:
      minutes: 30
    state: "off"
action:
  - service: media_player.turn_off
    target:
      entity_id: media_player.your_tv

At 12:30, start a 30 minute timer.
When motion is detected, restart the timer.
If timer finished, turn off the tv…

description: ""
mode: single
trigger:
  - platform: time
    at: "00:30:00"
    id: Start Timer
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.test_timer
    id: Power Off
  - type: motion
    platform: device
    device_id: 027442816a2fcfca695ae32cef26fa84
    entity_id: 8ed57d5e6d00c9539f37d23510ef3b19
    domain: binary_sensor
    id: Start Timer
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Start Timer
        sequence:
          - service: timer.start
            target:
              entity_id: timer.test_timer_2
            data:
              duration: "1800"
      - conditions:
          - condition: trigger
            id:
              - Power Off
        sequence:
          - service: media_player.turn_off
            target:
              entity_id: media_player.shield_2
            data: {}

@Rich37804 take a look at the example i posted.
there are a couple issues with what you have.

  • it is a touch more complicated, and requires a separate timer helper.
  • theoretically, yours will turn the tv off if motion detection is “on” for 30 minutes straight. practically speaking, unless you’re dancing in your tv room, this probably won’t happen, but it is a logical bug.
  • typo: you are starting timer.test_timer_2, but checking timer.test_timer:
  • please avoid using device_id uuid’s when not needed: