Broadlink RM Switch - how to send multiple IR packet & keep on/off state

I doubt this is the best way but this is how I did it. I have a dumb TV. So to make sure my tv turn off is working every time I have it turn the tv off then wait 20 seconds and then press my sleep button twice just in case the tv was already off. This makes my tv turn off in 5 minutes with a sleep timer. A little crude but it works. This probably isnt that best way just the way I currently have it.

# tv on
- service: switch.turn_on

# tv off script
- service: switch.turn_off
- delay:
    seconds: 20
- service: switch.turn_on
    entity_id: switch.sleep
- delay:
    seconds: 1
- service: switch.turn_off
    entity_id: switch.sleep

this is the switches

# broudlink ir

- platform: broadlink
  mac: 'my mac address'
  timeout: 115
  friendly_name: "TV"
  friendly_name: "Sleep"

I have them as a light template

- platform: template
  friendly_name: "tv"
    service: script.turn_tv_on
    service: script.turn_tv_off
    service: script.turn_tv_off
1 Like

Thanks a lot for the input.

It’s good to know that we can define delays within the sequence which means sending multiple IR packet in one single command is achievable.

But I found that I don’t quite understand the system after reading your code. Previously I assumed the system will keep the last switch state (On or Off) after executing the command_on or command_off, but it looks like it is not the case.

Probably I used a wrong example in my original question. In fact, I hope that the system would still keep the switch state, after executing a sequence of IR command. That means I need the switch will change from OFF to ON state by sending 3 to 4 numbers of IR codes to an appliance under the command_on.

Unfortunately the ir blasts won’t track last state, which is why on a lot of commands like like Power or Mute I just created a scene button instead of having a switch that may be on the wrong state. How benjimatt did it is your best option, although I’d test the script with or without the delay between switches, as some TV’s don’t need that pause between and on longer scripts multiple second delays can feel like forever.

One other thought is that if you have multiple states on a device and don’t want to take up too much space on your page, an input select to launch different scripts also works pretty well and would to some degree keep your state (HDMI-2)

I understand that the IR Blaster won’t keep or feedback any state. I just thought Home Assistant will keep it as it is an emulation of a switch, even thought we have to accept that the state can be wrong once the user use the original IR remote or physical button on the appliance.

That assumption is because I have experience on setting up homebridge for Homekit, which can keep the state and have the option to have the system to set the state back to off every time the command is executed (which is useful when simulate the volume and channel button, while keeping the on/off state for the power key

Now I just changed the platform to home assistant + google home to replace the homekit, so facing the problem. If this is what HA is doing, that means there seems no solution…


Dear all,

I’m very sorry that I didn’t realize that the original question has been discussed in another post on the topic of change channels on TV. So I modified the subject a little bit by adding the second part of my question which is about keeping the on/off state for a power switch.

Many thanks!


I honestly think there is a solution for you. I havent done it just yet but solely because I literally just purchased a new TV. You need a smart switch possibly zwave that shows energy usage. Then you can set some of your scripts based on the state of how much energy is being used.

This way you can turn a dumb tv to a smart one. So In theory you could keep the state by saying something like "if energy used is greater than X amount of watts to assume the state of the tv is on. If it is less than Y amount of watts assume the TV is off.

There are a few of these smart switches out there with various price ranges

Thanks for the idea which looks great!

In fact, I have another use case for a fan which needs multiple codes to turn it off. I just completed the HA setup and found that it does keep the on/off status for a switch platform. So the only problem is how to send multiple code for a command_off under a switch.

However, I’m encountering another big problem now which makes my whole idea not doable. I plan to use the Emulated Hue module to work with Google home, but there is problem recently in Google Home app that it cannot add the hue Hub in a local network anymore (need to go to the Philip Hue website now). So even I can solve the multiple code issue now I can’t use Google Home to perform the control…

Anyway thanks again for the help!

you just need to search, i just added a google home again a few days ago any havent had any issues. i think you ned to make emulated hue point to port 80.

this is my emulated hue

  type: google_home
  listen_port: 80
  expose_by_default: true
    - light
    - media_player
emulated_hue a:
  type: alexa
  expose_by_default: true
    - media_player
    - light

Yes, I did spend some efforts to use port 80 and finally I completed the configuration and it should be OK as I have verified it with the http://:80/description.xml & http://:80/api/pi/lights.

Did you add the hue in your system before? People are saying that it is just changed in May and we can’t add new hue with the latest Home app anymore. Or you added it by the Google Home Speaker directly? I just wanted to try it on the Android Phone Home app before I go to buy the Google Home speaker.

This is the post I found with other people having the same problem.

I had google home at the very beginning but have completely redone my VM more than once after I got rid of it. A few new features came out such as shortcuts and it made me go back. This is why I buy from walmart 90 day no BS return policy lol.

Dont worry I have the latest home app and have removed and re-added hue multiple times.

It sounds great. Then I need to check again what’s wrong in my settings…

Regarding Emulated Hue I don’t see any error or warning message in the HA log, and as I said I got both description.xml and api/pi/lights working. So I can’t figure out how to find out the root cause.

I tried Google Assistant on my Android phone to add the Philps Hue device manually through the Home Control tab, but it directed me to the website asking for sign-in. That’s why I thought the problem those people mentioned is valid…

Wait do you already have the Google Home?? Or are you trying to use google assistant? If you are trying to use google assistant to add emulated hue that isnt going to happen. You pretty much have to have the actual google home device to pick up the emulated hue. I tried lots of times before I got the home because I am cheap so thats the only reason I know lol.

Oh yeah, it looks like I am repeating what you had been doing before, i.e. trying to use google assistant before I pick up an actual Google Home lol!

Thanks a lot for the sharing!

Yeah if I didnt need the google home for google assistant to work I probably would have a good home lol. Id say I use my google assistant on my phone much more. I hold the home button and tell it what to do. I think I just hate say “ok google” or “hey google”

I have one more question:

Is it normal to have the following message or there is something wrong in my configuration even before adding the device into a google home:

INFO:homeassistant.components.light:Setting up light.hue
INFO:homeassistant.components.light.hue:Emulated hue found, will not add

do you have a google home or an echo? If not I wouldnt expect anything from it

OK I will try it again when my Google Home arrived.


Easiest way to use Assistant on your phone is to use IFTTT, easy peasy to set up and costs nothing. If you want to shout at your lounge to make things happen, get an Alexa instead as emulated_hue and the like just works without issue, lot cheaper too :slight_smile:

That’s what I do.

Thanks for the advice. I will try the IFTTT then.

I noticed some of you were sort of asking how to have a smarter on / off switch for your TV.

Here is some code that will add a smart toggle switch for your TV that knows whether the TV is on / off.

Requirements are:

  1. TV is connected to the network (via WiFi or ethernet cable) so that you can ping it.
  2. Your set your TV to a fixed IP address on your network

Optional requirement:

  1. Discrete on / off IR codes for your TV are preferable, but not essential.

Here is the post with instructions:

1 Like