Easier way to define mqtt rflink switch?

I have a rflink connected via MQTT. Now I try to define the switches.

This is what I have now :

binary_sensor.yaml:

  - platform: mqtt
    name: sensorTVboven
    state_topic: 'rflink/NewKaku-03a0b800'
    value_template: >- 
      {% if value_json.SWITCH == "2" and value_json.CMD == "ON" %}   
        {{'ON'}}
      {% elif value_json.SWITCH == "2" and value_json.CMD == "OFF" %}
        {{'OFF'}}
      {% else %}
        {{states('binary_sensor.switchTVboven') | upper}}
      {% endif %}


switch.yaml :

  - platform: template
    switches:
      tvboven:
        value_template: "{{ is_state('binary_sensor.sensorTVboven', 'on') }}"
        turn_on:
          - service: mqtt.publish
            data:
              payload: '10;newkaku;03a0b800;2;on;'
              topic: rflink/cmd
          - service: mqtt.publish
            data:
              payload: '{"SWITCH":"2","CMD":"ON"}' 
              topic: 'rflink/NewKaku-03a0b800'
        turn_off:
          - service: mqtt.publish
            data:
              payload: '10;newkaku;03a0b800;2;off;'
              topic: rflink/cmd
          - service: mqtt.publish
            data:
              payload: '{"SWITCH":"2","CMD":"OFF"}' 
              topic: 'rflink/NewKaku-03a0b800'

I wonder if it can be done without the binary_sensor ?

What did you have in mind?

Were you thinking of taking the binary_sensor’s functionality and incorporating it directly in the Template Switch’s value_template? (It doesn’t support that.)

I don’t know what i have in mind. Just looking if something more easy is possible to define the switch.

The only thing I can think of is if value_template could subscribe to the topic: rflink/NewKaku-03a0b800 and receive the payload directly. However, that’s not possible.

Does your switch actually work?

I was under the impression that the keys turn_on and turn_off only support one action, not a list of actions (I could be wrong).

Yes it works. I have to use the second action to bring the binary_sensor in sync with the switch if toggled from HA, since it don’t get the command from the remote.

Maybe I have to explain a little more about the setup:

  • if I switch on the outlet with the remote, the remote switches on the outlet (no HA there), and the rflink picks up the signal, receiving 20;05;NewKaku;ID=03a0b800;SWITCH=2;CMD=ON; which is translated on the Mqttgateway to topic ‘rflink/NewKaku-03a0b800’ with payload {“SWITCH”:“2”,“CMD”:“ON”,“SWITCH2”:“ON”}

this is where the binary_sensor is for (same for OFF)

  • if I switch the outlet on from HA, I publish ‘10;newkaku;03a0b800;2;on;’, which makes the rflink transmit the signal to switch the outlet on. But the rflink does not receive it’s own signal, so no 20;05;NewKaku;ID=03a0b800;SWITCH=2;CMD=ON; is received, and the binary_sensor is not updated.

  • so I publish a second MQTT message to bring the binary_sensor in sync with the outlet.

Just some information for people using rflink with mqtt.

Well, i have found there is a better (not easier) way. The problem with the old situation is the switches don’t remember their state when HA is restarted. Then I remembered Taras excellent solution for receiving data with the Sonoff Rf bridge (I use a modified version since my bridges are running openmqttgateway and not tasmota). So I decided to adapt it to esprflink :

Created an automation :

- id: '1565365902600'
  alias: rflink_boven_demultiplexer
  trigger:
  - platform: mqtt
    topic: rflink/NewKaku-03a0b800
  action:
  - data_template:
      payload: '{{trigger.payload_json.SWITCH}}' 
      command: '{{trigger.payload_json.CMD}}'      
    service: python_script.rflink_boven_demux

and a python script rflink_boven_demux.py :


d = { '2':['tvboven','true'],
      '3':['proximusboven','true'],
      '4':['fanboven','true'],
      '5':['rflinkboven','true']
    }


p = data.get('payload')
#logger.error('<rflink_boven_demux> Received p : {}'.format(p))
c = data.get('command')
#logger.error('<rflink_boven_demux> Received c : {}'.format(c))


if p is not None:
  if p in d.keys():
    service_data = {'topic':'sensor/{}'.format(d[p][0]), 'payload':'{}'.format(c), 'qos':0, 'retain':'{}'.format(d[p][1])}
  else:
    service_data = {'topic':'sensor/unknown', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
    logger.error('<rflink_boven_demux> Received unknown RF command: {}'.format(p))
  hass.services.call('mqtt', 'publish', service_data, False)

Now my binary sensor is defined this way :

  - platform: mqtt
    name: sensorTVboven
    state_topic: 'sensor/tvboven'
    device_class: 'plug'

and my switch :

  - platform: template
    switches:
      boven_tv:
        value_template: "{{ is_state('binary_sensor.sensorTVboven', 'on') }}"
        turn_on:
          - service: mqtt.publish
            data:
              payload: '10;newkaku;03a0b800;2;on;'
              topic: rflink/cmd
          - service: mqtt.publish
            data:
              payload: '{"SWITCH":"2","CMD":"ON"}' 
              topic: 'rflink/NewKaku-03a0b800'
        turn_off:
          - service: mqtt.publish
            data:
              payload: '10;newkaku;03a0b800;2;off;'
              topic: rflink/cmd
          - service: mqtt.publish
            data:
              payload: '{"SWITCH":"2","CMD":"OFF"}' 
              topic: 'rflink/NewKaku-03a0b800'

Now my switches stay synchronized with HA if I have to restart HA

Hi Francis,

In my search i found your topic.
Is it still available?

i try but hard for me to declare the switch .

I use Rflink by mqtt in node red