[Solved] Calculate formulas on sensor data

I have a device that measures barometric pressure. I would like to measure trends on this data in order to calculate a weather prediction.
How can I access the graph data? how can I calculate trends on it?

Found workflows->Data->Time series or table:get rows

I would like to calculate the following formula:
average(sensorDate( from(now- 60 minutes) to (now- 65 minutes)))
I want to create multiple such queries with different time windows, group them int a table and calculate a trend on this data.

How do I do such queries & math?


Hi Uri,

Currently, the way we have our Data: Time Series Node set up you cannot query on data given an exact time. However, if you use our Losant API Node there is a way to obtain the data by calling the API endpoint Time Series Query. You can give this endpoint a start e.g. (now-65) and end e.g. (now - 60), with a resolution of 5 minutes e.g. 300000 ms, and your aggregation will be “MEAN”. This will return you one data point which is the average of the 5 minute period.

As for storing the data, you can use the Table Insert Row, to add these averages to a table to access later to calculate the trends.

Hope that helps,

Hi Uri,

I have been informed of an even better way to solve your issue! If you use the Data Gauge Query Node and under the time range option select Custom. You can then specify the Range End e.g. (now-60 minutes) in milliseconds and the range which is 300000 ms. This way you do not have to worry about setting the resolution, which will be the entire range.

Hope this makes things even easier!

Thank you
I have done that, but guess I am doing something wrong with the data entry to the table. Can someone look into my app and tell me what I am doing wrong?

Hi Uri,

I took a look at one of your workflows and I noticed you are actually putting in (now - 60 minutes), (now - 30 minutes) etc. I should have specified that you have to use the handlebar helpers to caculate time in milliseconds. Try using the time that is on the payload, and to put that time into milliseconds using this handlebars helper e.g. {{ format time 'x' }}. Now to do the subtraction you will want to do something like {{subtract (format time 'x') 1800000 }} which will give you the time 30 minutes ago.

Hope that helps,

Sorry, too complicated for me, but the other method works too, no? or please explain more.
also, now that I have calculated a result, say data.dP_dt.t240, how can I return this result to a graph or indicator?
I would like to have an indicator with the following options

const char *weather[] =

and logic:

if (dP_dt < (-0.25))
return 4; // Quickly falling LP, Thunderstorm, not stable
else if (dP_dt > 0.25)
return 3; // Quickly rising HP, not stable weather
else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05)))
return 2; // Slowly falling Low Pressure System, stable rainy weather
else if ((dP_dt > 0.05) && (dP_dt < 0.25))
return 1; // Slowly rising HP stable good weather
else if ((dP_dt > (-0.05)) && (dP_dt < 0.05))
return 0; // Stable weather
return 5; // Unknown

Sorry Uri but (now - 30 minutes) will not work. You can either use {{subtract (format time 'x') 1800000 }} in the end range which will give you the exact time 30 minutes before the workflow was triggered.
For example:

Or if that is still too complicated on this particular node, on the Range End field you can put a negative number, -1800000, which will result in 30 minutes before the current time.
For example:

Both of the examples above will result in returning the average value of the attribute given from 35 minutes ago to 30 minutes ago.

As far as getting this information on a dashboard to a graph, you will have to create a device, and add "stable", "sunny", "cloudy", "unstable", "thunderstorm", "unknown" as attributes, and give them a type. Then on your workflow, after you get your data gauge query results, you can use a Function Node to compute your logic. And then finally you will need to send that result to the device using a Device State Node.

After you that you can create a dashboard, and add a block called time series graph. Click the add a segment button and you can select an attribute like sunny to display that attribute on the line graph. You can add multiple segments to compare different attributes at once.

I would also recommend that if you have not checked out our Walkthrough that you do so. It also deals with weather and getting weather data and displaying it on a dashboard. It is much simpler than what you are asking to accomplish but it may give you a better idea of how all the different parts of losant work together.

Hope this helps,

Your second method is limited to 2 hours, any number larger than that is ignored/truncated(i see that in the hint below where i put this number). as far as method one goes, i do not know if it is not truncated too.

Hi Uri,

Sorry for the confusion that is supposed to just be a human-readable time, so you don’t accidentally remove a digit. It is not actually truncating or ignoring your time, it will use the number inserted into the box. And as far as not being able to go past two hours. Below is an example of a Range End is 24 hours ago and the Range Duration is about 3.5 days ago.

Hi Erin
All issues solved and code is running. Many thanks for your knowledge & patience!

Glad to hear it, Uri! We are always happy to help!