Skip to main content
1-Visitor
March 5, 2020
Question

Counting hours

  • March 5, 2020
  • 2 replies
  • 3239 views

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

2 replies

22-Sapphire I
March 5, 2020

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)

1-Visitor
March 5, 2020

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 

22-Sapphire I
March 5, 2020

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

the result returned will be milliseconds.

 

16-Pearl
March 5, 2020

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. 

1-Visitor
March 5, 2020

thank you @rjanardan and @PaiChung ,

 

I will install influxDB and test it.

22-Sapphire I
March 5, 2020

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.