How to declare temperature sensors?

I have a few Xiaomi temperature/humidity sensors. They work on the Zigbee protocol and connect to zigbee2mqtt which pushes to MQTT

  • their “declaration” for autodiscovery
  • their status (temperature or humidity)

It happens sometimes, though, that the devices simply vanish from HA, which states that they are unknown (in yellow in the UI). zigbee2mqtt is still sending information to the MQTT broker so form that side everything works, the “vanishing” is on HA side.

I think (wildly speculating) that HA loses access to the autodiscovery information (which is supposed to be retained on the broker).

One fix for that would be to get rid of the autodiscovery and manually declare the sensors in configuration.yaml. My question is: what to actually put in the declaration?

Autodiscovery provides the following information:

3/7/2019, 3:11:43 PM - info: MQTT publish: topic 'homeassistant/sensor/0x00158d0002c77517/temperature/config', payload '{"unit_of_measurement":"°C","device_class":"temperature","value_template":"{{ value_json.temperature }}","state_topic":"zigbee2mqtt/0x00158d0002c77517","json_attributes_topic":"zigbee2mqtt/0x00158d0002c77517","name":"0x00158d0002c77517_temperature","unique_id":"0x00158d0002c77517_temperature_zigbee2mqtt","device":{"identifiers":"zigbee2mqtt_0x00158d0002c77517","name":"0x00158d0002c77517","sw_version":"Zigbee2mqtt 1.1.1","model":"Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)","manufacturer":"Xiaomi"},"availability_topic":"zigbee2mqtt/bridge/state"}'

The formatted JSON up there is:

{
  "unit_of_measurement": "°C",
  "device_class": "temperature",
  "value_template": "{{ value_json.temperature }}",
  "state_topic": "zigbee2mqtt/0x00158d0002c77517",
  "json_attributes_topic": "zigbee2mqtt/0x00158d0002c77517",
  "name": "0x00158d0002c77517_temperature",
  "unique_id": "0x00158d0002c77517_temperature_zigbee2mqtt",
  "device": {
    "identifiers": "zigbee2mqtt_0x00158d0002c77517",
    "name": "0x00158d0002c77517",
    "sw_version": "Zigbee2mqtt 1.1.1",
    "model": "Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)",
    "manufacturer": "Xiaomi"
  },
  "availability_topic": "zigbee2mqtt/bridge/state"
}

What should the declaration of this sensor look like in HA? Should all the fields be translated to YAML such as below?

sensor:
    -   unit_of_measurement: "°C"
        device_class: temperature
        value_template: "{{ value_json.temperature }}"
        state_topic: zigbee2mqtt/0x00158d0002c77517
        json_attributes_topic: zigbee2mqtt/0x00158d0002c77517
        name: 0x00158d0002c77517_temperature
        unique_id: 0x00158d0002c77517_temperature_zigbee2mqtt
        device:
          identifiers: zigbee2mqtt_0x00158d0002c77517
          name: '0x00158d0002c77517'
          sw_version: Zigbee2mqtt 1.1.1
          model: Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)
          manufacturer: Xiaomi
        availability_topic: zigbee2mqtt/bridge/state

this will give you the list of supported variables:

I’d personally start with the bare minimum to see what HA needs to work correctly:

sensor:
  - platform: mqtt
    name: "0x00158d0002c77517_temperature"
    state_topic: "zigbee2mqtt/0x00158d0002c77517"
    unit_of_measurement: '°C'
    value_template: "{{ value_json.temperature }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "temperature"

it happens me too, in my case is zigbee2mqtt that lose the device.

i’m trying this: add a watchdog sensor:

  • platform: mqtt
    name: “Monitor Aqara 0x00158d0003499e75”
    state_topic: “zigbee2mqtt/0x00158d0003499e75”
    value_template: “{{ now() }}”

so i have a new sensors with the last timestamp that the temperature is read ( otherwise there is no understandable difference with a constant temperature … )

now i’m trying to understand the problem, if i rejoin the device it’s ok, now i’ll try to restart zigbee2mqtt service and so on… at the end i’l create an automation to detect and fix

Entities have a last_updated property. If you have a sensor like this:

sensor.aqara0x00158d0003499e75

you can access its last_updated property like this:

{{ states.sensor.aqara0x00158d0003499e75.last_updated }}

For example, I have a temperature sensor and here’s how it looks in the Template Editor: