I just deleted everything and followed Taras Solution 2. It seems that the problem was a missing comma in the python script which caused a problem in the sensors .
Typically the best choice is to use the native api for direct (bi-directional) communication with home assistant when using esphome (so no mqtt broker or any configuration/“strategies” like described in this post are actually needed)
I did, you can just trigger anything interacting with ha from the esphome device directly. I use this for virtually everything (including IR and RF).
Also someone (don’t find any link right now - sorry) actually “abused” the tags in ha (with it’s nice management)
Home Assistant has a dedicated panel that allows you to manage your tags. You can add names, automate or delete them
Just now saw that you were referring to the Home Assistant Events. From what I know that’s not possible to use them with mqtt but only by utilizing the native api (from esphome).
First off, I’d like to say I’m very thankful for this script and the instructions on implementing it. I’ve used soon after this thread we as created and it’s integral to my setup.
I’m facing a problem with memory leak since updating to 2022.7 (2022.7.1 through to .7). I suspect it is due to the python 3.10 change because nothing else in the breaking changes affects me. I doubled all my custom integrations (HACS) for a day and the problem persisted. The last thing I can think of is this python script.
Is anyone aware whether this script would malfunction on the upgraded python library 3.10 on 2022.7. I don’t understand coding libraries so I’m appealing to the knowledge and experience here.
Thanks in advance
In case it matters: Rasp Pi 4, 4GB, Homeassistant OS.
First thing, thank you so much for the author and commenters on this topic.
I received several cheap RF door sensors, and the MQTT messages coming via rtl_433 would be joined in one topic because the devices use different identifiers, in this case ‘dipswitch’ which is part of the payload. After some messing about with strategy 1, I moved to strategy 2 which is so much more scalable and a good experience to go through to understand the idea behind demuxing.
Setup:
HW: HA running on an Intel NUC with RTL_433 running on a Rpi3b.
SW: Home Assistant 2022.7.7, Supervisor 2022.07.0, Operating System 8.4
d = { '-o-+-oo-o':['sensor01','true'],
'--+--o+++':['sensor02','true'],
'o++--o--o':['sensor03','true'],
'--o--o-+-':['sensor04','true']
}
e = { '10R':['on'],
'00L?':['off']
}
p = data.get('payload') # I must keep the dict and not str() convert it
p1 = p['dipswitch']
p2 = p['rbutton']
if p is not None:
if p1 in d.keys():
service_data = {'topic':'rtl_433/Cardin-S466/{}'.format(d[p1][0]), 'payload':"{}".format(e[p2][0]), 'qos':1, 'retain':"{}".format(d[p1][1])}
elif p1 == "":
service_data = {'topic':'rtl_433/Cardin-S466/blank', 'payload':'{}'.format(p), 'qos':1, 'retain':'false'}
else:
service_data = {'topic':'rtl_433/Cardin-S466/unknown', 'payload':'{}'.format(p).str() , 'qos':1, 'retain':'false'}
logger.warning('<rfbridge_demux> Received unknown RF command: {}'.format(p))
hass.services.call('mqtt', 'publish', service_data, False)
mqtt yaml file
# Garage Side Door
- unique_id: 'door_garagesidedoor'
name: 'Garage Side Door'
device_class: 'door'
state_topic: 'rtl_433/Cardin-S466/sensor01'
value_template: '{{ value }}'
payload_on: 'off'
payload_off: 'on'
# Front Door
- unique_id: 'door_frontdoor'
name: 'Front Door'
device_class: 'door'
state_topic: 'rtl_433/Cardin-S466/sensor02'
value_template: '{{ value }}'
payload_on: 'off'
payload_off: 'on'
etc...
I have configured everything so far, but the status of “binary_sensor” remains unchanged at “unknown”.
The automation displays the correct status, but the sensor does not receive the information.
alias: Tor Status
trigger:
- platform: mqtt
topic: tele/sonoff_bridge/RESULT
action:
- service: python_script.rfbridge_demux
data:
payload: '{{trigger.payload_json.RfReceived.Data}}'
/config/python_scripts/rfbridge_demux.py
d = { 'C1615E':['sensor1','ON','true'],
'C16157':['sensor1','OFF','true']
}
p = str(data.get('payload'))
if p is not None:
if p in d.keys():
service_data = {'topic':'home/{}'.format(d[p][0]), 'payload':'{}'.format(d[p][1]), 'qos':0, 'retain':'{}'.format(d[p][2])}
else:
service_data = {'topic':'home/unknown', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
logger.warning('<rfbridge_demux> Received unknown RF command: {}'.format(p))
hass.services.call('mqtt', 'publish', service_data, False)
@123 thanks for your work explaining the strategies. Is there any way of having resiliency of messages by having multiple sonoff bridges ? I feel i am missing 433mhz signals at times, which would be understandable, as i only have on of them.
Ofcourse then you have to deal with possible multiple messages…
Boy am I glad I found this thread. I have been sing rf sensor for a long time and it always annoyed me that they would go back to unknown each time I restarted HA.
Now my door sensor are all good and report the correct state (unless of course their state change while HA is rebooting)
My movement detector still shows unknown after a restart but I assume this is expected because HA is not able to read the current state until it changes.
If you are using Strategy #2 and your motion detector reports both its on and off states, the strategy offers a means of publishling the motion detector’s value as a retained message (i.e. stored on the broker). Therefore the stored value is available to Home Assistant immediately upon startup.
From the first post:
Because the door sensor reports both of its states, the automation can use retain: true when publishing to the contact sensor’s topic. When Home Assistant restarts, it will re-subscribe to the contact sensor topic’s and receive its current (retained) state from the broker.
Hi,
most of my contact sensors report the on and off state. Do you mind tell me where to add the retain: true to retain the states as they all show unknown on boot. Do I add it in the action of my rfbride_demultiplexer like this
In the demux python script there is a variable d where you store all the sensor you have. Like this example
d = { 'C1615E':['sensor1','ON','true'],
'C16157':['sensor1','OFF','true']
here you create a sensor called sensor1 that has 2 code (C1615E and C16157) and the retain is set to true so that it will remember its previous after a reboot.