I don't want to wait until the device publish it's new readings on it's own because is too slow.
More specifically I want to poll the state of an aqara motion sensor to get the value of the luminosity of the room on demand.
If I use the current state node, it will just return the last reported value. The problem is that if the sensor doesn't get triggered by movement, the luminosity value will only get updated and report each hour or so. So the value I get is not a realtime reading.
For example if you physically press the little button for pairing, briefly, all the sensor states will get updated and you will get fresh updated values each time you press it.
I know it is possible to similarly poll the sensor by software on demand because the poll state node is able to poll the sensor to get fresh readings each time, and you can set the frequency as short as you want. The problem is that the poll state node can't be dynamically modified to stop, it polls infinitely by the frequency you set it to. And this might drain the battery very quickly if set it to poll too often.
Ideally what I would need is to have a similar node that can be run only once when it receives a message and polls the device.
Do you know how to achieve that? maybe using a JavaScript node?
I thought of the possibility of cloning the code of the poll state node and modify it to create this variant but I am a newbie and that is way too advanced for me.
I was also looking into the possibility of putting the poll state in a separate flow and enable and disable the flow dynamically but I couldn't find info on how to do that.
(for info I am using zigbee2mqtt on home assistant on a raspberry pi )
to get the value of the luminosity of the room on demand.
Well, the question is: Does this aqara motion sensor support being asked it's current state?
The problem is that if the sensor doesn't get triggered by movement
RED FLAG for me with that.
It's a motion sensor that doesn't get triggered by motion.
I know it is possible to similarly poll the sensor by software on demand because the poll state node is able to poll the sensor to get fresh readings each time, and you can set the frequency as short as you want. The problem is that the poll state node can't be dynamically modified to stop, it polls infinitely by the frequency you set it to. And this might drain the battery very quickly if set it to poll too often.
Well, that's confusing to me.
The poll state node can poll the sensor to get fresh readings each time......
So do that, get the reading then set to poll time back to a large value.
it does because the poll state node is able to do it.
no red flags, what I meant to say is that if the sensor gets triggered by motion, it immediately updates all it's values, including the luminosity. If there is no motion, the sensor will update it's values each hour or so.
unfortunately you can't change the poll time dynamically at runtime
I don't know about this particular device, but in zigbee2MQTT you can often control when the device sends new data by defining a change in value that will trigger a send. So for the luminosity you could set the luminosity change that would cause it to send new data. Then you would know that the value you currently have is within a certain range of the actual value.
If it is the Aquara RTCGQ11LM Motion Sensor (or Xiaomi RTCGQ11LM in zigbee2mqtt) it cannot be polled. As far as I am aware none of the Aquara battery devices can.
From the zigbee2mqtt device data page
Exposes
#Battery (numeric)
Remaining battery in %. Value can be found in the published state on the battery property. It's not possible to read (/get) or write (/set) this value. The minimal value is 0 and the maximum value is 100. The unit of this value is %.
#Occupancy (binary)
Indicates whether the device detected occupancy. Value can be found in the published state on the occupancy property. It's not possible to read (/get) or write (/set) this value. If value equals true occupancy is ON, if false OFF.
#Device_temperature (numeric)
Temperature of the device. Value can be found in the published state on the device_temperature property. It's not possible to read (/get) or write (/set) this value. The unit of this value is °C.
#Voltage (numeric)
Voltage of the battery in millivolts. Value can be found in the published state on the voltage property. It's not possible to read (/get) or write (/set) this value. The unit of this value is mV.
#Illuminance_lux (numeric)
Measured illuminance in lux. Value can be found in the published state on the illuminance property. It's not possible to read (/get) or write (/set) this value. The unit of this value is lx.
#Illuminance (numeric)
Measured illuminance in lux. Value can be found in the published state on the illuminance property. It's not possible to read (/get) or write (/set) this value. The unit of this value is lx.
#Power_outage_count (numeric)
Number of power outages. Value can be found in the published state on the power_outage_count property. It's not possible to read (/get) or write (/set) this value.
#Linkquality (numeric)
Link quality (signal strength). Value can be found in the published state on the linkquality property. It's not possible to read (/get) or write (/set) this value. The minimal value is 0 and the maximum value is 255. The unit of this value is lqi.
Very interesting info, thanks for sharing. Although it says you can't read the values, I can confirm that is possible to poll them on demand (don't know for sure if read and poll means the same in this case) because I tested using the poll state node with frequency of 1 sec and then closing and opening the curtains and it gave fresh values each second
(I don't know if it's the actual code used in home assistant but it seems to be)
I hope it's ok to share the link here. Otherwise I will remove this post.
If you scroll to the bottom, I think this part of the code is doing the polling:
// Try to fetch from cache, if not found then try and pull fresh
async getState(entityId) {
let state = await this.nodeConfig.server.homeAssistant.getStates(this.nodeConfig.entity_id);
if (!state) {
state = await this.nodeConfig.server.homeAssistant.getStates(this.nodeConfig.entity_id, true);
}
return state;
}
Could anyone deduce from this code how to do a poll using a JavaScript node?
I advise against using that node. Just use MQTT nodes to get the value directly.
@Anduvo you said you said you are using zigbee2MQTT, but I thought that HA had its own zigbee plugin. I may well be wrong there though.
If you tell the poll state node to poll it rapidly, and then pick up the value directly from MQTT (either in node-red or in MQTTExplorer for example) does it come in more rapidly?
Ok thanks. I tried to set it up to report the illuminance each 5 seconds (to test) but each time I hit apply, I get an error that it couldn't update the device.
Update on this topic.
I recently purchased another aqara motion sensor P1 and to my surprise this one is an upgraded version of the original one. This one is the model RTCGQ14LM. If I am not wrong they released it about 6 months ago.
This one allows you to set the interval of detection from 2 to 65535 secs.
You can configure the sensitivity.
You can configure if you want the blue light to flash or not when motion is detected.
And now it comes with 2 batteries CR2450. Instead of only one with the previous model.
The downside is that to change those values you have to press the pairing button first, so I don't think that they can be changed dynamically.
Anyways, it is already a big improvement. I set the interval to 2 secs and I will se how long the battery last. With default settings which is detection interval of 30s they say it could last around 5 years.