Combining two MQTT topics into one entity

I have data feeding into two different MQTT topics that actually represents the same information, but at different times. For example, two temperature sensors in the same room.

I would like to create a single “virtual” device/entity that represents the merged streams of data from both sensors.

I think the answer is a template sensor, but I’m unclear on the details. I believe I would specify the two source streams/entities in the “entity_id” section of the sensor, but I don’t know what to put into the “value_template” section to say “take the value of whichever source sensor just changed”.

Hi Martin Vuille,

You are mentioning value_template, that is a legacy version of syntax. If you are learning, don’t learn that… It is legacy and being used less and less…
You want something like this:
Template - Home Assistant.

where you will create a template in state that will determine which of the values that you think should be used an set the state of the template sensor to that value.
There is some new thing in the GUI to do something with template sensors as well, but I have no idea about how that works.

Create two sensors, one for each topic.

This will give you the average of the two sensors:

Thanks, but that’s not what I need.

I have two sensors via MQTT. I want a new “virtual” sensor that changes every time one of the two MQTT sensors changes, and takes on the value/state of the latest sensor to change.

The averaging you suggest would indeed be the right answer for my example of two temps in a room, but that was just my poor attempt at illustration and isn’t actually what I’m dealing with.

Create a Trigger-based Template Sensor employing two MQTT Triggers.

The received value will be in trigger.payload.

Thank you. I think that’ll do the trick.

1 Like