Is there a way to read the last activity of a device in Node Red?
Thanks
Could you describe what you would like to do in more detail? In general, device state indicates it's last activity. For devices with multiple capabilities/attributes, you can have all device changes saved to a database in NR, and read from that database.
Are you referring to the Last Activity date/time in the table displayed at the bottom of the device details page? I can't say for certain, but it may be worth checking what you can get via Maker API.
Taking @sburke781 's pointer towards Maker API, and assuming I'm guessing correctly just what the OP is trying to do, you should be able to accomplish this using the MakerAPI "Get Device Event History" URL.
Get Device Event History (replace [Device ID] with actual subscribed device id
http://hub_ip/apps/api/##/devices/[Device ID]/events?access_token=#############
When I pointed that URL at one of my lights, device #292 I get:
[{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"98","date":"2023-12-01T21:14:09+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"1","date":"2023-12-01T21:14:04+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"switch","value":"on","date":"2023-12-01T21:14:04+0000","unit":null,"isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-setColorTemperature","value":null,"date":"2023-12-01T21:14:04+0000","unit":null,"isStateChange":false,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"switch","value":"off","date":"2023-12-01T21:12:28+0000","unit":null,"isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-off","value":null,"date":"2023-12-01T21:12:27+0000","unit":null,"isStateChange":false,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"10","date":"2023-12-01T21:11:32+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"97","date":"2023-12-01T21:11:27+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-setColorTemperature","value":null,"date":"2023-12-01T21:11:27+0000","unit":null,"isStateChange":false,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"98","date":"2023-12-01T20:33:03+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"1","date":"2023-12-01T20:32:58+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"switch","value":"on","date":"2023-12-01T20:32:58+0000","unit":null,"isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-setColorTemperature","value":null,"date":"2023-12-01T20:32:58+0000","unit":null,"isStateChange":false,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"switch","value":"off","date":"2023-12-01T20:31:40+0000","unit":null,"isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-off","value":null,"date":"2023-12-01T20:31:39+0000","unit":null,"isStateChange":false,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"10","date":"2023-12-01T20:30:44+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"97","date":"2023-12-01T20:30:39+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-setColorTemperature","value":null,"date":"2023-12-01T20:30:39+0000","unit":null,"isStateChange":false,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"level","value":"98","date":"2023-12-01T20:11:01+0000","unit":"%","isStateChange":true,"source":"DEVICE"},{"device_id":"292","label":"Office Lights","room":"Office","name":"command-setColorTemperature","value":null,"date":"2023-12-01T20:10:56+0000","unit":null,"isStateChange":false,"source":"DEVICE"}]
The most recent event appears to be the first event returned, and the timestamp on it is: 2023-12-01T21:14:09+0000
The following will work in Node Red to get that timestamp:
The inject node just injects a timestamp as message payload to trigger the flow.
The http request node points to the URL for MakerAPI Get Device Event History. In the drop-down box for "Return" select "a parsed JSON object"
In the change node, set the payload to msg.payload[0].date
The resulting payload in the debug node is:
Hopefully we're right....
Yes, Devices page, last activity.
I have some Zooz zsc44’s that report 1% battery when they are still working.
I also need to start looking for devices that fall off the network.
Getting the info from Maker API looks like it will work well.
Thanks.
If you open up browser console network tab and view the device list you will find a nice JSON api that lists all devices including the last activity timestamp - basically JSON of all the device list column data. Of course this is an undocumented API and could break with a future release but I use it in NodeRed to feed a MariaDB table of device data for monitoring.
@Alan_F thanks that works perfectly!
Sorry I have been distracted with the holiday and playing with HueMagc in Node Red.
Now I need to come up with some clever flow to track the age of events vs the device's history.