Adding a MQTT Sensor To Home Assistant

In the previous tutorial we looked at adding a MQTT switch device. In this tutorial we will look at adding MQTT sensors.

Sensor entities are read only and the information sent has a value which can be a integer,string or JSON encoded data.

Sensors also have an optional device class which is very useful and should be configured. Possible device classes are here



They also have a unit of measurement as well as other properties shown on this page.

As an example we will add four temperature sensors

Below is a screen shot showing the start of the sensor section with the single switch entry above.

home-ass-mqtt-sensors

My sensor section has four temperature sensors and the configuration is shown below:

homeass-sensor-mqtt-2

Notes:

Many of the settings for the fours sensors are the same. They are all temperature sensors and been given the device class of temperature.

The bedroom temperature sensor doesn’t use the availability topic and so it doesn’t need to be sent the available message, but the other sensors do.

The bedroom temperature sensor has a value template and expects to receive JSON data. e.g ‘{“temperature”:20}’. All of the other expect to see an integer,float or string.

The outside temperature sensor has been assigned to a device called outside monitor.

If I add each of these sensors to an area we see the value on the area card because we assigned the device class.

homeass-area-card

If we look at the available devices we can see our outside monitor.

MQTT-device-home-assistant

To activate the sensors that have an available topic I publish the message online to the available topic.

  • home/kitchen/temperature/available
  • home/livingroom/temperature/available
  • home/outside/temperature/available
If I don’t do that then the temperate value will not be displayed.
We can simulate any of the sensors using the mosquitto_pub tool.
Below I set the value of the bedroom temperature sensor.
mqtt-sensor-json


Resources and related tutorials
Please Let me Know if you found it Useful
[Total: 5 Average: 5]

One comment

  1. I struggled days to get my DIY sensors ‘available’ in HA. Tried Bard, ChatGPT, searched with Google, no answer – except from here. Simple fix. Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *