I am trying the following rule for sending myself an Alert with pushover.
It works, but not the way I want, because I have problems understanding the timer…
What I want:
I receive each 10seconds my WindValue in the variable WindValue.
If the Value is over 50 I want to get a push notification… After that notification there should not be sent a notification if the wind speed is higher than the last sent value plus an sending stop for 5minutes.
(At the first I got each 5sec. an alert… :-))
at the moment after the wind arrives 50km/h I get 10minutes later an alert… I want it right now, and get a sleep for 10min for next pushovers…
import org.openhab.model.script.actions.Timer
var Timer extended_timer
var EXTENDED_WIND_ALERT_TIME_SECONDS = 600
rule "WindAlert"
when
Item WindValue received update
then
if (WindValue.state >= 50){
extended_timer = createTimer(now.plusSeconds(EXTENDED_WIND_ALERT_TIME_SECONDS)) [|
logInfo("WindSpeed 50km/h überschritten: ",WindValue.state.toString())
pushover("Wind Wert: "+WindValue.state.toString() + "km/h")
])
} else {
if(extended_timer!=null) {
extended_timer.cancel
extended_timer = null
}
}
end
Well, it is currently working as written. When the speed is over 50 you sleep for ten minutes and then send the alert. The code in the timer executes when the timer goes off. A timer doesn’t prevent the Rule from executing.
Perhaps the easiest way to do this is with a timestamp.
var lastAlertTime = now.minusMinutes(10) // set the lastAlertTime to ten minutes ago so we get alerts immeidately
rule "WindAlert"
when
Item WindValue changed // if the value is the same just skip the rule entirely
then
if(now.minusMinutes(10).isAfter(lastAlertTime) && WindValue.state >= 50) {
logInfo("WindSpeed 50km/h überschritten: ",WindValue.state.toString())
pushover("Wind Wert: "+WindValue.state.toString() + "km/h")
lastAlertTime = now
}
end
thank you for your help! I am just trying some rules…
What if… I want to get an alert only if 50km/h has arrived, and each pushover only for max windspeed.
And max windspeed only for the rest of the day…
So if 60km/h is the max. wind speed today, this should be the last alert for that day.
at midnight reset.
how can this be done by rule ?
Number WindValue { http="<[beckhoff:2000:JSONPATH($.ActWindSpeed)]" }
Number MaxWindValue
rule "WindAlert"
when
Item WindValue changed
then
if(MaxWindValue.state == NULL){
MaxWindValue = 0
}
if((WindValue.state > MaxWindValue.state)){
MaxWindValue = WindValue.state
}
if(MaxWindValue.state >= 50) {
logInfo("WindSpeed 50km/h überschritten: ",WindValue.state.toString())
sendPushbulletNote("DEFAULT", "myMail@mail.mail", "Windspitze "+WindValue.state.toString()+" km/h erreicht", "Der Wind hat soeben einen Wert von "+WindValue.state.toString()+" km/h erreicht")
}
end
rule "reset WindValue"
when
Time cron "0 0 0 1/1 * ? *"
then
MaxWindValue = 0
end
sitemap:
Text item=WindValue label="Wind: [%.0f km/h]" icon="wind"
Text item=MaxWindValue label="Wind max: [%.0f km/h]" icon="wind"
it does not work, it shows an - for my maxWindSpeed. I need to initialize the item MaxWindValue, tried with == 0… == null also does not work…
getting:
2018-01-02 20:03:10.885 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘WindAlert’: An error occurred during the script execution: Cannot assign a value in null context.
sorry, I can’t find how ? I just copied the Time cron syntax for getting the rule each day on midnight. how can I Check if MaxWinValue is null in my if condition?
thanks,
all works, but my MaxWindValue gets never an reset… maybe my time cron is not correct? Tried to get every day on 00:00 my MaxWindValue to the actual WindValue…
rule "reset WindValue"
when
Time cron "0 0 0 * * * ?"
then
MaxWindValue.postUpdate(WindValue)
end