Multi sensor using same MQTT topic

Here are two ways to accomplish what you want.

In the first approach, each sensor uses a template to extract the desired information. Both sensors are subscribed to the same topic so each sensor’s template must extract the data intended for it. If the received data is not intended for it (i.e. temperature sensor receives humidity data) then the template reports the sensor’s existing state.

- platform: mqtt
  name: Temp
  device_class: temperature
  state_topic: '/response/node/7/class/sensor/status'
  value_template: >-
    {% if '0x01' in value %}
      {{value.split('=')[3] }}
    {% else %}
      {{states('sensor.temp')}}
    {% endif %}

- platform: mqtt
  name: Humidity
  device_class: humidity
  state_topic: '/response/node/7/class/sensor/status'
  value_template: >-
    {% if '0x05' in value %}
      {{value.split('=')[3] }}
    {% else %}
      {{states('sensor.humidity')}}
    {% endif %}

The second approach demultiplexes the data found in the single topic. An automation is used to extract and forward temperature data to a dedicated temperature topic. It forwards humidity data to a separate, dedicated humidity topic. The two sensors simply subscribe to the appropriate topic.

- alias: 'data demultiplexer'
  trigger:
    platform: mqtt
    topic: '/response/node/7/class/sensor/status'
  action:
    service: mqtt.publish
    data_template:
      payload: "{{trigger.payload.split('=')[3] }}"
      topic: >
        {% if '0x01' in trigger.payload %}
          sensor/temp
        {% else if '0x05' in trigger.payload %}
          sensor/humidity
        {% else %}
          sensor/error
        {% endif %}
  - platform: mqtt
    name: Temp
    device_class: temperature
    state_topic: sensor/temp


  - platform: mqtt
    name: Humidity
    device_class: humidity
    state_topic: sensor/humidity

3 Likes