Author Topic: example UserMetrics events timing  (Read 137 times)

gigawatts

  • Newbie
  • *
  • Posts: 15
example UserMetrics events timing
« on: March 03, 2020, 03:37:02 PM »
I am trying to create a custom event that triggers once per month.

I have looked through some of the examples in https://github.com/LowPowerLab/RaspberryPi-Gateway/blob/master/userMetrics/

Is this the cleanest way to trigger a monthly message to a node? The event itself will run every day at the specified time, but will only send a message on the 1st of the month?

Code: [Select]
exports.events = {
  resetWaterGal_monthly : {
    label:'WaterMote: Reset Gallons Total metric',
    icon:'clock',
    descr:'Runs @ midnight on the first day of every month',
    nextSchedule:function(node) { return exports.timeoutOffset(0,00); },
    scheduledExecute:function(node) {
      if (new Date().getDate()==1) sendMessageToNode({nodeId:node._id, action:'GALRST'});
    }
  },

};

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6279
  • Country: us
    • LowPowerLab
Re: example UserMetrics events timing
« Reply #1 on: March 04, 2020, 02:25:25 PM »
Just look in the metrics.js examples. And use more readable macros that are provided. For instance without getting fancy, you could do it in 30 day intervals:

Code: [Select]
resetWaterGal_monthly : {
  label:'WaterMote: Reset Gallons Total metric',
  icon:'clock',
  descr:'Runs @ midnight on the first day of every month',
  nextSchedule:function(node) {
    return exports.millisToFutureDate(exports.ONEDAY*30); },
  scheduledExecute:function(node) {
    sendMessageToNode({nodeId:node._id, action:'GALRST'});
    }
},