Change device settings from Node Red?

The Aqara PIR sensor has a “occupancy_timeout” setting exposed via Z2M that I want to change from inside of Node Red. Is this possible, and if so… how?

More info (reasoning behind question):
As I live alone, I currently have Node Red monitoring which room I’m in (from the last “occupancy” detected). It stores this in a Flow variable and then turns off lights, switches, etc in every other room apart from the one that I’m in… and turns on the lights in the occupied room if needed.

The issue with long timeout values is that if I walk into one room for a split second and then walk back out… I have to wait for the timeouts to end before the room I left & then instantly re-entered will come on.

I got around this by setting the timeout to 1 second… but this causes lots of occupancy events which will reduce battery life.

What I want to be able to do, is when a PIR has detected me… set its timeout really long and set the other PIRs to have a short timeout (I’m storing the room in a Flow variable, so it doesn’t need constant occupancy updates from the device anyway… just the first one).

Do you have their, z2m, nodes installed? If it is possible, those will be the likely path.

I installed that palette and captured the “config” message being sent with the “bridge” node.

The only parts that change, when I change the config in Zigbee2MQTT are the “occupancy_timeout” value of a certain device (it sends configs for them all)… and the “_msgid” (seems to be a random 8-hex(0-255) in 00-FF notation).

I have tried to replay that config message via the “bridge” node… but nothing responds and the changes to timeout aren’t saved. I have also sent the same message with a different random “_msgid” in case duplicate IDs are discarded… still no change.

I don’t think the “bridge” node is sending the messages that I pass into it… so I also tried the “out” node but no matter what I target with the “out” node, I get the error responds “No converter available for ‘options’ ({“occupancy_timeout”:2})”.

I came across: MQTT Topics and Messages | Zigbee2MQTT
…so I tried sending a message out into the ether with the topic:
zigbee2mqtt/bridge/request/device/options
… containing the JSON:
{"id": "lr_pir_1", "options":{"occupancy_timeout":2}}
…but nothing responded and the change didn’t happen.

I’ve also tried sending that JSON to the controller and the PIR device using the “out”… but same error response.

Soooo close, I think… I just cant seem to figure out the correct way to change device settings from within Node Red.

Anyone got any ideas or know how to do it?
I think that I’m soooo close? Yes / No ?

Found that “out” from that palette has an “options” section that allows me to set “occupancy_timeout” … and it does actually change! Yay!

It also generates an error message “Incorrect payload. Waiting for valid JSON” though. This might be because I have set “Command” to “None”… I dunno.

I can change the timeout now… but its not “right”… as its generating that “incorrect payload” message all the time.

Any ideas?

Further progress! Final solution found!

In order to change the settings… I used the “out” node from the palette linked by Mikefila.

The “options” section at the bottom can be set to “occupancy_timeout” of any seconds.

I was using the “command” of “nothing”… but for some reason it sends a 41-character string (not sure whats in it).

Setting the “command” to string and the “payload” to number… both with nothing in them… works fine and doesn’t seem to send anything apart from the “options” message that changes the occupancy_timeout.

Using these means I can now dynamically change the occupancy_timeout of every PIR on the network.

I am now using a flow variable to store the room that I’m in… and set the PIR in that room to have a really long occupancy_timeout (to save battery).

If ANY other PIR see’s me… it changes the flow variable to say I’m in its room… sets itself to have a really long occupancy_timeout (to save battery)… and resets the last PIRs occupancy_timeout to 1 second… effectively resetting it to detect movement instantly (in case I change my mind and go straight back to the room that I came from).

This means the current room I’m in… will hardly every trigger “occupancy” because the flow variable already knows that I’m in that room… but any room that I’m not in… wont ignore me if I’ve just triggered the PIR while leaving the room… and then go straight back in after triggering another for a split-second :slight_smile:

1 Like