Automation to Turn off Plug when TV is off

Newbie here and just trying to get some automation set-up.

Assume I have entries “TV Plug” and “LG TV”, what I want to do is:

After 23:00 if “LG TV” is OFF Turn off “TV Plug” if “LG TV” is ON check again 30 minutes later and keep repeating until “LG TV” is OFF, then turn off “TV Plug”

Hope this makes sense.

Any pointers how I would code the automation?

Thanks

Why not turn it around and have the TV turning off as the trigger? Then condition - is it after 23:00? Action - turn off the plug. No need to keep checking.

1 Like

But then if the TV is turned off at 22:00, the plug will remain switched on. Problem with that is when an over-the-air update is done to my satellite box in the early hours of the morning, my TV automatically turns on :).

I was going to keep it simple and just turn off the plug at 23:00. Problem is if someone is watching the TV I won’t be very popular.

Not if you add an 11pm time trigger as well.

1 Like

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.

  1. 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.

1 Like

Excellent - thanks Pete; this will really help me understand things. Now off to try and work it out for my set-up.

1 Like