Script - Mimic No Update State
-
I'd like to create a meta data point which mimics the No Update alarm state found within event detectors.
What's the best way to check whether a data point has updated within the last X minutes?
Essentially I'd like a point to have a value of 1, if no update is true, and value of 0 if false.
Would it to work compare times?
var alarmTime = 10; //set the amount of time in minutes before a no update "alarm" is triggered var alarm = 0; //set alarm value to 0 var timeNow = new Date(); //get time now var lastUpdate = new Date(point.time); //last point time value var diffMs = (timeNow - lastUpdate); // milliseconds between now & last point time value var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // convert to minutes if (alarmTime > diffMins) { return alarm = 1; } else return alarm = 0; }
And I would run this as cron every minute like this --> 0 * * * * ?
Did I make any mistakes?
-
I do not know why you are doing the double modulo for what appears to be days and hours. diffMins would be diffMs/60000 by itself. Also, be wary, JavaScript % operator is not a modulo that respects returning positive values only. Lots of people write their own modulo functions like
function mod(number, modulo) { return ((number % modulo) + modulo) % modulo;
To get around that problem. Also, it would seem to me you could have us do all the work for this with...
var data = p.pointValuesSince(new Date().getTime() - 10*60*1000) //ten minutes in milliseconds if( data.length == 0 ) return 1 return 0
Your logic should be fine (provided the samples aren't exactly a day apart, because of those modulo operations)
-
Haha, no I am not interested in that logic I half baked with my google fu.
Your code is so much nicer, I'll use that instead.
-
Hey Phil,
I don't think I tested your code last time, and now I'm realizing the code doesn't seem to be working. I'm always getting a return to zero, even though the boiler is OFF and cannot send data to update the time stamp.
Any thoughts?
-
Do you have the latest version of the Meta module installed?
-
Looks like it: Meta 2.2.4.
-
Hmm. You could try adding a print(data); after the first line, see what the statistics object looks like. Is the boiler on interval logging?
-
The point I am monitoring has a logging type: "When point value changes".
I have set up a No Update event notification, on that same point, to email when triggered, and that does work.
The data prints out as "0.0"
-
"print(data)" should print out an object, like {"count":0, "average":null, firstValue:PointValueTime@ABCDEF, lastValue:.... }
-
Oh, in that case I don't know where it print outs to. I tried looking around... I have no idea, sorry.
-
It would be right around where you are looking, above the return value. I would think the 0.0 you're seeing is the return value. Some of them have issues with the printstream getting eaten... you could try another browser perhaps. Or, you can just return data.toString() which would work to get its value returned in the error message saying it cannot be cast to a numeric.
-
Here ya go.
PS: I was using Chrome.
-
It looks like it's still reading values! Also I got my expectation wrong, that's just a list of PointValueTimes! I was thinking about the structure of statistics objects, like one would get from p.past()
i betcha my mistake is testing 'data.count == 0' instead of 'data.length == 0'
-
Replacing data.count == 0 with data.length == 0 now works.
Thanks for your help Phil.