Custom MQTT discovered Temp Sensor Python Script

So i have connected a am3202 temp/humidity sensor to my raspberry pi running pihole. I have installed the adafruit_dht libraries and have a script from adafruit which can output the temp and humidity to the screen. All is fine on that front.

I would like to get these values published to home assistant via mqtt and ideally like to have them setup using the mqtt discovery syntax.

I’ve downloaded this script which for the most part works as it will publish the values to an mqtt topic.

However for discovery to work the payload needs to be sent in json format and this is where I am struggling. I assumed if I created a variable and added the payload json it would work but I am constantly getting invalid syntax on my variable.

Am I going about this the correct way? Or is there an easier way to publish the json formatted payload via mqtt?


For simple json, it is perfectly possible to format json in an ordinary string variable. For larger json constructs, there are libraries available to make it easier.

If you just want to get something running, its probably quicker just to define your sensor in yaml, but if you want to learn how to do this I suggest posting what you have and I’m sure someone will see the problem.

Yeah I would like to get it working with discovery.

I found a new script which actually passes json so I have adjusted that and although I’m passing the correct information based on the docs it doesn’t create the sensor.

Variables that hold my values from the sensor:

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

My topic:


Payload variable

    data = {'device_class': 'sensor', 
            'name': 'temp sensor', 
            'state_topic': 'homeassistant/sensor/diningroomtemp/state', 
            'unit_of_measurement': '°C', 
            'value_template': round(temperature, decim_digits) }

This is my actually payload into the mqtt topic

{"state_topic": "homeassistant/sensor/diningroomtemp/state", "unit_of_measurement": "\u00b0C", "value_template": 20.7, "name": "temp sensor", "device_class": "sensor"}

Now the degree symbol doesn’t work. I had to utf8 encoding to the script other it said invalid character. No idea on how to fix that.

Just for comparison, here is what my MQTT lights post to MQTT:

homeassistant/light/Foyerlight1/config {"name":"Foyerlight1","platform":"mqtt_json","state_topic":"Foyerlight1","command_topic":"Foyerlight1/set","rgb":true,"color_temp":true,"brightness":true,"white_value":true}

Sorry I don’t have a discovered sensor.

To add to this if I switch the payload to humidity I can pass the % without issue however the sensor isn’t created in hass.

Back to basics, do you have discovery: true in your configuration.yaml?

Yep. I am currently discovering two sonoff devices, and a nodemcu temp sensor configured with esphomelib. Discovery works.

OK, one down :slight_smile:

Can you use mosquitto_sub to show exactly what topic is posted to, and what is posted there? You will need to use -v option to show the topic.

:thinking: I wonder if I am doing this wrong. I’m sending my value in the config topic. Reading the docs it kind of suggests I need to send two commands.

One with the config of the sensor to the config topic. Then another to the state sensor with my value.

The docs aren’t fully clear and the example isn’t obvious if I am right on that. Let me do some testing.

Yes I think you are right.

So I’m now publishing two topic

  1. Config topic payload

{"state_topic": "homeassistant/sensor/diningroomsensor/state", "value_template": "{{ value_json.temperature}}", "name": "temp sensor", "device_class": "sensor"}

  1. State sensor payload

{"temperature": 21.4, "humidity": 73.3}

Unfortunately no sensor is created in home assistant. Not sure why to be honest.

I tried sending this message

$ mosquitto_pub -t "homeassistant/sensor/diningroomsensor/config" -m '{"state_topic": "homeassistant/sensor/diningroomsensor/state", "value_template": "{{ value_json.temperature}}", "name": "temp sensor", "device_class": "sensor"}'

and I got the error in the homeassistant log

homeassistant    | voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data['device_class']

Restarting HA and sending this message without device_class parameter

$ mosquitto_pub -t "homeassistant/sensor/diningroomsensor/config" -m '{"state_topic": "homeassistant/sensor/diningroomsensor/state", "value_template": "{{ value_json.temperature}}", "name": "temp sensor"}'

seems to do the trick. I’m not sure why that parameter causing a problem - it looks like a bug.

Actually I do know. sensor isn’t valid for device_class. It has to be one of these values

1 Like

Yay. That the one. Argh I was the docs where clearer. I’ll update my script to do humidity now too and should be good to go.

Thank you

1 Like