# Calculating Machine On Time Using Synthetic Variables and Gauges

Hi!

I am trying to accomplish the following, but can’t seem to wrap my head around the logic when creating a new synthetic variable. I tried looking through the community forum, but can’t find a solution that exists.

Let’s say I have an amp logger (current meter) that is measuring the current of a compressor every 5 min. The data I receive in Ubidots is timestamped, with a value of anywhere between 0 and 100 Amps (whatever the sensor reports)

Now, assume that I want to count the total time that a variable is above a certain threshold, for example 50.

My current approach is to create a synthetic variable using a where function, which takes the raw value of the sensor and assigns it a 1 when the value is over 50, or 0 if it is under 50: where(variablename>50, 1, 0)

My next thought was to create another synthetic variable and follow Step 2 of this article: Analytics: Advanced Synthetic Variables | Ubidots Help Center

However, I tried this, and this code only works for when the value switches from 0 to 1, and then back from 1 to 0 (when a machine switches ON or OFF, but doesn’t periodically keep reporting 1 when it is running). But it doesn’t report how much time was spent when the value was continuously reporting as 1.

Can you provide any advice as to how I can modify the code in that article to report the time spent when the value was 1? Note that there could be instances where:

• there is a sequence of 0’s, then a single 1, then more 0’s
• there is a sequence of 1’s in a row followed by a sequence of 0s in a row, etc
• there is a pattern of 1 and 0s repeating
• any combination of 1s and 0s thereof

The goal is to then use a gauge widget to sum the time in microseconds (or hours after conversion) when a machine was using more than a specific amount of amps (and therefor running) using the SUM function on the widget.

I hope this is clear enough!

Thank You!

Hello @qntfy ,

I hope you’re doing well.

You can calculate the total time that a variable is above a certain threshold following these instructions.

1. Create a synthetic variable that returns the time in hours when the current is above a certain threshold. To return this value, you can follow this expression.
2. You can create any widgets to visualize your data. However, as you mentioned, you must set the aggregation method as SUM to retrieve the total time in a certain date range which can be set inside the widget or in the dashboard.