I'm trying to compare two timestamps and if it's greater than x seconds difference, indicate "offline". Here is what I have in the js editor of the widget:
// Example: Convert temp from C to F and truncate to 2 decimal places.
// return (datasources["MyDatasource"].sensor.tempInF * 1.8 + 32).toFixed(2);
console.log("Checking Time Difference")
var timediff = (new Date) - datasources["ConsentDS"].Timestamp
console.log(timediff)
if timediff > 1 * 60 * 1000 {
return 1
} else {
return 0
}
The indicator always stays "online" even when the difference should be greater than 30 seconds. It's not even writing to the console like I expect.
I can't find any documentation so I'm not even sure if I should be returning 1 or true or elephant :(
So most of my problem was javascript syntax, as @Donut and probably others noticed immediately.
Here is the working version:
var ts = new Date(datasources["ConsentDS"].Timestamp).getTime();
var ms = new Date().getTime();
var d = ms - ts;
if (d > 5 * 60 * 1000) {
return 0;
} else {
return 1;
}
If the current time minus the timestamp on the data is more than 30 seconds (30000 milliseconds) then it returns 0 which is the "OFF" status on the indicator widget.