433mhz receiver (mqtt) + multiple switches

Hey all! I’ve got one of those Sonoff bridge devices (flashed with Tasmota) and offers received signals as a json string over MQTT as follows

{"RfReceived":{"Sync":7740,"Low":250,"High":720,"Data":"051FD1","RfKey":"None"}}

Where ‘Data’ is different for each button that is pressed but also works for door sensors etc etc (everything that can send a 433mhz pulse) and SYNC is a random number. I’ve managed to configure multiple buttons as follows (2 buttons in this example but it’s actually 13)

- platform: mqtt
    name: "Switch433N1B2"
    state_topic: "tele/sonoffbridge/RESULT"
    value_template: '{% if value_json.RfReceived.Data == "057D51" %}{{ value_json.RfReceived.Sync }}{% else %}{{ 0 }}{% endif %}'
  - platform: mqtt
    name: "Switch433N1B3"
    state_topic: "tele/sonoffbridge/RESULT"
    value_template: '{% if value_json.RfReceived.Data == "057D58" %}{{ value_json.RfReceived.Sync }}{% else %}{{ 0 }}{% endif %}'

this works BUT when the first button is pressed the second value goes from (something) to 0 and button 1 gets the Sync value. Biggest issue I have now is that writing automations depending on the pressed button are ‘not done’ since even pressing the second button triggers a change on the first. I’ve tried things like ‘above’ in a condition but this gives me errors. I would prefer to either:

  • “With each press on button 2 the value of button 2 is incremented” (press on button 1 equals no change in button 2)
    or
  • “With each other button press except for 2 the value stays the same as the previous know value”

I’ve tried something like:

'value_template: {{states.sensor.switch433n1b3.attributes.state}} + {% if value_json.RfReceived.Data == "C0CB41" %}{{ value_json.RfReceived.Sync }}{% else %}{{ 0 }}{% endif %}'

and

'value_template: {% if value_json.RfReceived.Data == "C0CB41" %}{{ value_json.RfReceived.Sync }}{% else %}{{ states.sensor.switch433n1b3.attributes.state  }}{% endif %}'

but no success yet

Remove the else portion from each statement. Your value templates are setting each item to zero when it gets a value from another based on your if statement.

- platform: mqtt
    name: "Switch433N1B2"
    state_topic: "tele/sonoffbridge/RESULT"
    value_template: >
      {% if value_json.RfReceived.Data == "057D51" %}
        {{ value_json.RfReceived.Sync }}
      {% endif %}'
  - platform: mqtt
    name: "Switch433N1B3"
    state_topic: "tele/sonoffbridge/RESULT"
    value_template: >
      {% if value_json.RfReceived.Data == "057D58" %}
        {{ value_json.RfReceived.Sync }}
      {% endif %}'

Thanks for the reply! I’ll give that a shot, could it be that this does not work as a one liner (since I’ve tried removing the if earlier)… Right now i’ve managed to make it work by using this in the else statement

states.sensor.switch433n1b3.state

BUT the ‘sync’ value doesn’t seem to be very random and thus presses are missed

00:41:33 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7740,"Low":250,"High":720,"Data":"051FD2","RfKey":"None"}}
00:41:59 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7760,"Low":250,"High":720,"Data":"051FD1","RfKey":"None"}}
00:42:01 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7750,"Low":250,"High":720,"Data":"051FD1","RfKey":"None"}}
00:42:02 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7730,"Low":260,"High":710,"Data":"051FD8","RfKey":"None"}}
00:42:05 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7740,"Low":250,"High":720,"Data":"051FD1","RfKey":"None"}}
00:42:07 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7750,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}
00:42:09 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7760,"Low":250,"High":720,"Data":"051FD1","RfKey":"None"}}
00:42:11 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7750,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}
00:42:13 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7750,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}
00:42:16 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7750,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}
00:42:18 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7750,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}
00:42:20 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7760,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}
00:42:22 MQT: tele/sonoffbridge/RESULT = {"RfReceived":{"Sync":7740,"Low":250,"High":720,"Data":"051FD8","RfKey":"None"}}

setting it to states.sensor.switch433n1b3.state is identical to removing the else statement. It may be safer because templates require a value. You may have problems at start up’s using that method.

Try this if you are trying to create a toggle:

value_template: >
  {% if value_json.RfReceived.Data == "057D58" %}
    {% if is_state('sensor.switch433n1b3', 0) %}
      1
    {% else %}
      0
    {% endif %}
  {% else %}
    {{ states.sensor.switch433n1b3.attributes.state }}
  {% endif %}

That should cause the state of the sensor to flip flop on each press. It will become out of sync if home assistant is restarted when the device is on.

Thanks! I will give this a try later tonight! I think I can tackle the out-of-sync by turning off all connected devices on a reboot :slight_smile:

Hi Petro, it works! Thanks… only thing I did have to change was {% if is_state(‘sensor.switch433n1b3’, 0) %} to {% if is_state(‘sensor.switch433n1b3’, ‘0’) %} so I guess it sees the state as a string instead of an integer.

Spoke a bit too soon, the {{ states.sensor.switch433n1b3.attributes.state }} still had the negative effect or resetting all the switched to a empty state {{ states(‘sensor.switch433n1b3’) }} seems to work! Finally I can start programming some ‘real’ switches :slight_smile:

nice, glad to help

Jeroen,

I know this post is about a year old but I am trying to do something similar to what you and petro figured out. For about four days now I have been trying different things. Could you by any chance post exactly what you have in your configuration and automation to make this work? i am pounding my head over here and would really appreciate it, thanks.

what are you trying to do? The exact same thing he did?

I believe it is the same or quite similar. I have three RF outlets and one remote with 6 buttons. So each outlet is controlled by two buttons, one for off and one for on. I can control these outlets from HA but we will also be using the remote. Because the remote will also be in use I would like the state to change in the HA frontend when the remote is used.