Best way to add state to ir/rf devices

Hi,

I have a Xiaomi Miio IR Remote and am getting started to make all my IR devices work with it.
I am now thinking about how to best add them to my current Home Assistant setup.

Some remotes/devices work with toggle power commands and others use power on and power off.

I would like home assistant to remember the state and make them work with automations/scripts.
So when I turn on a device and later run an automation/script that is supposed to turn that device on as well it does not send a command again (especially when it is a toogle power command)

Has anyone experience with that or are there templates for that?

1 Like

No ideas? can’t be the only one with that idea/problem, but I did not find anything that helped.

I just added input_boolean values for any devices that I was controlling over IR, and then you can set up a template switch that only send the IR command if the state isn’t already in the one want. I had to do the same sort of thing for my garage door, which although not being IR, doesn’t have a discrete “open” and “close”, but rather just “start moving the door”.

In this case, the template switch’s turn_on and turn_off actions will first check to see if the input_boolean is in the opposite state, and then, toggle the input_boolean and send the IR command to control your actual device.

input_boolean:
  infrared_device:
    name: My infrared device

switch:
  - platform: template
    switches:
      infrared_device:
        value_template: '{{ is_state("input_boolean.infrared_device", "on") }}'
        turn_on:
          - condition: state
            entity_id: input_boolean.infrared_device
            state: 'off'
          - service: input_boolean.toggle
            data:
              entity_id: input_boolean.infrared_device
          - service: send infrared command
        turn_off:
          - condition: state
            entity_id: input_boolean.infrared_device
            state: 'on'
          - service: input_boolean.toggle
            data:
              entity_id: input_boolean.infrared_device
          - service: send infrared command

EDIT: Without discrete on/off IR codes, it can be really easy to get your device out of sync with home assistant though. In fact, if you do have discrete on/off codes, I wouldn’t even include the condition; it wouldn’t be necessary, and could even end up being annoying.

3 Likes

Yeah I see the problem with “toggle power” IR codes vs discrete “on/off” ones.
I’m just trying to figure out what the best way would be to use these IR-Devices in Automations and scenes.
Maybe I have to help HA out by turning something on/off because the state is not matching anymore in 1/10 cases.
But I think that would already be an improvement compared to doing it manually every time.

I have a Broadlink RM Pro. It controls lights, TV, garage door.
I’m thinking a lot about the states beacuse sometimes the Broadlink send the command (the state changed) but the IR/RF signal disappears in the ether.

2 Likes

The fix for matching states or getting out of sync (i.e. when the IR code never made it to the device or someone changed it manually) is to use template switches and use a sensor to report the real state.

For networked devices, it could be a ping to the device. For dumb AC I have a door sensor, if the louver is open then the AC is running. For dumb devices then a smart plug that monitors the power load. And so on. Gotta get creative :slight_smile:

As for automations, you have to put a condition to read the actual state before running the command. In example something like this to give you an idea.

draft for an automation to turn off an AC after you’re away for 10 minutes

Trigger
trigger: state
entity: yourphone
to: “not_home”
for: 10 minutes
Condition: state
entity: yourAC
state: on
action
service: switch.turn_off
entity: yourAC

Otherwise it will send the command again (for all these devices which use the same IR or RF code to turn on/off, basically a toggle).

3 Likes

Yes this is one of my solution too :slight_smile:
But sometimes you can’t install this plus sensors.

This is a great idea I have not thought about before.
It might now work for every device or case but maybe it will help some people searching for a solution here in the forum.

So here’s an example and also a half-baked solution I have for my AC when the IR codes disappears in the “ether”.

Template switch which uses Broadlink IR codes for turning on or off, and the power state is based on a sensor value (in this case Door Sensor):

      livingroomairconditioner:
        value_template: "{{ is_state('binary_sensor.door_window_sensor_158d000288e2ed', 'on') }}"
        turn_on:
          - service: switch.broadlink_send_packet_192_168_0_102
            data:
              packet: 
                - "JgCWAAABQgABQQABQQABQZdSCw0MHwohCw0LIAsOCg0NCwsNDh0LDQsODB4LDA8LCiELDQ0LCwwPCwoOCg4KDgoOCw0KDg0LCyALDQsNDQsMDQwMCg4KDgoOCiALDQ4LCg4LDQoODAwLDQ0eCw4LDQoOCiALDgogCw4KDg0eCw0LDQsKEB4LIAsgCyALDgoOCg4KAAKcmAANBQAA="
        turn_off:
          - service: switch.broadlink_send_packet_192_168_0_102
            data:
              packet: 
                - "JgCWAAABQgABQQABQQABQZdSCw0MHwohCw0LIAsOCg0NCwsNDh0LDQsODB4LDA8LCiELDQ0LCwwPCwoOCg4KDgoOCw0KDg0LCyALDQsNDQsMDQwMCg4KDgoOCiALDQ4LCg4LDQoODAwLDQ0eCw4LDQoOCiALDgogCw4KDg0eCw0LDQsKEB4LIAsgCyALDgoOCg4KAAKcmAANBQAA="

This is for my automation checking the power state before triggering (in case of very dumb devices which use the same code to turn on/off):

  - alias: LRACOnPresence
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d0001e0acf6 
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.livingroomairconditioner
        state: 'off'
    action:
      - service: switch.turn_on
        entity_id: switch.livingroomairconditioner
        
  - alias: LRACOffAway
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d0001e0acf6 
        from: 'on'
        to: 'off'
        for:
         minutes: 45
    action:
      - service: script.aclroff
      - delay: '00:00:15'
      - service: script.aclroff

And the scripts here. You might ask why do I do it with a script for turning it off? Why call the script twice?
I could do it for turning it on as well, but I care more about the AC turning itself off when I go away so it doesn’t stay on the whole day.

So I wrote a script that checks for the power state and sends the command if the power state isn’t the desired one. Then I run it twice and put a delay in between, so the AC has enough time to open or close the louver.

Logic wise
Trigger
Xiaomi doesn’t detect motion for 45 minutes

  • is it turned on? -> if yes, then turn it off -> wait 15 seconds -> is it still turned on? -> if yes, then the IR code didn’t make so it will be sent again.

  • is it turned on? -> if no, then it will just skip it -> wait 15 seconds -> is it turned on? -> if no, then just skip it again

  aclroff: 
        sequence:
          - condition: state
            entity_id: switch.livingroomairconditioner
            state: 'on'
          - service: switch.turn_off
            entity_id: switch.livingroomairconditioner
1 Like

I have a similar case and would like to hear some ideas:

I have a TV connected to an AC power consumption sensor, so i know when the TV is or on off by lookin gat the power consumption. I also have a broadlink IR blaster.

So i want to turn the TV on and off with the IR blaster and want to set the state of a switch accordingly based on the power consumption, so if i turn on/off the TV manually with the TV remote, then the switch in HA will reflect the status. The IR code for the TV on/off on the broadlink is the same for on or off, so it is a toggle.

How can i set the switch status based on the power consumption wihtout entering into an endless loop of on and offs?

Can’t you just use a template switch? The value template should point to the power consumption sensor and the on and off templates pointing to the IR switch or script.

For RF only, couldn’t you have a Sonoff RF Bridge to record codes being sent directly to dumb devices?

For example, I have dumb RF outlets, the Broadlink Pro can turn them on and off, but so can my wife with their included remote control (so things get out of sync).

My plan was to get a sonoff rf bridge, train it the on/off codes per outlet, and when my wife uses the remote (not HA) to control the outlets the code is sent by the remote, it is received by the outlet but also HA and then HA will update the status as required, this would stop them getting out of sync.

Not sure if this would work as I dont have a Sonoff RF bridge, but if someone can confirm the proof of concept for me that would be great!

1 Like

i used a template switch but it takes a while to update. So i was wondering if there maybe another solution that maybe faster, i have like a 10 second delay at the moment, using a template switch and pointing the switch state to the power sensor

Certainly, you can write an automation that triggers the update service of the template switch when the power sensor state changes. However, I have not observed such delays with my template switches as far as I am aware of. Did you add the entity_id of your sensor to the switch besides in the value template? As far as I understand the docs, this may help: “entity_id: A list of entity IDs so the switch only reacts to state changes of these entities. This can be used if the automatic analysis fails to find all relevant entities.”

can you elaborate on " Did you add the entity_id of your sensor to the switch besides in the value template" ?

Check the docs of the template switch. There it is stated:
“A list of entity IDs so the switch only reacts to state changes of these entities. This can be used if the automatic analysis fails to find all relevant entities.”

This may be the cause of your problems.

1 Like

hey @Coedy

i have the same problem, have you succeed with Sonoff RF Bridge please?