cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X

Counting hours

anassNasser
8-Gravel

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 9
PaiChung
22-Sapphire I
(To:anassNasser)

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)

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 

PaiChung
22-Sapphire I
(To:anassNasser)

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

the result returned will be milliseconds.

 

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. 

thank you @rjanardan and @PaiChung ,

 

I will install influxDB and test it.

PaiChung
22-Sapphire I
(To:anassNasser)

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.

Thank you @PaiChung and @rjanardan ,

 

I Have a thing with an aleatory update rate, and I don't know how to model my prb with events and subscriptions.

 

can you please give me any tips or some idea to develop my problem.

 

thank you .

A Subscription could be used to listen to the occurrence of an Event and take some action when it occurs. In your case, the Event would be the value of a Thing property changing. 

So, navigate to Subscription tab in your Thing and create a new one. Enable it. Choose Me or Other entity depending on whether the property, whose value will change, belongs to the same Thing or another one. Under Input, you have to select Event as DataChange(event when a property value changes) and you will need to specify which property's DataChange Event you would listen to. Once that is done, you will write code for the action that needs to be done when your property's value changes. You could call existing services as well here.  

PaiChung
22-Sapphire I
(To:anassNasser)

Besides the great answer given already, for additional learning resource that is free you can go here: https://developer.thingworx.com/en

besides the official stuff here: https://www.ptcu.com/ 

Announcements


Top Tags