• Hi
    I have started with this example as a base to modify for my needs:
    https://www.espruino.com/Smart+Meter

    I am measuring time between pulses to calculate current power use. But I also want to keep track of pulses per hour and per day. I am lost at math that is used in the example to modify. I dont need an array to store, as data will be picked up but another device and stored elsewhere.
    Can someone point me in the right direction/assist with javascript to detect a day/hour rollover, so that counters can be reset?
    Some pseudo-code =)

    var hourPulses=0
    var dayPulses=0
    For every pulse
       if hour > previous hour then hourPulses =0
       if day > previous day then dayPulses =0
       increment dayPulses
       increment hourPulses
       send NRF data
       loop
    

    Something like that. I just dont know how it will work when moving into a next day.
    The example in the link above somehow magically sets it into an array. But I just want one value to be sent for the current hour and day.

  • Mon 2021.07.12

    Hi @user130485

    re: 'Can someone point me in the right direction/assist with javascript to detect a day/hour rollover'

    While the collection and conversion of pulses code/concept is not shown, and knowing that the Javascript Date() function constructor is based on the number of milliseconds since 1 January 1970 UTC, it would make sense just to return the current time in msec and remain using msec, then extract the hours and minutes individually as needed, rather than a more obtuse method of trying to detect rollover.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

    Interactive live playground:

    https://www.w3schools.com/js/js_dates.asp

    https://www.codesdope.com/blog/article/how-to-create-a-digital-clock-using-javascript/

  • I have another reading that shows current watts based on time between the pulses.
    But I wanted to keep track of the total pulses for the day and actual kW use per hour. But for this I need to know when the current hour or day is over

    Does the time sync happen during the code upload? Guess it would break then when doing battery replacement

  • ok, got it sorted

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Puck.js as power meter logger - how do you keep track of pulses per hour and day?

Posted by Avatar for user130485 @user130485

Actions