Create automation to turn off tv in kids room between the hours of 8pm and 6am

Hello,

I am looking to use HA to enforce tv off time for my children while we are sleeping. I want this automation to check every 5 minutes to see if their tv is on, and if it is on, send a signal to turn the tv off. I have an automation set that that work when manually activated but it dont see in the traces it waking up to check every 5 minutes to check.

THis is the yaml of my automation. Please tell me what I am doing wrong.

alias: Turn off girls TV - 8pm to 6am - weekday
description: ""
trigger:
  - platform: time_pattern
    minutes: /5
condition:
  - condition: time
    before: "06:00:00"
    after: "20:00:00"
  - condition: device
    device_id: 33f44c2a912979c0d0abc51065a6bde6
    domain: media_player
    entity_id: 6993da84820a3c842669a2ddbd1128f7
    type: is_on
action:
  - type: turn_off
    device_id: 33f44c2a912979c0d0abc51065a6bde6
    entity_id: 6ee76f9eb2877a40d032d261d1067756
    domain: remote
mode: single

This condition will never be true. Try splitting it up using an Or condition.

Why not just trigger when the TV is turned on?

Time pattern triggers are rarely the most efficient option.

Also read this: Why and how to avoid device_ids in automations and scripts

Actually it will. Time conditions can span midnight. It will be true from 8pm to 6am.

https://www.home-assistant.io/docs/scripts/conditions/#time-condition

1 Like

I’m concerned that if the TV is already on it won’t turn off during that window. My kids are often watching tv before the start of the window.

1 Like

Neat! When did that change? It never used to work for me and I had to split it into an or some years back when I needed something similar re:time…

1 Like

Then you would only need a single automation with a time trigger of 8 PM to turn the TV off, and then a second automation with a trigger of “TV turn on” and conditions of “after 8PM” “before 6 AM”

Someone even more creative than me might know how to combine them into a single automation, but I personally would rather have 200 easy-to-understand automations rather than 20 SUPER complicated ones that will take me an hour to understand when I have to make changes to them 2 years after I wrote them. LOL

1 Like

Exactly. Much simpler and reliable.

My only caution if you go this route is to make sure to have a good naming scheme for them. Since HA doesn’t have folders or any other type of organization system for automations, finding the one you want to modify can be a challenge if you don’t have a good naming convention. I made this mistake when I first started, and learned the hard way. Now, my automations are named like this:

Lights - Exterior - Floodlights
Lights - Exterior - Garage
Lights - Exterior - Uplighting
Lights - Interior - Kitchen
Lights - Interior - My Office
Notification - Doorbell - Front door
Notification - Doorbell - Garage door
Notification - Water Leak - Half Bath sink
Notification - Water Leak - Hot water tank
Notification - Water Leak - Kitchen sink
Notification - Water Leak - Master Bath sink
Notification - Water Leak - Prep sink

You get the idea. I try to group them up into what the notification does - controls lights, notifies me about something, takes action based on security camera, locks up when nobody is home, open the garage door when someone arrives home, etc.

Agree. I have 90% of my config generated by shell scripts. In other words, I’ve built a class / object system outside of HA to generate the config and that creates the naming conventions. With this I can add a new light control loop with multiple triggers, occupancy and luminance in about a minute. :love_you_gesture: and it’s based on code that already works. Some day I’ll package these tools into a public repo.

1 Like

That sounds cool! I’d love to take a look when/if that becomes publicly available.

It’s pretty simple:

alias: Turn off girls TV - 8pm to 6am - weekday
description: ""
trigger:
  - platform: state
    entity_id: 6993da84820a3c842669a2ddbd1128f7
    from: "off"
    to: "on"
  - platform: time
    at: "20:00:00"
condition:
  - condition: time
    before: "06:00:00"
    after: "20:00:00"
  - condition: state
    entity_id: 6993da84820a3c842669a2ddbd1128f7
    state: "on"
action:
  - service: media_player.turn_off
    target:
      entity_id: 6ee76f9eb2877a40d032d261d1067756
mode: single

No need to poll continuously. If it is on at 8pm it gets turned off. If someone attempts to turn it on between 8pm and 6am it gets tuned off.

If you look in Developer Tools → States you will be able to replace the stupid entity_id: 6ee76f9eb2877a40d032d261d1067756 with something more meaningful like entity_id: media_player.kids_tv or whatever it is called.

Won’t that have an issue with the “at 20:00” trigger because the condition specifies “after 20:00” though?

Nope. HA compares times down to the millisecond. I use a similar trigger and condition without issue.