Display time since last zigbee2mqtt recieved

Hi
I am using the zigbee to mqtt admin panel in node red, to manage all my zigbee devices. This is a really good flow that displays all the infomation from zigbee devices and modify them. But one thing that would be useful thats not on the device status list is how long since the zigbee device was last heard from. If you look at the network map this infomation is available, but not on the general panel. so i thought i would add it.
In the function that formats the data to be displayed in a ui-list node i added the following:-
newPayload.push(newentry);
newentry = {
'title': 'Last Seen',
'description':''+ msg.payload.lastSeen,
'icon_name': 'timer'
};
this displays the data as timestamp, but i need to subtract this number from the now timestamp and convert it to hours, mins, seconds. to display the time elapsed since the device was last seen.
Is there a simple solution for this, i have tried Date(), toMillis etc, but can't figure it out.
Chris.
Now got this:
'description':''+ (Date.parse(Date())-msg.payload.lastSeen/1000),
Solved it ! A bit messy but it works.,,,,
//Get hours from milliseconds
var hours = (Date.parse(Date())-msg.payload.lastSeen)/ (10006060);
var absoluteHours = Math.floor(hours);
var h = absoluteHours > 9 ? absoluteHours : '0' + absoluteHours;

//Get remainder from hours and convert to minutes
var minutes = (hours - absoluteHours) * 60;
var absoluteMinutes = Math.floor(minutes);
var m = absoluteMinutes > 9 ? absoluteMinutes : '0' + absoluteMinutes;

//Get remainder from minutes and convert to seconds
var seconds = (minutes - absoluteMinutes) * 60;
var absoluteSeconds = Math.floor(seconds);
var s = absoluteSeconds > 9 ? absoluteSeconds : '0' + absoluteSeconds;
msg.payload.last = h + " Hours, " + m +" Minutes and " + s +" Seconds ago";
newPayload.push(newentry);
newentry = {
'title': 'Last Seen',
'description':''+ msg.payload.last,
'icon_name': 'timer'
};

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.