Autodiscover for Mqtt temperature sensor

Hi,
I’m trying to setup an esp8266 to auto discover and publish via Mqtt. I followed the examples found here: https://www.home-assistant.io/docs/mqtt/discovery/
but when I publish the configuration message:
{“device_class”:“sensor”,“name”:“Temperature”,“state_topic”:"/homeassistant/sensor/blablablastate",“unit_of_measurement”:“°C”,“value_template”:"{{ value_json.temperature }}"}
with topic:
/homeassistant/sensor/blablablaT/config

I get this error in HA log:
2019-01-20 09:04:08 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/sensor/mqtt.py”, line 69, in async_discover_sensor
config = PLATFORM_SCHEMA(discovery_payload)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled([], data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 589, in validate_dict
return base_validate(path, iteritems(data), out)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 427, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data[‘device_class’]

what I’m doing wrong?

thanx in advance

/ is not correct as a start to the topic, for either message.

/homeassistant is a completely different topic than homeassistant

thanx for you’re reply… I removed the starting / from both topic and state_topic, but I can’t see any item added on HA…

is this set to homeassistant/sensor/blablablaT/state?

The first 3 things need to match homeassistant/sensor/blablablaT and it is case sensitive

the topic is homeassistant/sensor/blablablaT/config and the state is homeassistant/sensor/blablabla/state (without the T) as shown in the doc page…

the topic, when I publish temperature and humidity is homeassistant/sensor/blablabla/state (without the T)

now I get in the log:
2019-01-21 15:54:32 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor camerettaT, sending update
2019-01-21 15:54:32 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor camerettaH, sending update

but there’s any item in HA…

Does this sensor have more than one sensor on it? The example you are following is for devices that contain more than one sensor. If your sensor is Temperature only, I would make the state topic match the correct sensor.

But you sure you dont see sensor.blablablaT in your dev-state? You might need to reboot HA if you have been messing with MQTT Discovery a lot.

EDIT: Sorry I don’t read so well. :wink:

hehehe the name of the room is not a secret :slight_smile: I tried to restart ha and I still receive this error:
2019-01-21 16:18:28 INFO (MainThread) [homeassistant.components.sensor] Setting up sensor.mqtt
2019-01-21 16:18:28 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/sensor/mqtt.py”, line 69, in async_discover_sensor
config = PLATFORM_SCHEMA(discovery_payload)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 267, in call
return self._compiled([], data)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 589, in validate_dict
return base_validate(path, iteritems(data), out)
File “/srv/homeassistant/lib/python3.5/site-packages/voluptuous/schema_builder.py”, line 427, in validate_mapping
raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: value is not allowed for dictionary value @ data[‘device_class’]

to be clear,
configuration topic1: homeassistant/sensor/camerettaT/config
message: {“device_class”:“sensor”,“name”:“Temperature”,“state_topic”:“homeassistant/sensor/cameretta/state”,“unit_of_measurement”:“°C”,“value_template”:"{{ value_json.temperature }}"}

configuration topic2: homeassistant/sensor/camerettaH/config
message: {“device_class”:“sensor”,“name”:“Humidity”,“state_topic”:“homeassistant/sensor/cameretta/state”,“unit_of_measurement”:"%",“value_template”:"{{ value_json.humidity }}"}

state topic: homeassistant/sensor/cameretta/state
message: {“time”:“15:51:18”,“temperature”:“14.80”,“humidity”:“85.70”}

my config (the 2 sensor in the config that I have manually added works good):

configuration.yaml:
mqtt:
broker: 192.168.1.252
discovery: true
discovery_prefix: homeassistant
port: 1883
client_id: home-assistant-1
username: !secret mosquitto_username
password: !secret mosquitto_password

sensor: !include includes/sensors.yaml

sensors.yaml:
- platform: mqtt
name: “temperatura camera”
state_topic: “home/sensor”
qos: 0
value_template: “{{ value_json.temperature }}”
unit_of_measurement: “°C”

- platform: mqtt
  name: "umidità camera"
  state_topic: "home/sensor"
  qos: 0
  value_template: "{{ value_json.humidity }}"
  unit_of_measurement: "%"

I don’t think you need a device class of sensor for a sensor. I am trying to remember how I have mine configured. Lemme look

EDIT:

In your camerettaT device_class should be temperature
and
in your camerettaH device_class should be humidity

you’re great FlamingM0e!!! It discovered the 2 sensor with name:
sensor.humidity
sensor.temperature

know the clue is… how can I change the names via Mqtt message? :stuck_out_tongue:

It’s right there in your config publication. "name":"Temperature"

I’ve found that’s the friendly name… the sensor name remain sensor.temperature :frowning:

name in my configs are directly responsible for the name in HA

#define TEMP_NAME "Office Temp"

oops, sorry, my topic states the same name as well.

#define TOPIC_TEMP_CONF "homeassistant/sensor/office_temp/config"
#define TOPIC_PRESS_CONF "homeassistant/sensor/office_press/config"

uhm… I’m doing the same… maybe HA don’t like me :slight_smile:

if you want to have a look, this is my code (I build the string in mqtt_publish sub): https://pastebin.com/BnMTp6Fw

Interesting. Mine is much more simplified than that, but I do not see where you are specifying the actual name. Perhaps I am overlooking it.

I’m now trying to send 2 messages, 1 for temp and 1 for hum… I’ll update later :wink:
(sorry for my code is not so clear… I don’t have much time to spend :frowning: )

I prefer one message for both

seems I solved the problem with the name… if I use upper case (cameretta_Temp) it will create sensor.temperature, if I use only lowercase (cameretta_temp) it will create sensore.cameretta_temp :slight_smile:
now I have to delete several garbage sensors… :slight_smile:

thanx a lot!
I have to offer you a pizza and a beer!

1 Like