We enhanced the flespi analytics system with the possibility to access values located in the previous interval when calculating or validating the next one.
This functionality is available via counter type=”interval” and validate_interval expression.
There you may operate with values calculated by current interval counters and apply some logic mixing values calculated/stored in previous interval. You can access such values using the previous(“interval-counter-or-property”)
call.
For example, to know the difference between the end of previous interval and beginning of current interval you may use the counter of type=”interval” and expression: if(previous('end'), begin - previous('end'), 0)
.
Or if you would like to prevent intervals creation within 1 hour from the end of the previous interval you may put into validate_interval the following expression: begin - previous('end') >= 3600
.
As a bonus and for really complex cases previous function will allow you to retrieve the data not only calculated by the counter by also attached to the interval via PUT /gw/calcs/{calc_id}/devices/{device_id}/intervals/{previous-interval-id} call. For example you may attach to some interval user data and subsequently copy its value to all further intervals until the next user-data. For example, for counter name=”datacache”, type=”interval” you may use the next expression: if(previous('userdata'), previous('userdata'), previous('datacache'))
and attach to the interval custom value under 'userdata' key.