Skip to main content
Mar 5, 2020 2:27 PM UTC
Question

Counting hours

hi  community, 

I want to calculate the sum of time during a variable is equal to true , we can simulate this by a lamp and  we want to calculate how many hours that lamp is on;

 

thanks in advance

9 replies

Mar 5, 2020 2:35 PM UTC

You can use the On DataChange event. 

When a data change happens, the subscription receives Old and New VTQ (Value Time Quality)

this allows you to determine what state the lamp changed into and what the time delta was.

(Make sure to set the DataChange to Value so you aren't capturing unnecessary triggers)

Mar 5, 2020 2:44 PM UTC

Thank you @PaiChung ;

 

I don't know how to use eventData.newValue.time; or  eventData.oldValue.time, to calculate delta between the start and the end.

I need more detail please, 

thank you 

Mar 5, 2020 2:57 PM UTC

If you look in the snippets there is a date functions library which has dateadd available.

the result returned will be milliseconds.

 

Mar 5, 2020 3:28 PM UTC

I am assuming your data would look something like ON, Timestamp and OFF, Timestamp. If you are storing it in a value stream and using InfluxDB as persistent provider then an approach would be to use InfluxDB API /query HTTP endpoint through a service and use its ELAPSED function(calculates the difference between subsequent timestamps) and sum it, to obtain the total uptime between a specified interval. Please note that, I have not tested this approach. 

Mar 5, 2020 3:37 PM UTC

thank you @rjanardan and @PaiChung ,

 

I will install influxDB and test it.

Mar 5, 2020 3:42 PM UTC

I don't think you would need InfluxDB

again you can just model what you are monitoring, add a property that tracks hours on and use the datachange event/subscription to update it.

 

I recommend not going the influxdb path unless you have thousands of 'Things' with a very high update rate.

Neither which in this case seem to be true.