Sending subsets of data over MQTT to the same topic

Hi, I have a sensor sending three values over MQTT (temperature, humidity, battery level). But the battery level only gets sent every 10 transmissions, so when I send a temperture and humidity reading then my battery level on dashboard goes blank. And when I send a battery level then my temperature and humidity values go blank. Is there a config that allows me to send subsets of data (e,g, temperature, humidity and battery level) without having to send all three with every MQTT message?

Here is the YAML I am using:
sensor:

  • platform: mqtt
    name: “Temperature”
    state_topic: “myhome/RF_Device04”
    unit_of_measurement: ‘°C’
    value_template: “{{ value_json.TMP }}”
  • platform: mqtt
    name: “Humidity”
    state_topic: “myhome/RF_Device04”
    unit_of_measurement: ‘%’
    value_template: “{{ value_json.HUM }}”
  • platform: mqtt
    name: “Battery”
    state_topic: “myhome/RF_Device04”
    unit_of_measurement: ‘V’
    value_template: “{{ value_json.BATT }}”

It is simpler to have different payloads use different topics.

But if you really must use the same topic for different payloads thevalue template needs to check to see if the payload is in the message before setting it, and then set it to the existing value if it is not present

From memory (which isn’t very good) it goes something like

value_tempate: {% if value_json.BATT is defined %} {{ value_json.BATT}} {% else %} {{ ...existing state value ... }} {% endif %}

Many thanks for the quick reply. I’ll use different topics per payload. I was using the Temperature and Humidity example from this link:

Which led me down the road of using same topic for different payloads because in the example it is sending both temperature and humidity to the office/sensor1 topic.

1 Like