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

Community email notifications are disrupted. While we are working to resolve, please check on your favorite boards regularly to keep up with your conversations and new topics.

Calculate time difference between true/false timestamp

MM_9023322
14-Alexandrite

Calculate time difference between true/false timestamp

I have a requirement where I have to calculate the time difference between two instances of the PumpRunSignal property (Boolean), we need to determine the duration in minutes. The Pump Run Signal is a Boolean property that triggers an event data subscription and a service call when there is a change from true to false in the input from the PLC. We calculate the time difference by comparing the timestamps of index values 1 and 0 of the PumpRunSignal. However, there is a delay in loading data from Thingworx to InfluxDB when there is a false signal. To address this delay, we have implemented a 30-second Pause every time there is a false signal. While this pause logic seems effective, there is some uncertainty about its reliability, especially when dealing with multiple calculations for each Thing within Thingworx.

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @MM_9023322 

 

I can see there is a one-minute time difference between the actual and logged values. This could be due to Math.floor method.

 

Instead of Math.floor use Math.round method. 

 

For your reference:

Velkumar_0-1703046336580.png

 

If you want a more accurate time. You need to get the decimal value from the minute and multiply it with 60 which will give you exact seconds. And you can add seconds to your minutes.

 

/VR

View solution in original post

3 REPLIES 3

Hi @MM_9023322 

 

You can use subscription input EventData - NewValue Time and OldValue Time to get the time difference

 

Velkumar_0-1702276540245.png

 

Sample Code in Subscription

 

if (!eventData.newValue.value) // If new value is false
{
	// Calculate time 
	me.timeDifference = dateDifference(eventData.newValue.time, eventData.oldValue.time); // Returns milliseconds
}

 

 

/VR

 

 

 

MM_9023322
14-Alexandrite
(To:Velkumar)

Hi @Velkumar,

 

Whenever the signal triggers from true to false, the time difference is calculated. But the false trigger time and the time stored in the Database are not the same, so there is some conflict in the time difference calculations.

 

Below is the code:

MM_9023322_0-1702985815616.png

 

 

The time difference is calculated using the above code in Thingworx and manually for the PumpRunSignal property. The highlighted in Red are mismatching.

 

MM_9023322_1-1702985815622.png

 

MM_9023322_2-1702985815625.png

 

 

MM_9023322_3-1702985815627.png

 

 

 

 

Hi @MM_9023322 

 

I can see there is a one-minute time difference between the actual and logged values. This could be due to Math.floor method.

 

Instead of Math.floor use Math.round method. 

 

For your reference:

Velkumar_0-1703046336580.png

 

If you want a more accurate time. You need to get the decimal value from the minute and multiply it with 60 which will give you exact seconds. And you can add seconds to your minutes.

 

/VR

Top Tags