Pushover if WindSpeed > 50

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… :slight_smile: 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
1 Like

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 ?

Use a boolean flag for each speed and create a Rule that runs at midnight to reset the flag.

var fiftyAlert = false
var sixtyAlert = false
...

rule "Alert for wind speed"
when
    Item WindValue changed
then
    val speed = WindValue.state as Number
    val message = "Wind Wert: "

    switch(speed){
        // bigger speeds go first
        case speed >60 && !sixtyAlert: {
            message = message + speed + " km/h"
            sixtyAlert = true
        }
        case speed > 50 && !fiftyAlert: {
            message = message + speed + " km/h"
             fiftyAlert = true
        }
    }

    if(message.length > 11) {
        logInfo("Windspeed", message)
        pushover(message)
    }

end

There are all sorts of ways to implement this and this is probably not the best or cleanest, but it should be pretty easy to understand.

1 Like

how can I reset the flat ? do I have to put a new rule in the same file or are these global variables ?

Same file. The variables are only global to the one file, not across all Rules.

tried:

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.

any hint for me ?
Thanks,

You can’t just set an Item’s state using =. You must call postUpdate or sendCommand

MaxWindValue.postUpdate(0)
...
MaxWindValue.postUpdate(WindValue.state as Number)

Thank you for your reply. And how can I initialize my value? Tried in an if ==Null or something like isEmpty?

Just like you do it in your time above

sorry, I can’t find how ? :wink: 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?

You already are.

        if(MaxWindValue.state == NULL){
		MaxWindValue = 0
	}

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

WindValue.state

tried:

rule "reset WindValue"
when
Time cron "0 0 0 * * * ?" 
then
	MaxWindValue.postUpdate(WindValue.state)
end

no success:

openhab2 says:
cannot create timer for ruel ‘reset WindValue’: CronExpression ‘0 0 0 * * * ?’ is invalid.

Try the Quartz Cron builder website (search Google). It will help you build valid from expressions.