I believe some of the advanced Expression features could help you here.
Here’s one approach that comes to mind:
- Store an array of events and add the newest event to the list
- Filter the array of events to just those that occurred in the last 30 minutes
- Average the list of filtered events
The main expression for managing the list would look something like the following, where we are setting an $AverageList
text variable using an expression.
Then immediately after that we would set a $AverageNumber
numeric variable with an expression:
These are using a bunch of advanced expression features, so feel free to ask questions.
Text Version for easier copy-paste (tap to expand)
Set $AverageList to expression:
#// decode the object
list = parseJson($AverageList)
#// if it's empty, initialize it
list = isEmpty(list) ? [] : list
#// add our new event to the list
list = concat(list, [{"ts": now(), "value": $context.event.value}])
#// filter the list
list = filter(list, x.ts > addMinutes(now(), -30))
#// last step always needs to stringify the content for the result
stringify(list)
Set $AverageNumber to expression:
list = parseJson($AverageList)
#// calculate the average of the 'value' property within the items
mean(map(list, x.value))
So your final rule might look something like: