MQTT publish divided values to same topic

I designed the sensor node working on ESP8266 connected DHT11 temp&humid sensor and BMP085 temp&pressure sensor.
This node publishes mqtt data on json format respectively for DHT sensor values and BMP sensor values, like the following;

first publish the below DHT data;

{"temperature":31.70,"humidity":61.30}

delay 500ms, second publish below BMP data;

{"bmp_temp":32.00,"pressure":1.0122e3,"altitude":8.75}

Both published to the same topic; sensor/feeds/values

On frontend of Home Assistant, first published data disappeared and only second data shows.
Would anybody like to give me any hints to resolve this issue?

Could you post the sensor part from your HA configuration so it would be easier to replicate your problem? :slight_smile:

Thanks for your replying, sensor part of HA configuration like below;

#mqtt dht bmp sensors output
sensor 1:
- platform: mqtt
state_topic: "sensor/feeds/values"
name: "DHT Temperature"
unit_of_measurement: "°C"
value_template: '{{ value_json.temperature }}'
- platform: mqtt
state_topic: "sensor/feeds/values"
name: "DHT Humidity"
unit_of_measurement: "%"
value_template: '{{ value_json.humidity }}'
- platform: mqtt
state_topic: "sensor/feeds/values"
name: "BMP Temperature"
unit_of_measurement: "°C"
value_template: '{{ value_json.bmp_temp }}'
- platform: mqtt
state_topic: "sensor/feeds/values"
name: "BMP Pressure"
unit_of_measurement: "mbar"
value_template: '{{ value_json.pressure }}'
- platform: mqtt
state_topic: "sensor/feeds/values"
name: "BMP Altitude"
unit_of_measurement: "m"
value_template: '{{ value_json.altitude }}'

The reason why published data to be divided into two part is for EEPROM limitation of ESP8266 for Json format data.

I can’t try it right now but you could try to separate it into the two sensors. One for each value published.
My guess is that HA reads each message and since the last one is “blank” for the value it tries to read it assumes the sensor doesn’t have a value.

Another workaround would be to post each of the parts to different topics.
I’m not certain this will work but I would be interested in the results.

If you get a working setup and feel like it please also share the code you used with your esp8266 :relaxed:

Dear Sir, thanks for your quick replying.

  • I can’t try it right now but you could try to separate it into the two sensors. One for each value published.

—> same result as one.

  • My guess is that HA reads each message and since the last one is “blank” for the value it tries to read it assumes the sensor doesn’t have a value.

–> I also think so, first published data values are read as void.(no display, but each data exists on chart.)

  • Another workaround would be to post each of the parts to different topics. I’m not certain this will work but I would be interested in the results.

–> will be good for adopting another topic to second published data.

We might be able to workaround this with some templating. Check if the value is defined and if not don’t update it.
I’ll have to continue this when I have access to a HA install but I’m fairly certain we can do it.

Have you tried to publish with different topic for each sensor?

Can just confirm that I have the same issue. If you try to read different json keys sent in multiple messages over MQTT then you will face this issue. Mine are sent with a delay of about 100ms and you can see the frontend flicker when the “old” values go blank since they are not part of that message.

In steps:

  1. {“key1”:1, “key2”:3} sent over MQTT on topic “home/topic”
  2. {“key3”:1, “key4”:3} send over MQTT on topic “home/topic”

Once 2. is read then the values for 1. will go blank in the frontend.

Is it possible to exclude updating the GUI for values not present in the message fro that topic? Guess the event sent over websocket need be updated, or how it is handled in the GUI.

Where can I find the code for this functionality?

BR Alexander

Have you tried setting retain to True? Just an idea, or measure key1 and key2, wait, measure key3 and key4 then publish all 4 at once?

Did anyone find a solution for this?

I’m facing the same problem with Openmqttgateway BTtoMQTT, where the Xiaomi sensor packets don’t have all params every time.

Found a solultion:

value_template: “{% if value_json.tem %} {{ value_json.tem }} {% else %} {{ state.state }} {% endif %}”

Is this still the only way to accomplish this?
I’m trying to integrate with Sleep As Android https://docs.sleep.urbandroid.org/services/automation.html#events and it’ll send events in a JSON payload, like {event: sleep_tracking_started} and {event: sleep_tracking_stopped}, among others. So the template could still work, using if/elif/else/endif:

    value_template: "{% if value_json.event == 'sleep_tracking_started' %} on {% elif value_json.event == 'sleep_tracking_stopped' %} off {% else %} {{ state.state }} {% endif %}"

but it would really be nice if there were a better way. The only other way I can think of is to use a mqtt automation trigger for the single topic, and manually toggle switches based on what the incoming JSON $.event is. But that requires a toggleable switch, rather than a binary_sensor.