Smart toggle switch for device (using a delayed network ping to detect power on / off)

new%20tv%20power

I initially used a template switch using the ping from the TV to detect whether the TV was on or off, however the problem was the switch would bounce on and off as it takes a while for the TV to connect / disconnect from the network. This template switch uses a timer to prevent the TV power from bouncing on and off.

How this works

  • Television toggle sends a broadlink IR command to turn the TV on / off
  • After you toggle the tv power in HA it takes a while for the TV to connect / disconnect from the network, so ignore the tv ping for this first 9 seconds after turning the TV on / off.
  • the input boolean stores a temporary on / off palce holder while we ignore the TV ping for the first 9 seconds after toggling the TV power.

The advantage of a template switch is if you change the TV power outside of home assistant then the switch will update, you can also query the state of the template switch in your automations to find out if your TV is already on / off (this is useful if your TV doesn’t have a discrete on / off IR code).

Configuration.yaml

switch:
  - platform: template
    switches:
      tvpower:
        friendly_name: Television
        value_template: >-
          {% if is_state('timer.tv_delay', 'idle') %}
          {{ states("binary_sensor.tv_ping") }}
          {% else %}
          {{ states("input_boolean.tv_power_status") }}
          {% endif %}
        turn_on:
          - service: switch.broadlink_send_packet_192_168_178_81
            data:
              packet: 
                - "JgBGAJGVETgSOBE4ERQRFBITERQRFBI4ETgSOBAVERQSExITEhMROREUERQROBI4ERQQFRE4EhMSOBE4EhMSExI4ETgSExIADQUAAA==="
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.tv_power_status
          - service: timer.start
            entity_id: timer.tv_delay
        turn_off:
          - service: switch.broadlink_send_packet_192_168_178_81
            data:
              packet: 
                - "JgBGAJWTEzYTNhM3ExESEhISExITERM2EzcTNhMREhITEhMREhISEhMSExETNhM3ExESEhM2EzcTNhM2ExITERI3EjgTERIADQUAAA==="
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.tv_power_status
          - service: timer.start
            entity_id: timer.tv_delay
        icon_template: mdi:television-classic

timer: # It takes 8 seconds for my tv to connect / disconnect from the network
  tv_delay:
    duration: '00:00:09'

input_boolean: # This stores the temporary TV power state while we are waiting for the TV ping to update
  tv_power_status:
    name: TV Power Status

binary_sensor:
  - platform: ping
    host: 192.168.178.28
    name: TV Ping
    scan_interval: 2
    count: 2

You will need to edit the following

  1. Under template switch, turn_on, edit the IP address for broadlink switch.broadlink_send_packet_192_168_178_81
  2. Under template switch, turn_off, edit the IP address for broadlink switch.broadlink_send_packet_192_168_178_81
  3. Change both the on / off IR codes to an IR codes that works for your TV
  4. Change the TV’s IP address under binary_sensor … host: 192.168.178.28
  5. If you want you can reduce the duration: ‘00:00:09’. My TV takes 8 seconds to connect / disconnect, so I set this value to 9 seconds. Try reducing it to around 5 seconds and if the TV toggle bounces then you need to increase it until it doesn’t bounce.

When you have it working nicely you can hide sensor.tv_ping, input_boolean.tv_power_status and timer.tv_delay

6 Likes

This is what it looked like using the original power on / off IR button.

old%20tv%20power

This button is dumb, because it doesn’t know if the TV is on / off, it just guesses based on what button was last pressed on the home assistant webpage.

Here is the new button, it is smart, it knows whether the TV is on / off, it also looks better.

new%20tv%20power

How quickly does the state of the tv change as soon as you switch it on or off? I have a lag of at least 10 seconds.

Do you experience that?

The state changes immediately on the switch because of the script and doesnt bounce back because of the script. The ping sensor takes around 5s to become on. My tv seems pretty quick to establish wifi connection when it is turned on.

oh right well thanks for this anyway !!

this worked,
i cant turnoff or on using this ,
cn you please check and guide
thanks

Did you change the ip address of the broadlink and the ir code?

Yes I do changed it

Post your code and describe what happens.

Dear,
With this configuration can we turn off or on TV,
It does shows me current status by ping and the code provided.
Please guide

Yes. It uses broadlink. If you can’t turn on and off maybe your ir code is wrong.

binary_sensor:
    - platform: ping
       host: 192.168.11.37
       name: Samsungtv Ping
       scan_interval: 5
       count: 2

input_boolean:
  tv_power_status:
    name: TV Power Status
    initial: off

switch:

- platform: template
  switches:
    samsungpower:
      friendly_name: Samsung Hall
      value_template: "{{ states.input_boolean.tv_power_status.state == 'on' }}"
      turn_on:
        - service: switch.broadlink_send_packet_192_168_11_19
          data:
            packet: 
          - "JgBGAJKVETkRORA6ERQRFBEUERQRFBE5ETkQOhAVEBUQFREUEBUQOhEUERQRORE5EBURFBA6EBUQOhE5EBUQFRA6EDoRFBEADQUAAA=="
        - service: input_boolean.turn_on
          data:
            entity_id: input_boolean.tv_power_status
      turn_off:
        - service: switch.broadlink_send_packet_192_168_11_19
          data:
            packet: 
          - "JgBGAJOVEDoQOhA6DxYPFhAVEBUQFRA6ETkROREUERQRFBEUEBUQFREUERQRORE5EBUQFRE5ETkRORE5ERUQFRA6DzsPFhAADQUAAA=="
        - service: input_boolean.turn_off
          data:
            entity_id: input_boolean.tv_power_status
      icon_template: mdi:television-classic

automation:
- alias: 'Samsung TV Power Status to On'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: binary_sensor.samsungtv_ping
      from: 'off'
      to: 'on'
  action:
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.tv_power_status
    
- alias: 'Samsung TV Power Status to Off'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: binary_sensor.samsungtv_ping
      from: 'on'
      to: 'off'
  action:
    - service: input_boolean.turn_off
      data:
        entity_id: input_boolean.tv_power_status

customize:
input_boolean.tv_power_status:
  hidden: true
binary_sensor.samsungtv_ping:
  hidden: true

i did get it working now.
i have to include broadkink as platform to get it work

dear i have one more question
i am using broadkink.py provided by vasilis.
and i am using this as

 - platform: broadlink
   host: !secret broadlink_host
   mac: !secret broadlink_mac
   name: SamsungTv
   ircodes_ini: 'broadlink_media_codes/samsung.ini'
   power_consumption_entity: media_player.samsungtv

how to include your code to get current status here,
Please guide where and what to add,
thanks in advance

Ok it sounds like you figured out your first problem.

I don’t understand what your current problem is?

Tip: make it so the tv ping sensor is not hidden. (
binary_sensor.samsungtv_ping ) Is the tv ping sensor working?

Dear,
thanks for guiding, i have solved my problem, ping is working great with your code,
i want to integrate that code with my media components where i can get current status of my device.
Please guide

Which media component?
How do you get status of that component?

this is my media config
i have used this custom components provided by Vasilis
https://github.com/vpnmaster/homeassistant-custom-components

here is my config

- platform: broadlink
  host: !secret broadlink_host
  mac: !secret broadlink_mac
  name: SamsungTv
  ircodes_ini: 'broadlink_media_codes/samsung.ini'
  power_consumption_entity: media_player.samsungtv

i have used ping command but it keeps on changing its status
so i want your above mentioned code to get status here in media component

That component doesn’t give on off state of the tv.

Only way to know is.

  1. Ping tv
  2. If tv turned on via toggle switch in hassio then assume tv is on (until user turns off tv in hassio or the tv ping sensor changes state from on to off).

If ping command keeps changing status without the tv changing status then my code won’t work for you.

Your video works perfectly,
But when I use ping in my media player android TV box config, it keeps on changing

And I want to integrate your code with media player.
Please guide

You need to set a fixed ip address for the media player.

I’ve updated the code in the first post, it’s much cleaner and works better.