Using a Sensor through MQTT

Hi all,

I just installed HA (based on image on Raspberry Pi) and it looks pretty nice.

Now I have a CO2. humidity and temperature sensor which sends its data through MQTT.

So I installed the MQTT broker (Mosquitto) on my HomeAssistant and configured it correctly. Additionally I configured my sensor to send its data to the broker.

So far this works fine. I can even connect with mosquitto_sub and see the messages from my sensor dropping in:

root@zentrale:~# mosquitto_sub -h hass -t hass/sensor/schlafzimmer/luftsensor -u mqtt -P "xxxxxxx"
{"meta":{"sn":75,"ip":"192.168.22.60","name":"Unipi IAQ Sensor RW-THC","desc":"IAQ RW-THC Schlafzimmer","fw_version":"3.4","uptime":1533,"heap_size":123.184,"heap_size_min":93.184,"rst_0":12,"rst_1":12,"http_server_state":5,"http_server_counter":42,"wifi_rssi":-78.200088500976562},"temperature":24.15,"relative_humidity":43.72,"absolute_humidity":9.57,"dew_point":11.03,"CO2":463.69,"atm_pressure":1003.56,"ambient_light":20.88,"VOC_index":42.08,"VOC_equiv_CO2":529.84,"VOC_accuracy":1}

Even I did a lot of reading about MQTT autodiscovery and al lot of related stuff I am absolutely unsure how to go from here.

I would guess I have to configure it somehow in configuration.yaml. Regarding to read the json data correctly as shown here.

sensor:
    platform: mqtt
    state_topic: 'hass/sensor/schlafzimmer/luftsensor'
    name: 'temp_sz'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.temperature }}'   

In the logs I see:



Logger: homeassistant.components.sensor
Source: components/mqtt/__init__.py:368
Integration: Sensor (documentation, issues)
First occurred: 09:50:55 (8 occurrences)
Last logged: 09:51:18

    Error adding entities for domain sensor with platform mqtt
    Error while setting up mqtt platform for sensor

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 382, in async_add_entities
    await asyncio.gather(*tasks)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 587, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 697, in add_to_platform_finish
    await self.async_added_to_hass()
  File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 588, in async_added_to_hass
    await self._subscribe_topics()
  File "/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py", line 249, in _subscribe_topics
    self._sub_state = await subscription.async_subscribe_topics(
  File "/usr/src/homeassistant/homeassistant/components/mqtt/subscription.py", line 93, in async_subscribe_topics
    await requested.resubscribe_if_necessary(hass, current)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/subscription.py", line 48, in resubscribe_if_necessary
    self.unsubscribe_callback = await mqtt.async_subscribe(
  File "/usr/src/homeassistant/homeassistant/components/mqtt/__init__.py", line 368, in async_subscribe
    async_remove = await hass.data[DATA_MQTT].async_subscribe(
KeyError: 'mqtt'

Anyone having a clue what is wrong here? And yes, I tried to use the documentation but it does not explain how to add such a sensor at all :frowning:

I guess I have to create some sort of mapping. But hoe?

Thanks

/KNEBB

Did you setup the mqtt integration as well? This is used by home assistant to connect to the mqtt broker.
One part is the broker and the other part is the integration, you need to configure both.

2 Likes

Looks like this has been the issue, indeed!

I have now my sensor on my dashboard.

Thanks for pointing this out, it has not been clear.

EDIT/ UPDATE:

Forget the below. When publishing for testing purposes I did some bad double quoting so the message never came through properly (used -m "{"lhzr":99]" instead of -m '{"lhzr":99}'.

A follow-up question arises:

Meanwhile I moved the topic to my “real” MQTT erver by pointing the MQTT integration to this server. Thus, I was able to remove the MQTT from HomeAssistant Raspberry. Still working :star_struck:
The sensors all appear in a single deck on the Dashboard and they really look nice.

Now I want to have another sensor which publishes to a different topic. How do I configure this in the yaml-file?

sensor 0:
  - platform: mqtt
    state_topic: 'hass/sensor/schlafzimmer/luftsensor'
    name: 'OG Schlafzimmer Sensor Temperatur'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.temperature }}'   
  - platform: mqtt
    state_topic: 'zentrale/sensor/heizung'
    name: 'Keller TEST'
    unit_of_measurement: '%'
    value_template: '{{ value_json.lhzr }}' 

When doing this way and publishing a message: {"lhzr":99} brings an error in HA:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1496
First occurred: 13:18:29 (14 occurrences)
Last logged: 13:29:45
    Template variable error: 'value_json' is undefined when rendering '{{ value_json.lhzr }}'
    Template variable error: 'value_json' is undefined when rendering '{{ value_json.temperature }}'
    Template variable error: 'value_json' is undefined when rendering '{{ value_json.CO2 }}'
    Template variable error: 'value_json' is undefined when rendering '{{ value_json.relative_humidity }}'
    Template variable error: 'value_json' is undefined when rendering '{{ value_json.atm_pressure }}'

How do I configure now additional sensors in additional topics?

Thanks in advance again!

/KNEBB

You should put all sensors under the sensor: section instead of creating a new section sensor 0:. If you create new sections like the one you have, then you need to create one for each sensor as far as I remember.

1 Like

Hi,

well, I am still struggling here. I did not find any well documented site.
Even some example I found on the web do not work. I do not know why :frowning:

This is configuration.yaml:

sensor:
    platform: mqtt
    state_topic: 'hass/sensor/schlafzimmer/luftsensor'
    name: 'temp_sz'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.temperature }}'
    platform: mqtt
    state_topic: 'hass/sensor/schlafzimmer/luftsensor'
    name: 'co2_sz'
    unit_of_measurement: '°C'
    value_template: '{{ value_json.co2}}'

The file editor shows me “bad indentation”. How should the syntax look like? Whitespaces? Tabs? “-”?

Any idea how to add additional sensors here?

Thanks a lot!

/KNEBB

before every platform: mqtt

Well, does not help:

sensor:
  -platform: mqtt
   state_topic: 'hass/sensor/schlafzimmer/luftsensor'
   name: 'temp_sz'
   unit_of_measurement: '°C'
   value_template: '{{ value_json.temperature }}'
  -platform: mqtt
   state_topic: 'hass/sensor/schlafzimmer/luftsensor'
   name: 'co2_sz'
   unit_of_measurement: 'ppm'
   value_template: '{{ value_json.co2}}'

Result (line 23 is the first “state_topic” line):

bad indentation of a mapping entry at line 23, column 15:
       state_topic: 'hass/sensor/schlafzimmer/luft ... 
                  ^

Ok, bad indentation. So put only two spaces before these entries as follows:

sensor:
  -platform: mqtt
  state_topic: 'hass/sensor/schlafzimmer/luftsensor'
  name: 'temp_sz'
  unit_of_measurement: '°C'
  value_template: '{{ value_json.temperature }}'
  -platform: mqtt
  state_topic: 'hass/sensor/schlafzimmer/luftsensor'
  name: 'co2_sz'
  unit_of_measurement: 'ppm'
  value_template: '{{ value_json.co2}}'

Now I a m getting:

duplicated mapping key at line 27, column 3:
      -platform: mqtt
      ^

Removing the second platform line complains the about the following line being duplicate :hot_face:

Am I rellay to dumb to understand the syntax here?

Someone having a working example?

Thanks

/KNEBB

sensor:
    - platform: mqtt
      state_topic: 'hass/sensor/schlafzimmer/luftsensor'
      name: 'temp_sz'
      unit_of_measurement: '°C'
      value_template: '{{ value_json.temperature }}'
    - platform: mqtt
      state_topic: 'hass/sensor/schlafzimmer/luftsensor'
      name: 'co2_sz'
      unit_of_measurement: '°C'
      value_template: '{{ value_json.co2}}'
1 Like

Thanks- indeed it looks like I have been to dumb. I did not have a space after the “-” in the platform lines… now the editor does not complain. And I have both devices in my dashboard. Now they both pick up their values through mqtt.

Thanks again!