IoT sensor common queries
All scenarios below use the sample dataset provided by the . For more information, see Sample data.
In this scenario, we look at whether a production line is running smoothly (state
\=OK
) and what percentage of time the production line is running smoothly or not (state
\=NOK
). If no points are recorded during the interval (state
\=NaN
), you may opt to retrieve the last state prior to the interval.
To visualize the time in state, see the .
To calculate the percentage of time a machine spends in each state
- Import the contrib/tomhollingworth/events package.
- Query the
state
field. - Use
events.duration()
to return the amount of time (in a specified unit) between each data point, and store the interval in theduration
column. - Group columns by the status value column (in this case
_value
),_start
,_stop
, and other relevant dimensions. - Sum the
duration
column to calculate the total amount of time spent in each state. - Pivot the summed durations into the
_value
column. - Use
map()
to calculate the percentage of time spent in each state.
The query above focuses on a specific time range of state changes reported in the production line.
range()
defines the time range to query.filter()
defines the field (state
) and measurement (machinery
) to filter by.- regroups the data by the field value, so points with
OK
andNOK
field values are grouped into separate tables. sum()
returns the sum of durations spent in each state.
_value | duration |
---|---|
OK | 172 |
pivot()
creates columns for each unique value in the _value
column, and then assigns the associated duration as the column value. The output of the pivot operation is:
NOK | OK |
---|---|
22 | 172 |
Given the output above, map()
does the following:
- Adds the
NOK
andOK
values to calculatetotalTime
. - Divides
NOK
bytotalTime
, and then multiplies the quotient by 100. - Divides
OK
bytotalTime
, and then multiplies the quotient by 100.
This returns:
The result shows that 88.66% of time production is in the OK
state, and that 11.34% of time, production is in the NOK
state.
Mosaic visualization
The mosaic visualization displays state changes over time. In this example, the mosaic visualization displays different colored tiles based on the state
field.
To calculate the time-weighted average of data points, use the .
The example below queries the oil_temp
field in the machinery
measurement. The timeWeightedAvg()
function returns the time-weighted average of oil temperatures based on 5 second intervals.
Output data
stationID | _start | _stop | _value |
---|---|---|---|
g1 | 2021-08-01T01:00:00.000Z | 2021-08-01T00:00:30.000Z | 40.25396118491921 |
g2 | 2021-08-01T01:00:00.000Z | 2021-08-01T00:00:30.000Z | 40.6 |
g3 | 2021-08-01T01:00:00.000Z | 2021-08-01T00:00:30.000Z | 41.384505595567866 |
g4 | 2021-08-01T01:00:00.000Z | 2021-08-01T00:00:30.000Z | 41.26735518634935 |
Calculate the value between events by getting the average value during a specific time range.
The following scenario queries data starting when four production lines start and end. The following query calculates the average oil temperature for each grinding station during that period.
Output
stationID | _start | _stop | _value |
---|---|---|---|
g1 | 2021-08-01T01:00:00.000Z | 2021-08-02T00:00:00.000Z | 40 |
g2 | 2021-08-01T01:00:00.000Z | 2021-08-02T00:00:00.000Z | 40.6 |
g3 | 2021-08-01T01:00:00.000Z | 2021-08-02T00:00:00.000Z | 41.379999999999995 |
g4 | 2021-08-01T01:00:00.000Z | 2021-08-02T00:00:00.000Z | 41.2 |
Use multiple existing values to determine a state. The following example calculates a state based on the difference between the pressure
and pressure-target
fields in the machine-production sample data. To determine a state by comparing existing fields:
- Query the fields to compare (in this case,
pressure
andpressure_target
). - (Optional) Use
aggregateWindow()
to window data into time-based windows and apply an aggregate function (likemean()
) to return values that represent larger windows of time. - Use
pivot()
to shift field values into columns. - Use
map()
to compare or operate on the different field column values. - Use
map()
to assign a status (in this case,needsMaintenance
based on the relationship of the field column values.
Output
_time | needsMaintenance | pressure | pressure_target | pressureDiff | stationID |
---|---|---|---|---|---|
2021-08-01T12:00:00.000Z | false | 101.62490431541765 | 104.83915260886623 | -3.214248293448577 | g2 |
2021-08-02T00:00:00.000Z | false | 94.52039415465273 | 105.90869375273046 | -11.388299598077722 | g2 |
_time | needsMaintenance | pressure | pressure_target | pressureDiff | stationID |
---|---|---|---|---|---|
2021-08-01T12:00:00.000Z | false | 92.23774168403503 | 104.81867444768653 | -12.580932763651504 | g3 |
2021-08-02T00:00:00.000Z | true | 89.20867846153847 | 108.2579185520362 | -19.049240090497733 | g3 |