# Creating a period condition based on signal value

## Recommended Posts

Hello,

I have a signal that presents the total rolling downtime duration for 1 week every 3 hours. I'm trying to extract each value from the signal and create a condition such that each capsule has the same duration of the extracted value. I would usually use periods(1week,3h) if I have a constant value of one week duration.

The intent here  is to create a condition to calculate a rolling average of a signal based on a unit downtime values.

Thanks

• 1
##### Share on other sites

Hello Ali,

The following formula should allow you to convert your duration signal into a condition.

```\$signal.transformToCapsules(
\$sample -> capsule(
\$sample.getKey()-\$sample.getValue(),
\$sample.getKey()),
3 weeks)```

Below is a screen shot of the formula tool setup. Let me know if this gets you what you want.

Teddy

• 2
##### Share on other sites

• 3 weeks later...

My Question matches the title of this post, but is a little different. I have a signal that is a counter (increments by 1 every time a product is made). The signal/tag value resets when it reaches 32000. I want to create a period condition that is every 5000 logs. So the time of each capsule may be different but the delta of my signal from start to end of each capsule is 5000, except for the instances where a reset occurs during the capsule when the delta will appear to be -27000.

Edited by Jules
##### Share on other sites

Hi Jules,

as I do not know your data I created an example based on the sample data generated within Seeq. Therefore I created a sawtooth signal resetting every 60 days. So you may have to do adjustments to that solution.

. First step was transforming this to a continous signal using formula:

`\$values.runningDelta().max(1).runningAggregate(sum(),years()).toLinear()`

More details can be found in this post from @Allison Buenemann: Last step is creating a condition that contains exactly 5000 samples:

```((\$continousValues-(\$continousValues / 5000).floor()).runningDelta() == 0) // -> create capsules when counter % 5000 == 0
.growEnd(1y) // -> grow capsule to start of the next capsule
.transform(\$c -> \$c.setProperty('Values', \$continousValues.count(\$c))) // -> create capsule property containing the count of samples inside the capsule```

Hope this helps.

Regards,

Thorsten

• 1
##### Share on other sites

Can you go through some of the theory of your formula so I can understand how to apply it to my data?

When I use the above formula my capsules are various counts (3000 to 16000). My tag has a highly variable rate of change which I believe is the reason for my various counts.

##### Share on other sites

Hi Jules,

I guess converting your sawtooth signal to a continous one should work, so I modified the last formula a little bit:

```((\$continousValues / 5000)-(\$continousValues / 5000).floor() == 0) // -> create capsules when continous signal can be divided by 5000
.growEnd(1y) // -> grow capsule to start of the next capsule
.transform(\$c ->{
\$c.setProperty('Values', \$continousValues.count(\$c))  // -> create capsule property containing the count of samples inside the capsule
.setProperty('ValueStart', \$continousValues.getValue(\$c.getStart())) // -> create capsule property containing the value at start of the capsule
.setProperty('ValueEnd', \$continousValues.getValue(\$c.getEnd())) // -> create capsule property containing the value at end of the capsule
}) ```

The first part of the formula creates a capsule each time the value of the signal can be divided by 5000 (0, 5000, 10000, 15000, ...). These capsules are very short in duration, but can be extended to the start of the next capsule using grow(). The last part just adds some properties to the capsules used to validate the formula . Result should look like this: You may notice that the number of values is sometimes greater than 5000. I think this may occur because it counts the number of samples. If the value in the Historian does not change it may create another sample with the same value anyway. I guess the better values to check are the values at start and end of the capsule, which always can be divided by 5000.

Regards,

Thorsten

• 1
##### Share on other sites

This works! thank you, Also noticed there are many times when the increment amount is 2 instead of 1 so seeing value start/end was helpful.