Notification when Aqara binary senors went offline

Hey guys!
First of all I have to confess, I'm a noob :slight_smile: I get things working most of the time but if it comes to coding it's over for me :slight_smile:

What I'm trying to do is a "watchdog" for my Zigbee Sensors (Aqara Door/Window Sensors). I thought about a Timer which activates every 10 minutes the phoscon get node for the specific sensor. The node provides then the timestamp-value for "last updated".
As a next step I thought about a function node which compares the "last updated" timestamp with the current time, and if the time difference is higher than one or two hours it notifies me via my Telegram node or sth.
BTW: Do I need to do some timezone corrections? It seems to me that the timezone is not as I initially adjusted it in my HomeAssistant / NodeRed confguration.

Does this approach make sense to you and if so, can you help me with the function node?

Thank you in Advance

If you have a point in your flows where you have regular messages coming in and want to detect the fact that no messages arrive within a given time, then connect them to a Trigger node, set to Send Nothing then after 60 minutes (or whatever) send the timeout message. Set Extend Delay on new message and that will mean that it keeps restarting the timer every time a new message comes in. If no messages arrive for 60 minutes then it will send the message.
If you want it to fire 60 minutes after a reboot or node-red restart if no data comes in then also connect an inject node set to inject once on startup and that will set the timer going immediately.


Hay Thank you Colin! Unfortunately I have to "request" the sensors. And then I have to examine the payload for the "last updated" state. The sensors don't fire any payload as far as I see if not requested actively or if no state change happens.


Add a debug node and show us what comes back from the sensor.

Here you are:


Is that a UTC date/time or local timezone?

I guess it's a UTC Time, but I don't know why. I have my local Time defined everywhere...

Good, you would be in trouble if it was local time. What would happen when there was a DST change for example. Have a look at the JavaScript Date type. Probably you can feed that string straight into the Date constructor to give you a JavaScript Date, which you can then then subtract from the current time to give you how long ago it was. Start with a function node and see if you conduct the date object, put that in the message payload for the moment and feed it into a debug node to see what it says.

Sorry but I have no clue what the function has to look like. Can you help me?

Thank you!

First do you know how to access the lastupdated timestamp in a function node? If not then read the docs page Working with Messages and see if you can work out how to get at the timestamp.
Once you have done that then create a Date object from it and pass it out to go to a debug node to see what it shows. You will need something like

let lastupdated = new Date(....)
msg.payload = lastupdated
return msg

Replace the ... with the reference to the lastupdated string from the data coming in, that you have worked out from the Working with Messages page.

[Edit] What node is that timestamp coming from (possibly node-red-contrib-somehing)? The timestamp should really have a Z on the end to show it is UTC, so whatever is providing the time is doing it in a non-standard way

The Time is coming from the Phoscon get Node. There is no Z but it is UTC.

I can't find any reference to a phoscon node on the node red flows site, can you post a link to some information on it?

Yes of course, you have to search for:


Thank you :slight_smile:

I use several aqara door /windows sensors together with a zigbee stick with a zigbee2mqtt firmware and I'm sure, that the aqara sensors send status informations once in a while even if the status does not change.