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
Under template switch, turn_on, edit the IP address for broadlink switch.broadlink_send_packet_192_168_178_81
Under template switch, turn_off, edit the IP address for broadlink switch.broadlink_send_packet_192_168_178_81
Change both the on / off IR codes to an IR codes that works for your TV
Change the TV’s IP address under binary_sensor … host: 192.168.178.28
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
This is what it looked like using the original power on / off IR button.
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.
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.
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
That component doesn’t give on off state of the tv.
Only way to know is.
Ping tv
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.