How do I parse out the correct MQTT sensor from a topic that has multiple sensors data publishing?

Good day,

I am trying to setup a couple of freezer monitors because we had our fridge go out a couple months ago and we lost a lot of food.

I am following this guide:

I don’t really understand MQTT that well, but unfortunately it appears that all the sensors are being published to the same topic “rtl_433/9b13b3f4-rtl433/events”. This includes random soil sensors and remotes that my neighbors must have. It also includes both of my freezer monitors.

In the guide linked it appears each device of the authors publishes different data to different topics.

In MQTT explorer if I highlight anything other than events I get no data. In MQTT configuration I can listen to a topic and events is the only one that populates anything.

So if I am getting events for 7 different sensors, how do I create devices out of only 2 of those sensors?

I tried adding identifiers, as shown in the YAML below, but it is updating both temperatures at once with the latest one polled, so they both always read the same temp, instead of the temp for the proper sensor.

Also, is it possible to just create 1 “device” in home assistant with the sensor data as attributes to that device, instead of just creating sensors for each attribute I am pulling?

mqtt:
  sensor:
  - name: deepfreeze_1_temp
    state_topic: "rtl_433/9b13b3f4-rtl433/events"
    json_attributes_topic: "rtl_433/9b13b3f4-rtl433/events"
    value_template: "{{ value_json.temperature_F }}"
    device_class: temperature
    unique_id: deepfreeze_1_temp  
    unit_of_measurement: '°F'
    expire_after: 600
    device:
      identifiers: 52815
  - name: deepfreeze_2_temp
    state_topic: "rtl_433/9b13b3f4-rtl433/events"
    json_attributes_topic: "rtl_433/9b13b3f4-rtl433/events"
    value_template: "{{ value_json.temperature_F }}"
    device_class: temperature
    unique_id: deepfreeze_2_temp  
    unit_of_measurement: '°F'
    expire_after: 600
    device:
      identifiers: 53265
  binary_sensor:
  - name: deepfreezer_1_battery_ok
    state_topic: "rtl_433/9b13b3f4-rtl433/events"
    json_attributes_topic: "rtl_433/9b13b3f4-rtl433/events"
    value_template: "{{ value_json.battery_ok }}"
    payload_off: 0
    payload_on: 1
    unique_id: deepfreezer_1_battery_ok
    device:
      identifiers: 52815
  - name: deepfreezer_2_battery_ok
    state_topic: "rtl_433/9b13b3f4-rtl433/events"
    json_attributes_topic: "rtl_433/9b13b3f4-rtl433/events"
    value_template: "{{ value_json.battery_ok }}"
    payload_off: 0
    payload_on: 1
    unique_id: deepfreezer_2_battery_ok
    device:
      identifiers: 53265

I tried to use the rtl433_autodiscovery addon and it worked how I wanted it, but for some reason the devices went unavailable after like 6 hours even though I can still see them publishing data in MQTT Explorer.

Thanks for your time.

What you have encountered is not uncommon. Your topic’s payload can contain “multiplexed” data. In other words, a single payload is used for reporting data from multiple devices. One time it may be data from device A and the next time it may be data from device B.

I suggest you consider implementing Strategy #2 described in the following topic:

It describes how to create a “demultiplexer automation”. It’s purpose is receive the published data and re-publish it to separate topics, one topic per device. Then each one of your MQTT Sensors is configured to use a device-specific topic that receives payloads only meant for that sensor.

Thanks for pointing me that direction and giving me proper terminology!

I think this definitely will help me get started. Unfortunately not versed in python. I am not sure if it’s possible to modify the script to do what I need. For reference, the script looks like:

d = { '2C8D0A':['sensor1','ON','true'],
      '2C8D0E':['sensor1','OFF','true'],
      'E5D30E':['sensor2','ON','false'],
      '30D8A0':['sensor3','ON','false']
    }

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)

My payloads look like this:

events = {"time":"2023-08-24T19:02:35.984139-0400","model":"Acurite-986","id":52815,"channel":"1R","battery_ok":1,"temperature_F":-2,"status":0,"mic":"CRC"}

I could be wrong, but I am assuming in the script is grabbing the ‘data’ field in the payload which tells if their sensor is on or off with:

p = str(data.get('payload'))

so maybe mine would be:

p = str(id.get('payload')) ?

and that data field is what cooresponds with the state which is mapped in the dictionary with:

d = { '2C8D0A':['sensor1','ON','true'],
      '2C8D0E':['sensor1','OFF','true'],
      'E5D30E':['sensor2','ON','false'],
      '30D8A0':['sensor3','ON','false']
    }

My temperature and battery_ok fields don’t have a unique data number I can map. Is it possible to make the second field in the dictionary (ON or OFF in the example) map to another value from my payload?

Again I could be totally reading the script wrong…

I had a closer look at the screenshot you posted.

Have you considered getting the temperature and battery status of the first freezer monitor directly from these two topics? (No ‘demultiplexing’ required.)

rtl_433/9b13b3f4-rtl433/devices/Acurite-986/1R/52815/temperature_F
rtl_433/9b13b3f4-rtl433/devices/Acurite-986/1R/52815/battery_ok

These are the topics for the second freezer monitor:

rtl_433/9b13b3f4-rtl433/devices/Acurite-986/2F/53265/temperature_F
rtl_433/9b13b3f4-rtl433/devices/Acurite-986/2F/53265/battery_ok

Sorry for the delay. A tornado hit! I am glad I have a physical device to monitor the temps as well!

Before when I was listening to those topics I didn’t appear to actually be getting any data, but maybe I was doing something wrong, because now I am! I am going to go ahead and set this up. Thanks for the help!