I just did something like this. To make it manageable I inverted the problem and used a demand model.
You want the TV to turn off if there is no demand. There are two demands on the TV
- it’s between 10 Am and 11:00 PM it’s in demand
- if someone is watching it.
So when the demand goes away the switch gets turned off and when demand appears the switch gets turned on.
Here’s a working example for my Sonos media player.
- Create a binary sensor template for the time range that is true when it’s in demand.
input_number:
av_tr_lr_sonos_start:
min: 0
max: 24
unit_of_measurement: h
mode: box
av_tr_lr_sonos_end:
min: 0
max: 24
unit_of_measurement: h
mode: box
binary_sensor:
- platform: template
sensors:
av_tr_lr_sonos_demand:
value_template: >-
{% set hour = now().hour|int(0) %}
{% set start_hour = states("input_number.av_tr_lr_sonos_start")|int(0) %}
{% set end_hour = states("input_number.av_tr_lr_sonos_end")|int(0) %}
{% if end_hour > start_hour %}
{{ start_hour <= hour and end_hour >= hour }}
{% else %}
{{ start_hour <= hour or hour <= end_hour }}
{% endif %}
recorder:
include:
entities:
- input_number.av_tr_lr_sonos_start
- input_number.av_tr_lr_sonos_end
- binary_sensor.av_tr_lr_sonos_demand
Next step is to create a binary sensor that reports whether the TV is on. I’m assuming you already have that.
Then create a master binary sensor template indicating if the TV is in demand. This includes the time demand sensor above and for my example if any of my Sonos are replaying. For you just change it to include your TV sensor.
I add a 5:00 delay, so that if I inadvertently stop playing music it doesn’t immediately cut power.
binary_sensor:
- platform: template
sensors:
av_demand_lr_sonos:
value_template: "{{ is_state('binary_sensor.av_tr_lr_sonos_demand','on') or is_state('binary_sensor.av_sonos_living_room_playing','on') or is_state('binary_sensor.av_sonos_kitchen_playing','on') or is_state('binary_sensor.av_sonos_master_bedroom_playing','on') or is_state('binary_sensor.av_sonos_garage_playing','on') }}"
delay_off: "00:05:00"
recorder:
include:
entities:
- binary_sensor.av_demand_lr_sonos
Then the automation that turns it on / off based on the demand. This has a couple more conditions like if I am on vacation and away it won’t turn on. And I have a input_boolean that allow me to turn all AV automation off.
- id: lr_sonos_demand_on_off on
alias: lr_sonos_demand_on_off on
description: ""
trigger:
- platform: state
entity_id: binary_sensor.av_demand_lr_sonos
to: "on"
from: "off"
condition:
condition: and
conditions:
- "{{ is_state('binary_sensor.away_mode', 'off') }}"
- "{{ is_state('input_boolean.av_control', 'on') }}"
- "{{ is_state('switch.tv_powerstrip_sonos','off') }}"
action:
- service: script.rs_tv_powerstrip_sonos_switch_turn_on
mode: single
- id: lr_sonos_demand_on_off off
alias: lr_sonos_demand_on_off off
trigger:
- platform: state
entity_id: binary_sensor.av_demand_lr_sonos
to: "off"
from: "on"
condition:
condition: and
conditions:
- "{{ is_state('binary_sensor.away_mode', 'off') }}"
- "{{ is_state('input_boolean.av_control', 'on') }}"
- "{{ is_state('switch.tv_powerstrip_sonos','on') }}"
action:
- service: script.rs_tv_powerstrip_sonos_switch_turn_off
mode: single
What I like about the demand model is it’s easy to add new demands. For example my AV receiver turn on if its has demand from the Roku, the TV or the LR Sonos.