Retain RF433 sensors states after Hass Restart?

Hello community!!
Is there any way to retain the states of RF433 sensors after a Hass restart like publish2 option in Tasmota Rules?

Hello @geoxen and welcome!

You mean that you would like OMG to publish the RF values to MQTT with retain state?

Yes! After the Home Assistant restart.
As i see in User_config.h there is a parameter #define will_Retain true
But whenever i make changes to my Home Assistant config and restart the RF Sensors states are changing.

Example: I have some GS-WDS07 Sensors with Open/Closed RF Signal. When i restart the Home Assistant, all these sensors change to “Closed” state even if it’s really “Open”.

I tried the same with Tasmota and created a rule with the Publish2 Option which publishes topics with retain flag and worked great. Even after restart.

Is it possible with OMG?

Thank you for your time and your project btw!! :smiley: :smiley:

+1 I’ve noticed this as well :slight_smile:


This macro is only used for the will message, online/offline.

The issue if we use the retain state of 433toMQTT the broker will only retain the last received value. And as most of people have several sensors I don’t think it will be relevant. Maybe it should be liked with the use of valueAsaSubject option ?

Do you mean the last received value from a single sensor?

The last received value will correspond to the last emitting 433mhz sensor from all your 433mhz sensors.

On tasmota firmware all sensor states are being retained after restart if i create the rule with Publish2 option, not just the last received one!

I’m building a custom Gateway atm with Mega+Ethernet Shield+RF433+BL+IR and this feature is a must have for me!!

Is it possible to add this feature on OMG? :grinning:

I understand the need, but how are you handling this, if you have one topic per gateway ?

In Home Assistant I have a demultiplexer that relays messages to separate mqtt topics and optionally (based on my config) adds retain flag (for example, I only need door sensors’ state, no PIRs etc).

not sure it’s good.
Anyway, don’t think the gateway is the best option to do that king of job.

Thank you for your reply!

I found a multiplexer script for Sonoff Bridge devices. But i can’t make it work even with some modifications i’ve made.

Can you provide your multiplexer script?

I think it’s better to find out why your modifications didn’t work.

Here’s my automation:

- alias: pilight2mqtt_contact demultiplexer
    - platform: mqtt
      topic: !secret pilight_bridge_state_topic
    condition: template
    value_template: >
      {{ trigger.payload_json.protocol == 'GS-iwds07' or trigger.payload_json.protocol == 'kerui_D026' }}
    - service: python_script.pilight2mqtt_contact_demux
        protocol: "{{ trigger.payload_json.protocol }}"
        code:     "{{ trigger.payload_json.message.unit if trigger.payload_json.protocol == 'GS-iwds07' else trigger.payload_json.message.unitcode }}"
        state:    "{{ trigger.payload_json.message.state }}"
        battery:  "{{ trigger.payload_json.message.battery if trigger.payload_json.protocol == 'GS-iwds07' else '1' }}"

and here’s the script:

protocol = data.get('protocol')
code = int(float(data.get('code', 0)))
#assert code != 0, "please specify code"
state = data.get('state', None)
#assert state is not None, "please specify state"

#logger.debug("code: %s, state: \"%s\", battery: \"%s\"", code, state, battery)

pilight2mqtt_states = {
    'opened':   hass.states.get('sensor.contact_always_opened').state,
    'closed':   hass.states.get('sensor.contact_always_closed').state,
    'tamper':   hass.states.get('sensor.contact_always_tampered').state

## substitute with a proper import!!!
contact_low_battery = 'low_battery'

#assert opened_state is not None, "cannot get opened_state!"
#assert closed_state is not None, "cannot get closed_state!"
#assert tampered_state is not None, "cannot get tampered_state!"

key_topic           = 'topic'
key_payload         = 'payload'
key_retain          = 'retain'
key_attrs           = '_attrs_'

key_attr_battery    = 'battery'

value_retained      = 'True'
value_not_retained  = 'False'

known_codes = {
    'kerui_D026': {
        441825: {
            key_topic : 'home/contacts/1st_floor/safe',
            key_retain : value_retained
    'GS-iwds07': {
        29319: {
            key_topic : 'home/contacts/1st_floor/bathroom/window',
            key_retain : value_retained,
            key_attrs: {
                key_topic : 'home/contacts/1st_floor/bathroom/window/attributes',
                key_retain : value_not_retained

if protocol in known_codes.keys():
    if code in known_codes[protocol].keys():
        if state in pilight2mqtt_states.keys():
            service_config = known_codes[protocol][code]
            service_data = {
                key_topic : service_config[key_topic],
                key_payload : pilight2mqtt_states[state],
                key_retain : service_config[key_retain]
            logger.debug("publish state:'mqtt', 'publish', %s:%s, %s:%s, %s:%s)", key_topic, service_data[key_topic], key_payload, service_data[key_payload], key_retain, service_data[key_retain])
  'mqtt', 'publish', service_data)

            if protocol == 'GS-iwds07':
                battery = int(float(data.get('battery')))
                service_config = known_codes[protocol][code][key_attrs]
                service_data = {
                    key_topic : service_config[key_topic],
                    key_payload : "{\"" + contact_low_battery + "\":\"" + str(bool(battery == 0)) + "\"}",
                    key_retain: service_config[key_retain]
                logger.debug("publish attributes:'mqtt', 'publish', %s:%s, %s:%s, %s:%s)", key_topic, service_data[key_topic], key_payload, service_data[key_payload], key_retain, service_data[key_retain])
      'mqtt', 'publish', service_data)
            logger.error('unknown state \"%s\" for code %s, ignored', state, code)
        logger.debug('unknown code %s, ignored', code)
    logger.debug('unknown protocol: \"%s\", ignored', protocol)

I managed to make it work through a Nodered script which is doing the same job as this python one you provided! Thank you for your time and the inspiration! :grinning::grinning: