Notification if ITEM stays OPEN

Hey Guys,

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

Many Thanks
Simon

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

Configuration model ‘briefkasten_OPEN.rules’ has errors, therefore ignoring it: [7,56]: mismatched input ‘,’ expecting ‘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.

A minute later, xxxx executes.

Okay thx, i try that!

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.

try this

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.plusMinutes(2), [|
                      val telegramAction = getActions("telegram","telegram:telegramBot:b99")
                      telegramAction.sendTelegram("Briefkasten ist noch offen!")                     
                      Briefkasten_timer = null
                 ])
             }
          }
          else if(Briefkasten.state == CLOSED){
                          Briefkasten_timer?.cancel
                          Briefkasten_timer = null
                          logInfo("Briefkasten_timer","Timer canceled") 
                      }
end
1 Like

if you dont get a notification, check your telegram bot!
with my bot it´s working

Find out if for yourself your Timer runs when you expect it

Briefkasten_timer = createTimer(now.plusMinutes(2), [|
        logInfo("Briefkasten_timer","Timer executing")
        logInfo("Briefkasten_timer","state is " + Briefkasten.state.toString)

if(Briefkasten.state == OPEN ){
...
}
                ])
        logInfo("Briefkasten_timer","Timer created")
end

put in every other line a loginfo command with a different message. then you will instabtly see what your script does and does not

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 :slight_smile:

@Simon_B Your rule rewritten in JRuby Scripting

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

1 Like

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.

Ah, we’d assumed your Item was a Contact type, I suppose it is String type.

Yes, it is a string. Thank you all for the Help.
:laughing:

Hm thx, i will try this Out, it looks nice and simple.