Getting aqara door sensor to work with zigbee2mqtt

Hello
I’m about to integrate my Aqara door sensor correctly into HomeAssistant and can’t get any further. I don’t have a Xiaomi bridge but a flashed CC2531 USB stick and use zigbee2mqtt.
Maybe to mention: This is my first setup with HomeAssistant, MQTT and Zigbee. So if you have “stupid” questions, please understand.

Current status:

  • HomeAssistant set up v0.89.0
  • Mosquitto Broker Addon v4 set up and configured
  • zigbee2mqtt v1.1.1 installed and set up
  • Aqara door sensor paired with USB stick

In the zigbee2mqtt log I can already see the payloads arriving when I move the sensor:

3/7/2019, 11:24:17 PM - info: MQTT publish: topic 'zigbee2mqtt/DoorTest', payload '{"contact":false,"linkquality":63,"battery":100,"voltage":3005}'
3/7/2019, 11:24:18 PM - info: MQTT publish: topic 'zigbee2mqtt/DoorTest', payload '{"contact":true,"linkquality":60,"battery":100,"voltage":3005}'

The goal is the following:
I want to create an automation that switches on the light in the entrance when the front door opens and the sun has set and has not risen yet.

Problem:
Now I have (understanding) problems to configure the sensor in HomeAssistant. How is the payload read out?
In the configuration.yaml I have defined the following sensor:

# Sensors
sensor:
  - platform: mqtt
    name: "DoorTest"
    state_topic: "zigbee2mqtt/DoorTest"

In the HomeAssistant under States I see the sensor as follows:

Now I only need the information about the “contact” for my application purpose. How do I have to configure the sensor to get this value? Or is this correct so that all values are received and then I read out the corresponding value in the automation? And if so, how is a corresponding value read out?

I hope this is explained in an understandable way.

Thanks for the help in advance,

Dave

You need to extract the state from the payload:

# Sensors
sensor:
  - platform: mqtt
    name: "DoorTest"
    state_topic: "zigbee2mqtt/DoorTest"
    value_template: "{{ value_json.contact}}"
1 Like

Thank you that worked :+1:
Is it possible to overwrite the value with an if else query? So if true then closed if false then open?

# Sensors
sensor:
  - platform: mqtt
    name: "DoorTest"
    state_topic: "zigbee2mqtt/DoorTest"
    value_template: "{% if value_json.contact == 'true' %}Closed{% else %}Open{% endif %}"

Thank you. But i have a problem now:
I changed the value_template of the sensor and restarted homeassistant. The value of the sensor now was unknown. So i opened and closed the door. But now the sensor stays always on the “Open” state. In the zigbee2mqtt log i can see that the sensor reported both events (false then true) but in Homeassistant the state does not change anymore. Any idea whats wrong here?

see if this works better:

# Sensors
sensor:
  - platform: mqtt
    name: "DoorTest"
    state_topic: "zigbee2mqtt/DoorTest"
    value_template: "{% if value_json.contact %}Closed{% else %}Open{% endif %}"

Okay, it works now. I don’t understand how it works without checking for true or false, but it’s more my bad programming skills :sweat_smile:. Anyway, thanks for your help.

1 Like