CUSTOM Binary Sensor with Xiaomi Aqara Motion Sensor (RTCGQ11LM) + Zigbee2MQTT

OK, I’m giving up on trying to figure this our on my own…

I have an Aqara motion sensor that’s connected to Zigbee2MQTT - it works flawlessly, BUT only has a 2 minute timer (I need much less). So I used this mod: https://community.smartthings.com/t/making-xiaomi-motion-sensor-a-super-motion-sensor/139806 now it actually reports motion every 5 seconds, BUT not if the motion stops under 2 minutes.

That’s why I would like to make a custom binary sensor

Zigbee2MQTT sends this mesage

zigbee2mqtt:info 6/17/2019, 8:33:06 AM MQTT publish: topic 'zigbee2mqtt/0x00158d000123456ab', payload '{"illuminance":141,"linkquality":31,"occupancy":false,"battery":100,"voltage":3065}'

An I tried to make a binary_sensor using this code:

  - platform: mqtt
    name: "Bewegungsmelder_02_x"
    state_topic: "zigbee2mqtt/0x00158d000123456ab"
    value_template: '{{ value_json.occupancy }}'
    payload_on: "true"
    off_delay: "6"
    qos: 1

It does not work…
What am I doing wrong?!

You shouldn’t need payload on or payload off. Just set the value_template properly.

  - platform: mqtt
    name: "Bewegungsmelder_02_x"
    state_topic: "zigbee2mqtt/0x00158d000123456ab"
    value_template: "{{ value_json.occupancy == 'true' }}"
    off_delay: 6
    qos: 1

That’s if the value comes back as a string from occupancy, if it’s a boolean then you just need.

  - platform: mqtt
    name: "Bewegungsmelder_02_x"
    state_topic: "zigbee2mqtt/0x00158d000123456ab"
    value_template: "{{ value_json.occupancy }}"
    off_delay: 6
    qos: 1

I tried your suggestions, but neither one is working :frowning:

whats not working? Also, what does the topic look like when you detect motion?

the binary sensor doesn’t report motion (but the motion sensor zigbee2mqtt creates, does detect motion)

What do you mean by that?

Is it this:

  zigbee2mqtt:info 6/17/2019, 4:17:43 PM MQTT publish: topic 'zigbee2mqtt/0x00158d000123456ab', payload '{"illuminance":78,"linkquality":28,"occupancy":true,"battery":100,"voltage":3065}'

you have the mqtt hub set up right?

I believe so - everything else works like it should :thinking:

However, I found an other solution - maybe even more elegant. It appears there’s the option to set the occupancy_timeout within Zigbee2MQTT (in share/zigbee2mqtt/configuration.yaml):

0x00158d000123456ab:
    friendly_name: '0x00158d000123456ab'
    retain: true
    occupancy_timeout: 6 # <---

This works exactly like I wanted it to

Still, thanks for your help!

But it’s weird that this custom binary_sensor doesn’t work - I tired to use mqtt messages for other sensors before and it never worked…

Yeah, but the only thing that wouldn’t work is if you aren’t connecting to the mqtt ‘hub’, which is in a different section of your config. The sensor had the correct information.

Doesn’t zigbee2mqtt just send the messages over the whole network?!

I connected it with the mosquitto broker with my mqtt credentials, like everything else (sonoffs for example)

It does, but if you use a mqtt sensor you need to add the mqtt hub component so that the sensor has an IP to look at

isn’t the

zigbee2mqtt

part in

zigbee2mqtt/0x00device0id0123

the “address” of the hub?!

I’m a little confused now

Iike I said, I believe zigbee2mqtt is set up correctly - every sensor/lamp/switch/whatever I add, work propperly. However, I never managed to “pull” variables out of the mqtt messages for custom sensors

Well - now I feel a bit stupid, but it’s all there, in the zigbee2mqtt documentation…

I have never realized, that you could click the devices in the “supported devices” list and get to a detailed documentation on how to configure them… :exploding_head:

Nope, that’s the name of the topic. The IP of your hub is wherever your mqtt broker is.

That got added recently I think

i just edited \share\zigbee2mqtt\devices.yaml and added “occupancy_timeout: 6” under my aqara motion sensor. it’s just works.

you could click the devices in the “supported devices” list and get to a detailed documentation on how to configure them… but you can’t find it now… Can you post it? Thank you