433 Mhz Motion sensors with rolling codes

Hi,

I have these motion sensor from a previous alarm system that didn’t perform very well. Now I want to integrate them into hassio through an mqttgateway. All thanks to DrZzs inspiration :wink:

The part where I receive and publish the code to my mqtt broker works perfectly, but now I see that it’s using some sort of rolling codes.

There is some sort of repetition, but I can’t extract it.
Example:

373201CC0546040E5C
372801CC0546040E5C
373C01CC0546040E5C
373201CC0546040E5C

This is my code for the binary sensor.

  - platform: mqtt
    name: "Motion test"
    state_topic: "mqttgateway/rfin"
    payload_on: "372801CC0546040E5C"
    payload_off: "372801CC0546040E5C_off"
    device_class: Motion 
    optimistic: false
    qos: 1
    retain: false

Can I solve this with a payload template?

Any help is highly appreciated :smiley:

This payload is the first and fourth in the sequence. Does this always correspond with motion being detected?

There is only a motion message, nothing else I’m aware off. So how can I extract the correct payload? I guess the last part of the message is enough to use for the binary sensor?

The last part of the string seems to be the same, so I think I would have the value_template pick out the last characters. A bit of experimenting with the HA template dev tools page says this will get everything after the 4th character

value_template: {{ value[4:] }}

and set the payload on to match the ending characters

  payload_on: "01CC0546040E5C"

If you only have an on indication, then you will need some kind of automation to reset the sensor, but I am afraid my yaml programming isn’t good enough to help you with that - I use appdaemon for automations.

Thanks a lot! It works! :smiley: Although, turns out, I had to change the value to 13, just to be sure about getting it right every time. Still wondering if there is a solution for the rolling codes. Would that be the job of the gateway or home assistant to decode?

Here is my code for now:

- platform: mqtt
    name: "Motion test"
    state_topic: "mqttgateway/rfin"
    payload_on: "40E5C"
    payload_off: "40E5C_off"
    value_template: '{{ value[13:] }}'
    device_class: Motion 
    optimistic: false
    qos: 1
    retain: false
1 Like

I am not sure what else you would want to decode, if the sensor only sends ON indications, but personally I like to have all the intelligence in one place in HA because its easier to debug there, and leave the sensors and gateways as simple as possible.