i would like to receive a notification when the mailbox is left open.
Rule:
var Timer Briefkasten_timer = null
rule “Briefkasten_OPEN”
when
Item Briefkasten changed from CLOSED to OPEN
then
Briefkasten_timer = createTimer(now.plusMinutes(1), [|
logInfo("Briefkasten_timer","Briefkaste ist OFFEN")
])
if(Briefkasten.state == OPEN ){
val telegramAction = getActions("telegram","telegram:telegramBot:b99")
telegramAction.sendTelegram("Briefkasten ist noch offen!")
Echo_Kueche.sendCommand("Briefkasten ist noch offen!")
}
end
Can you please help me to find the error?
2022-01-28 15:23:39.142 [INFO ] [.core.model.script.Briefkasten_timer] - Briefkaste ist OFFEN
I may have this wrong, but I think your formatting is a little out. I’ve updated your rule below, see if that works for you.
var Timer Briefkasten_timer = null
rule “Briefkasten_OPEN”
when
Item Briefkasten changed from CLOSED to OPEN
then
Briefkasten_timer = createTimer(now.plusMinutes(1)), [|
logInfo("Briefkasten_timer","Briefkaste ist OFFEN")
val telegramAction = getActions("telegram","telegram:telegramBot:b99")
telegramAction.sendTelegram("Briefkasten ist noch offen!")
Echo_Kueche.sendCommand("Briefkasten ist noch offen!")
]
end
When the rule runs, a timer is set up for 1 minute in the future, which will execute code between .
The rule immediately continues to execute yyyy, then the rule exits.
You also want to cancel your timer so that you don’t get notified when the letter box closes. Take a look at the example in the documentation for inspiration.
Just make sure that you change the myTimer.cancel() to myTimer?.cancel() to prevent a possible null pointer exception when myTimer is still null.
var Timer Briefkasten_timer = null
rule "Briefkasten_OPEN"
when
Item Briefkasten changed from CLOSED to OPEN
then
Briefkasten_timer = createTimer(now.plusMinutes(2), [|
if(Briefkasten.state == OPEN ){
val telegramAction = getActions("telegram","telegram:telegramBot:b99")
telegramAction.sendTelegram("Briefkasten ist noch offen!")
Echo_Kueche.sendCommand("Briefkasten ist noch offen!")
}
])
logInfo("Briefkasten_timer","Briefkasten wurde geöffnet")
end
i dont get any notification that it stays open, i dont get it.
var Timer Briefkasten_timer = null
rule "Briefkasten_OPEN"
when
Item Briefkasten changed
then
if (Briefkasten.state == "OPEN" ){
logInfo("Briefkasten_timer","Briefkasten wurde geöffnet")
if (Briefkasten_timer === null) {
Briefkasten_timer = createTimer(now.plusSeconds(30), [|
if (Briefkasten.state == "OPEN" ) {
val telegramAction = getActions("telegram","telegram:telegramBot:b99")
telegramAction.sendTelegram("Briefkasten ist noch offen!")
Echo_Kueche.sendCommand("Briefkasten ist noch offen!")
Briefkasten_timer = null
}
])
}
}
else if (Briefkasten.state == "CLOSED" ){
Briefkasten_timer?.cancel
Briefkasten_timer = null
logInfo("Briefkasten_timer","Timer canceled")
val telegramAction = getActions("telegram","telegram:telegramBot:b99")
telegramAction.sendTelegram("Ich glaube Post war da!")
Echo_Kueche.sendCommand("Ich glaube Du hast Post!")
}
end
Many thanks, i had to put the states in “” do make i work and i also updated the rule
require 'openhab'
rule "Briefkasten" do
changed Briefkasten, to: "OPEN", for: 30.seconds, attach: "Briefkasten ist noch offen!"
changed Briefkasten, to: "CLOSED", attach: "Ich glaube Du hast Post!"
run do |event|
Echo_Kueche.command(event.attachment)
things["telegram:telegramBot:b99"].sendTelegram(event.attachment)
end
end
The only caveat is the command to Echo_Kueche is the same as telegram. It can be made different of course but the rule will be a bit less compact.
EDITED because Briefkasten is a string item, not a Contact
If you don’t need to send a message when it is closed, the rule becomes simpler:
require 'openhab'
rule "Briefkasten" do
changed Briefkasten, to: "OPEN", for: 30.seconds
run do
Echo_Kueche.command("Briefkasten ist noch offen!")
things["telegram:telegramBot:b99"].sendTelegram(Echo_Kueche.to_s)
end
end
Or the same as the rule in the previous one, just delete the “changed Briefkaster, to: CLOSED” line.