Mqtt binary sensor with more than one payload_on

I have a PIR motion sensor that runs over RF. But its an annoying one that have several payloads according to the proximity from the sensor.
So if I’m close when it triggers, it pays out one number, a bit further away = another number etc.
This makes it extremely annoying and inconsistent when I’m trying to automate around that.
I don’t want to have sensors in HA for every proximity the sensor gives a state on.

My binary sensor is implemented in HA like so:

- platform: mqtt
  state_topic: "tele/JDBA-RF-Bridge/RESULT"
  name: "Kitchen movement"
  value_template: "{{ value_json.RfReceived.Data }}"
  payload_on: "14C9EC"
  off_delay: 5
  device_class: motion

It is reporting in to a Sonoff RF bridge with the values.

So, my question is the following:
Is there a way that i can make several payload_on: values, or can someone point me to a direction for me to achieve my goal of just getting a statement if there is movement, and not which kind of movement there is?

Thanks in advance
Baskedk

1 Like

Use a demux script, and set the value for all possible receives.

d = { '14C9EC':['pir1_sensor','ON','false'],
      '14C9ED':['pir1_sensor','ON','false'],
      '14163857':['doorbell_sensor','ON','false'],
      '13666408':['firealarm','ON','false'],
      '564886':['garagedoor','ON','false'],
      '16726408':['lichteetkamerboven','ON','false'],
      '16765268':['impuls','ON','false'],
      '16766303':['impulsb','OFF','false'],
      '16765265':['flamingo1','ON','false'],
      '352003':['buurvrouw','ON','false'],
      '1213858':['lichtwaskot','ON','false'],
      '1224498':['buurvrouw1','ON','false'],
      '16762193':['impulsa','OFF','false'],
      '16762196':['impulsa','ON','false'],
      '2291358':['voordeur','ON','true'],
      '2291351':['voordeur','OFF','true'],
      '2283159':['voordeur','OFF','true'],      
      '152232' :['schuindak','ON','false'],
      '16762196':['onbekend','ON','false'],
      '14783052':['firealarm','ON','false'],
      '8345702':['onbekend','ON','false'],
      '774915':['achterdeur','ON','true'],
      '774921':['achterdeur','OFF','true']
    }


p = data.get('payload')

if p is not None:
  if p in d.keys():
    service_data = {'topic':'sensor/{}'.format(d[p][0]), 'payload':'{}'.format(d[p][1]), 'qos':0, 'retain':'{}'.format(d[p][2])}
  else:
    service_data = {'topic':'sensor/unknown', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
  hass.services.call('mqtt', 'publish', service_data, False)

2 Likes

Thanks a lot for pointing me in the right direction.
I only have 5 RF sensors (two almost never gets used) so I opted in for the easier solution with the else statements.
If anyone else wants a summary on how I got a working sensor with more on states, the evolved version of my original sensor is below here:

- platform: mqtt
  state_topic: "tele/JDBA-RF-Bridge/RESULT"
  name: "Kitchen movement"
  value_template: >-
    {% if value_json.RfReceived.Data == '14C9EC' %}
      {{'ON'}}
    {% elif value_json.RfReceived.Data == '14C9E8' %}
      {{'ON'}}
    {% elif value_json.RfReceived.Data == '14C9E0' %}
      {{'ON'}}
    {% else %}
      {{states('binary_sensor.kitchen_movement') | upper}}
    {% endif %}
  off_delay: 5
  device_class: motion

Thanks again for your help, very much appreciated :kissing_heart:

1 Like

Thank you so much for your help @francisp :star_struck:

From my perspective, what the sensor publishes is not the Id, but rather the Id+measurement. This is confirmed by the fact that the first 3 bytes are always the same for a given sensor.
Rather than creating multiple if/else statements, I would just create a template that keeps the sensor ID part of the message.
It makes it much easier to debug and mantain (imagine you have some measurement value that you missed out, in which case the entity will not update corectly)