Issue with garage door timer and notification send from a rule

Tags: #<Tag:0x00007f616f3464a8>

Hello,

My system environment is:
Raspbian GNU/Linux 8 (jessie)
Kernel = Linux 4.9.35-v7+
Platform = Raspberry Pi 2 Model B Rev 1.1
openHAB 2.5.4-1 (Release Build)

and I have an issue with garage door timer and notification send from a rule after 15 minutes. This message will be send only once not every time when the garage door has been opend. Any idear?
Thank in advanced.

best regads
Timo

var Timer offTimer = null

rule "Benachrichtigung_2" // 15 Minuten nach öffnen des Tores
	when		
		Item Sensor_Garagentor received update OPEN
		//Item Test_Gar_Sen received update ON
	then
		logInfo("Hinweise", "Sensor Garagentor ist: " + Sensor_Garagentor.state.toString )
		if(offTimer === null || offTimer.hasTerminated) {
			offTimer = createTimer(now.plusMinutes(15), [|
				offTimer = null
				if (Sensor_Garagentor.state == OPEN){
				    val telegramAction = getActions("telegram","telegram:telegramBot:89b7b984")
					telegramAction.sendTelegramQuery("Das Garagentor steht noch auf. Soll das Tor jetzt geschlossen werden?", "Reply_Garagentor", "Ja", "Nein")						
					logInfo("Hinweise", "Sensor Garagentor ist: " + Sensor_Garagentor.state.toString )				
				}
			])
		}	
	end

/* ---------------------------------------------------------------------------------------------------------------------------- */

rule "Reset Benachrichtigung_2"
	when
		Item Sensor_Garagentor received update CLOSED
	then
		offTimer.cancel() 
		val telegramAction = getActions("telegram","telegram:telegramBot:89b7b984")
		telegramAction.sendTelegram("Das Garagentor wurde geschlossen.")	
	end
	

/* -------------------------------------------------------------------------------------------------------------------------------- */	

rule "Reply handler for Garagentor"
when
    Item telegramReplyId received update Reply_Garagentor
then
     val telegramAction = getActions("telegram","telegram:telegramBot:89b7b984")
	if (telegramMessage.state.toString == "Ja")
	{
        Garagentor.sendCommand(ON)
        telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, das Garagentor wird geschlossen.") 
    }
    else
    {
        telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, das Garagentor bleibt offen.")
    }
end

Create a global variable and initialize it to yesterday (now.minusDays(1)).

in the rules that send the telegram, check the timestamp and only send a message if it’s been more than a day since the timestamp. Reset the timestamp to now when you do send the telegram.

This will only send the message once a day.

For those using Python rules, I’ve released a library you can use to do this at https://github.com/rkoshak/openhab-rules-helper. Install the rate_limit library and you can do this with one function call:

from communite.rate_limit import RateLimit

latch = RateLimit()
...

    # From a Rule, call the latch to limit how often the a command can be sent.
    # Let's say we don't want an alert more than once per hour. Any subsequent
    # calls to this latch will be dropped until an hour has passed.
    latch.run(lambda: events.sendCommand("Alert", "Somethings wrong!"), hours=1)

Please be aware that offTimer.hasTerminated will never be true. This condition is only true if the timer has run its code. The timer code first deletes the pointer offTimer, so this will never be true.
Your rule "Reset Benachtiigung2" cancels the timer, but does not delete offTimer, so the condition is not met after this rule was executed.

It’s better to use changed instead of received update, because you want to trigger to the change, not the update.
So, the rules would be better something like this:

var Timer offTimer = null

rule "Benachrichtigung_2" // 15 Minuten nach öffnen des Tores
when
    Item Sensor_Garagentor changed
then
    logInfo("Hinweise", "Sensor Garagentor ist: {}",newState)
    offTimer?.cancel
    if(newState == OPEN) {
        offTimer = createTimer(now.plusMinutes(15), [|
            val telegramAction = getActions("telegram","telegram:telegramBot:89b7b984")
            telegramAction.sendTelegramQuery("Das Garagentor steht noch auf. Soll das Tor jetzt geschlossen werden?", "Reply_Garagentor", "Ja", "Nein")
            logInfo("Hinweise", "Sensor Garagentor ist: {}", Sensor_Garagentor.state)
        ])
    } else {
        val telegramAction = getActions("telegram","telegram:telegramBot:89b7b984")
        telegramAction.sendTelegram("Das Garagentor wurde geschlossen.")
    }
end

/* ---------------------------------------------------------------------------------------------------------------------------- */

rule "Reply handler for Garagentor"
when
    Item telegramReplyId received update Reply_Garagentor
then
    val telegramAction = getActions("telegram","telegram:telegramBot:89b7b984")
    if (telegramMessage.state.toString == "Ja") {
        Garagentor.sendCommand(ON)
        telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, das Garagentor wird geschlossen.") 
    } else {
        telegramAction.sendTelegramAnswer(telegramReplyId.state.toString, "Ok, das Garagentor bleibt offen.")
    }
end

Thanks a lot. I will change my code and test it.

Works perfect. Thanks a lot.

You’re welcome :slight_smile: