LowPowerLab Forum

Software support => Pi Gateway => Topic started by: Nspector5 on September 16, 2016, 01:02:35 PM

Title: Problem printing out logged event time
Post by: Nspector5 on September 16, 2016, 01:02:35 PM
I am currently monitoring the daily high and low temperature and logging the time when a new high or low is reached. Twice a day, I will tweet out the daily high or overnight low. I am currently having a problem saving, and then outputting the date information. My setup is as follows:

Variables in metric.js:
var dailyhightemp = 0;
var dailyhightime = new Date();
var dailylowtemp = 100;
var dailylowtime = new Date();


Function to monitor the temperature:
MonitorOvernightLow : { label:'Monitor Overnight Low', icon:'fa-heartbeat', descr:'Monitor the overnight low temperature',
serverExecute:function(node) {
if (node.metrics['Outside'] && node.metrics['Outside'].value < dailylowtemp && (Date.now() - new Date(node.metrics['Outside'].updated).getTime() < 2000))
      {
dailylowtemp = node.metrics['Outside'].value;
dailylowtime = Date.now();
console.log('New Daily Low set:'+dailylowtemp+''); 
}
},
  },


And the function to tweet the temperature:
TweetOvernightLow : { label:'Tweet Overnight Low', icon:'alert', descr:'Send tweet of the overnight low temperature',
nextSchedule:function(node) { return exports.timeoutOffset(8,0); }, //ie 8:00 (8am)
        scheduledExecute:function(node) {
sendTweet('It got down to'+dailylowtemp+'deg @ ' + (dailylowtime.toLocaleTimeString() + (dailylowtime.getHours() > 12 ? 'PM':'AM'))); 
dailylowtemp = 100;
}
  },


The problem comes in that the dailylowtime.toLocaleTimeString() section, in that I get an error stating that dailylowtime does not have toLocaleTimeString().

Any ideas on how to resolve this?

Also, I'd like to combine the monitor and sendtweet functions, but I have not figured out how to do that either.

Thank you.
Title: Re: Problem printing out logged event time
Post by: Felix on September 16, 2016, 01:32:00 PM
Your dailylowtime.toLocaleTimeString() won't work because the scope of the declared variable is not what you probably assume. So when your code executes dailylowtime is null instead of a date object.
Although the metrics.js is a separate file, the scope is that from gateway.js since that is where the events are scheduled. So a band aid fix might be to move the declarations in gateway.js but I haven't verified this. I would not rely on global vars for what you want to do. Also scheduled events encapsulate the scope they were declared in. So an event 1 hour later will have the scope of when it was scheduled, so it could be stale. For this reason I would always "get-latest" of whatever you are trying to check. If you want to save variables there's a good example here (https://lowpowerlab.com/forum/pi-gateway/scheduled-events/msg15157/#msg15157) on how to do that in an event.

You could combine these but you have to decide how/when you want your hi/low values to be determined. Right now you have an event that fires on every message (the one using serverExecute) and you are trying to set min/max variables in metrics.js (although this won't work as you have it now - see above link for saving those values in the neDB node database instead which would keep them safe in case of a crash). The other uses scheduledExecute to run twice daily and just tweet the saved values.

To combine the two you could write a custom function somewhere to determine the daily hi/low from the past day worth of data, then just call that in your MonitorOvernightLow.
The node metric logs are handled by functions in logutil.js (https://github.com/LowPowerLab/RaspberryPi-Gateway/blob/master/logUtil.js), take a look in there to see how you might do that. But I would just save the values in the db, then you only need the scheduled event to read and tweet them.