How to use rflink (433) MHz remote to control Sonoff (MQTT) switch?

I’m trying to gradually replace rflink switches (newkaku…) by sonoff wifi switches (that already work with built in mqtt broker).

What I would like is to use the current rflink remote control to control the sonoff, so in essence a newkaku switch command (e.g. newkaku_014…be_2) should set or clear a switch in the mqtt platform.

Maybe I needed a script to accomplish this, but it would be very cool if this also could be done purely from the configuration file.

(I’ve already managed (with help of the forum :grin: ) to map two newkaku switches to the same lamp, using an “aliases:” line in the configuration file).

Any ideas ?

Do you already have a receiver for the remote?

I have a cheap USB tv receiver compatible with RTL-SDR, the Hassio add on RTL2MQTT to get the commands and the node-red add on for the automations. Can easily give any command to Ha with any button of the remote.

hi Namadori,

thanks for your reaction.

I do have a set of compatible remote controls and 433MHz switches, that work together and can be controlled by Home Assistant.

Disadvantage of the switches is that they don’t have a manual button (for those home mates that not always find the remote), so I want to replace the switches with (one by one) with Sonoff s20 types.
I’ve already integrated one of these into Home Assistant (flashing Espurna into the switch and connecting via mqtt) and that works,
but what I would like is to use the 433MHz remote to control the sonoff (by triggering an mqtt event when the 433MHz remote is used).

I’ve never used node-red yet, I hoped it would be possible to map (or kind of duplicate) the 433MHz switch (newkaku_xxx in configuration.yaml) to the mqtt switch.

I’m still looking into that direction and probably move to a python script if this fails.

I used to use an RM Pro to listen for 433MHz and interface with MQTT, which is sort of similar to what you are doing.

Old post here

I see a missing link… you need a 433MHz receiver to get the remote control into HA, and then to the Sonoffs.
A bit of a hack, I used the TV receiver as described, but I think there are better suited products from our reputable chinese sources :grin:

@PianSom, if I understood your post and remember how RM Pro works, you are using it to transmit radio messages, to control 433MHz plug from HA. What @gerk59 is asking is to receive commands from the 433MHz remote and then control the Sonoffs.

Once you have the button push from the remote, simple scripts can let you control everything with it.

@namadori : the missing link is not on the picture, but I do have a rflink 433 MHz receiver, and the combination works flawlessly with HA.

rflink-enclosure

Oh yes, @namadori you are quite right. My bad!

Finally managed to accomplish this :grinning:

using a custom component (with redundant logging)
Based on https://www.youtube.com/watch?v=Cfasc9EgbMU (around 17:30)

in configuration.yaml:

same_automation:
  switch1: switch.lamp_433
  switch2: switch.lamp_sonoff

In custom_components folder, created file same_automation.py with content:

from homeassistant.helpers.event import (track_state_change)
from homeassistant.components import is_on, toggle
import logging

DOMAIN = 'same_automation'

# Shortcut for the logger
_LOGGER = logging.getLogger(__name__)

def setup(hass, config):
    _LOGGER.info("Hi from automation")
    entity_1 = config[DOMAIN]['switch1']
    entity_2 = config[DOMAIN]['switch2']

    def state_changed(entity_id, old_state, new_state):
        if entity_id == entity_1:
            other = entity_2
        else:
            other = entity_1

        if is_on(hass, entity_id) != is_on(hass, other):
            _LOGGER.info("Toggle !")
            toggle(hass, other)

    # ensure current state is opposite
    if is_on(hass, entity_1) != is_on(hass, entity_2):
        toggle(hass, entity_2)

    track_state_change(hass, [entity_1, entity_2], state_changed)

    return True

Whenever I use the lamp_433, the lamp_sonoff follows the commands

Thanks to everyone for their responses.

After updating to recent version (0.80), python custom component no longer works, API has had some breaking changes.

Apparently the import
from homeassistant.components import is_on, toggle
no longer is valid.

How can I check and change the state of switches in hass ?

Just setting hass.states.set(entity_id, new_state) only updates the state, but does not toggle the switch with given entity_id

Found a solution:

  • in the event handler that is registered with track_state_change(hass, switch_id, handler),
    the entity of the changed switch is passed as parameter

  • in that handler, once you have the dependent_id (say 'the_switch'), the following works:
    (use turn_off for turning off):

    hass.services.call('homeassistant', 'turn_on', {'entity_id': 'the_switch'})