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.
#!/usr/bin/python
# DHT Sensor Data-logging to MQTT Temperature channel
# Requies a Mosquitto Server Install On the destination.
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# MQTT Encahncements: David Cole (2016)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
This file has been truncated. show original
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?
Cheers
gpbenton
(Graham)
October 12, 2018, 6:42am
2
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.
#!/usr/bin/env python2
import paho.mqtt.client as mqtt
import time
import Adafruit_DHT
from configparser import ConfigParser
import json
config = ConfigParser(delimiters=('=', ))
config.read('config.ini')
sensor_type = config['sensor'].get('type', 'dht22').lower()
if sensor_type == 'dht22':
sensor = Adafruit_DHT.DHT22
elif sensor_type == 'dht11':
sensor = Adafruit_DHT.dht11
elif sensor_type == 'am2302':
sensor = Adafruit_DHT.AM2302
else:
This file has been truncated. show original
Variables that hold my values from the sensor:
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
My topic:
homeassistant/sensor/diningroomtemp/config
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.
nickrout
(Nick Rout)
October 13, 2018, 3:57am
5
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.
nickrout
(Nick Rout)
October 13, 2018, 5:07am
7
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.
nickrout
(Nick Rout)
October 13, 2018, 5:20am
9
OK, one down
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.
eximo84
October 13, 2018, 5:22am
10
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.
nickrout
(Nick Rout)
October 13, 2018, 5:23am
11
Yes I think you are right.
eximo84
October 13, 2018, 9:53am
12
So I’m now publishing two topic
Config topic payload
{"state_topic": "homeassistant/sensor/diningroomsensor/state", "value_template": "{{ value_json.temperature}}", "name": "temp sensor", "device_class": "sensor"}
State sensor payload
{"temperature": 21.4, "humidity": 73.3}
Unfortunately no sensor is created in home assistant. Not sure why to be honest.
gpbenton
(Graham)
October 13, 2018, 10:36am
13
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.
Edit:
Actually I do know. sensor
isn’t valid for device_class
. It has to be one of these values
1 Like
eximo84
October 13, 2018, 12:53pm
14
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