I’m still fairly new to Home Assistant and have been trying to wrap my head around a bunch of different aspects of this incredibly powerful system.
One thing I haven’t been able to figure out is when one would use a template sensor vs. an MQTT sensor. Can someone give me an example or two of when one would use one over the other?
You would normally use a template sensor locally. With locally I mean within HA: You have an existing value or sensor already in HA from which you want to derive something new, often applying some additional logic.
MQTT is a communications protocol and you need a broker to run it. It gives you a very generic interface to integrate many things into HA without the use of additional integrations (component / “plug-ins”; not talking about add-ons). It’s called interoperability. In other words, it’s for external use. That said, you can totally publish a value to MQTT from HA just to consume it again within HA. Nothing wrong about it, but it’s odd.
Lots of people flash devices with Tasmota and then integrate the device over MQTT. I have done it too, and where devices support it I will enable that option on the device.
Additional note on MQTT: Depending on your installation method of HA, your MQTT broker (e.g. mosquitto) could be run on a separate server (for any method), natively installed on the OS (e.g. running only HA core on Debian), as an add-on (containerised software with an HA docker, HA supervisor, or an HAOS installation). Regardless, within HA you need the MQTT integration to communicate with it.
An MQTT sensor is a sensor which uses MQTT as it’s communication protocol. As such it needs to communicate with a MQTT broker. HA will need to speak to the same broker for communication between HA and the sensor to talk place.
For example: a ZigBee device can be set up within ZigBee2mqtt. ZigBee2mqtt can only talk to an MQTT broker. The Mosquito broker can be used and, HA and ZigBee2mqtt can communicate via this.
A template sensor is something you create within HA. It is a kind of virtual sensor that combines information from any number of sources in any number of ways to produce discrete of continuous states.
For example: you could create a sensor that takes the elevation attribute of the sun.sun entity and make this the state of a new template sensor. This could be useful because it is often possible to use states but not always easy to get to attributes.
So, unless I’m missing something, MQTT sensors and template sensors are quite different.
The source of confusion may be that templates can also be applied to MQTT topics. For example a MQTT device that you want to monitor may publish only one long json string on it’s state topic, eg
{"temperature": "30", "humidity": "75"}
Home assistant cannot, on its own, make sense of that, so you use a json template to get just the temperature, and another json template to get the humidity.
That way you can, from that one topic, make one sensor entity that has the temperature as the state, and the humidity as an attribute, or you can make two sensors, one with state temperature and one with state humidity. Or any combination of those.
Thanks for the replies. I admit I’m still somewhat unclear. I think I understand the fundamental differences between template sensors and MQTT, but I still don’t get why one would use a template sensor vs an MQTT sensor.
What made me think about this is I saw some YouTube tutorials on creating notifications when a laundry (washing machine) cycle has finished. One guy did this using MQTT sensors, where the state of the sensor was changed via an automation, eg the power usage level of the washer plug would trigger an automation which would use the MQTT publish service to change the state of the sensor. Why not just use a template sensor in that case, for which the state is dependant on the power consumption of the washer plug? That would avoid the need for an automation.
Honestly, if you did, we wouldn’t be still having this conversation. Fundamentally, MQTT is a protocol. It’s like HTTP. It’s a way to pass information between systems where the two sides can be built on different technologies. Imagine email: Thousands of email providers using their own systems and flavours, and yet you can send an email to anyone with an address.
Thus, you would use an MQTT sensor if the device that’s connected to HA via a broker makes use of MQTT to expose information. Note what Nick said: In an MQTT sensor you can also use templates (to parse the information).
You would use a template sensor if you already have the information available within HA, but in another form or format. Again, a template sensor (by definition) uses templates to parse the information.
So, your choice between an MQTT sensor or a template sensor comes down to where you currently have the information available you want to use. You would need to provide more info at this point to get more help (integrations and devices involved, any existing entities, data of those entities, etc.).