Add state to existing switch

Hello,

Is there a way to add state to an existing broadlink switch or create a new switch containing state that calls the original switch?

I have a broadlink IR Blaster with which I’m controlling my TV and I would like to track whether it is on before sending as the command for my TV is a toggle command.

I don’t see any way to add the current state of the TV from the documentation that I see here:

I’ve done something similar before with a command line platform and would like to do something like this:

- platform: broadlink
    host: !secret ip-broadlink-ir_blaster
    switches:
      tv:
        friendly_name: "Samsung TV Power"
        command_on: !secret living_room-samsung_tv-power
        command_off: !secret living_room-samsung_tv-power
        # command_state: "wget --spider --server-response --tries=1 --timeout=5 http://192.168.1.5:8000 2>&1 | grep '200\ OK'"

Best Regards,
WeldFire

Having the same issue with my TV, i.e. the command for TV on and TV off is the same and basically comes down to a toggle, I realized had a Wemos D1 Mini lying around.

I set it up for access to my WiFi and plugged it into the USB port of my TV that only has power when the TV is actually switched on.

The Wemos shows up as ‘home’ within a few seconds of the TV being switched on but it takes a little longer for it to show up as not_home when the TV is switched off, so I read the TV stat off this info.

Still, this is by far more reliable than trying to map an input-boolean to the Broadlink that switches whenever I use it - but got out of sync easily when SWMBO used the ‘proper TV remote’ to turn the TV on and off.

There might be cheaper devices around to do that (the Wemos cost me $12 for two) but that’s what I had lying around.

1 Like

Hi Chairstacker,

Thank you for your response, you have a creative solution for detection presence!
Fortunately my TV connects to WiFi when it is on so I get that out of the box.

Did you ever figure out a way of smushing the state and switch together?

Could always just stick both the switch and the boolean state in a card together; however, I don’t see why it couldn’t be combined like it is for other switches.

Assuming you have a device_tracker set up fro your TV, does it only show up as ‘home’ when it’s actually turned on, or in stand-by also?
If there’s a distinction you have your state-tracker right away.

If there isn’t this disttinction, my only suggestion is measure the TV’s power consumption via e.g. a Sonoff S31 and get the state that way - way more expensive than a Wemos, though.

BTW:
I solve the toggle-only restriction with a condition in an automation, i.e. I only really fire the command_on when the TV’s presence state is not_home and vice versa.

I’m actually using a “command line sensor” to ping a webpage the TV makes when it is on and it seems to work pretty well.

I feel like I’m not following you when you are saying that you solved the toggle restriction with an automation. Can you elaborate on that a bit further?

I feel like worst case I’m looking for a UI solution to smush the sensor and switch together, but if automations could somehow fix this I’m all game!

Hi @WeldFire - I just tried to look it up to give you a real-life example with the automation and all, but I guess I must have replaced it a little while ago and forgot about it.

But here is what I have working now:

I have a WOL Switch for the Wemos - maybe you can set one up for your TV MAC and monitor the status of this ‘switch’:

- platform: wake_on_lan
  mac_address: "AA:BB:8CCE:12:34:56"
  name: TVESP
  host: 192.168.X.Y

If that works for you you’d be all set :+1:

For complete info, I also have a script to toggle the TV Power:

toggle_tv:
  sequence:
    - service: switch.broadlink_send_packet_192_168_7_26
      data:
        packet:
          - "JgCgAJaSEw4TDhMOEw"

And I added the switch in a glance card in Lovelace

      - type: glance
        title: TV Remote
        show_state: false
        show_name: false
        entities:
          - entity: switch.tvesp
            tap_action: call-service
            service: script.toggle_tv

As I mentioned above, it takes a little while for the switch status to change in the interface, but it works reliably.

I think I haven’t managed to get it to work in an entities card, though.

I’m a bit confused. This still doesn’t connect state directly to the switch correct?
Thank you so much for your help!

Figured it out!

switch:
    - platform: broadlink
      switches:
        tv:
          friendly_name: "Samsung TV Power"
          command_on: !secret living_room-samsung_tv-power
          command_off: !secret living_room-samsung_tv-power
    - platform: template
      switches:
        smarttv:
          value_template: "{{ states('sensor.tv_power') == 'on' }}"
          turn_on:
            service: switch.turn_on
            data:
              entity_id: switch.tv
          turn_off:
            service: switch.turn_off
            data:
              entity_id: switch.tv
sensor:
- platform: command_line
    name: TV Power
    scan_interval: 5
    command: "wget --spider --server-response --tries=1 --timeout=5 http://192.168.1.5:8000 2>&1 | grep '200\ OK' | wc -l"
    value_template: >
    {% if value == "1" %}
    on
    {% else %}
    off
    {% endif %}

Adding a template switch allowed me to do what I wanted!
There is a slight delay after changing it where it flips back before finally deciding on the next state update; however, I can live with this. Theoretically I can shorten the scan_interval; however, I’m not sure how much stress this would put on the system for little benefit.

Thank you again @chairstacker for your examples!

2 Likes