How can I use patterns in perseo rules with time windows? I have tried many different combinations, following the directions of EsperTech Tutorials, though, none of them was successful each time I post it to perseo. Any example for time window and how to get the average of any value (e.g. temperature) during that time will be highly appreciated. Trust me, before asking I have made a ton of efforts ;) One working query for me is the following:
select *,\"temperature_range_XX-YY\" as ruleName, *, ev.temperature? as Temperature, ev.id? as Id, ev.datetime? as DateTime, ev.cid? as CID, current_timestamp as TS from pattern [every ev=iotEvent(cast(cast(temperature?,String),float)>XX or cast(cast(temperature?,String),float)<YY and type=\"sensors\")]
Of course, this query returns only any temperature outside XX and YY range. What I need is to get an alert if temperature is outside the range for specific time and what is the average of all values during that time.
To get an average you could use de avg()
function within the select clause.
The following rule does work for me:
{
"name":"calculate_avg",
"text":"select *, avg(cast(cast(ev.NO2?,String),float)) as mediaNO2, ev.id? as id, \"calculate_avg\" as ruleName from pattern [every ev=iotEvent(type=\"AirQualityObserved\")].win:time(1 minute) group by ev.id?",
"action":{
"type":"update",
"parameters":{
"id":"${id}",
"type":"AirQualityAveraged",
"attributes": [
{
"name":"average",
"value":"${mediaNO2}"
}
]
}
}
}
I'd say your rule should be similar to:
"text":"select *,\"temperature_range_XX-YY\" as ruleName, *, avg(cast(cast(ev.temperature?, String), float)) as Temperature, ev.id? as Id, ev.datetime? as DateTime, ev.cid? as CID, current_timestamp as TS from pattern [every ev=iotEvent(cast(cast(temperature?,String),float)>XX or cast(cast(temperature?,String),float)<YY and type=\"sensors\")].win:time(1 minute)"