# Time since last sample in a signal

## Recommended Posts

• Super Seeqer

A generally interest question I want to document this morning

Quote

I have a signal and I want to know how long it has been since the last sample has been recorded in the historian

Step 1 - Create a condition from the last sample till "Now"

```\$caps = \$MySignal.toCapsules()

\$timeSinceSample = past().subtract(\$caps)

\$timeSinceSample```

Step 2 - Create a Scorecard to Capture the duration of the condition created in Step 1

• Make sure the Maximum Capsule Duration is longer than the longest gap you would expect in your signal. In this example it is 180 days
• 1
##### Share on other sites

Hi Shamus,

If I understand correctly, is this used to calculate deltaT of the samples in a signal? Lets say we have a signal A. Will this formula give us the new signal B?

 t A B t1 a1 0 t2 a2 t2-t1 t3 a3 t3-t2 t4 a4 t4-t3

I tried on my end, but error shows like this. • 1
##### Share on other sites

• Super Seeqer

The formula above is only focused on getting the single value of the time period between the very last sample and "Now". It uses the past() operator which was added in Seeq Version 51 so the error you are seeing is because you are probably on an earlier version.

On your separate question about a signal that represents the time since the last sample this formula will give you a continuous signal that calculates things are you outlined in your table. You can replace the durationkey() parameter with either startkey() or endkey() depending on if you want the time between samples to be plotted at the beginning or end of the interval.

```\$CapsEachSample = \$signal.toCapsules()
//create a capsule for the time between each sample in the orignal signal

\$CapsEachSample.aggregate(totalduration(),\$CapsEachSample,durationkey())
.convertUnits("min")
//convert the duration of the capsules into their own signal```

##### Share on other sites

• Super Seeqer

Final variation on this theme

How do I calculates a running time since the last sample in my signal

```\$cond = \$VariableSampleRateSignal.toCondition().setMaximumDuration(30d)
\$cond.timeSince(1min)```
##### Share on other sites

Hi Shamus,

I have another question. I have a Rate signal (blue curve). My goal is to create a Volume signal (integral of the rate) starting from 05/01/2021. Ideally it would be similar as the red curve.

I think the key is to create a series of capsules with increasing duration. (1st capsule has duration of 1 day, 2nd capsule has 2 days etc..) But I couldn't figure out how to do it. Is there any way to solve this problem? Thanks,

Yanmin

Edited by Yanmin Xu
##### Share on other sites

• Super Seeqer

Yasmin,

This should be pretty straight forward using the integral function in formula

Step 1 - Create a condition that marks the start and end time for when you want to calculate the integral. This could be done with a value search or manual condition

Step 2 - Use the integral function to calculate the new signal

`\$signal.integral(\$condition.removeLongerThan(5d)).convertUnits('kWh')`
• 1