NOTE: It is easier to read code if you use code formatting on your postings. Just surround your code with triple backticks like the following:
```
You code goes here
```
I see two errors with your code which might be your problem and some syntax errors.
When you create a Timer, the stuff inside the “[ | ]” is what gets executed when the Timer goes off. You only have garagedoor2Timer = null inside the timer block. Your sendTelegram is outside the timer block so it will immediately send your message rather than waiting for the Timer to expire.
Don’t you want to set the timer when Garage_Port_2State_Closed is OPEN? Your intent is to send the alert when the door has been open for more than 30 minutes, not closed for more than 30 minutes.
Syntax wise, your else clause is not proper syntax. It looks like you are trying to apply the Rule’s Trigger syntax to an if statement which is incorrect. I highly recommend getting Designer and editing your config with it as it will point out syntax errors as you type. The big thing to realize is that a Rules trigger is based on an event. As soon as the described event occurs (e.g. Garage_Port_2_State_Closed changed to CLOSED) the Rule triggers. A if statement is testing for the current state. The difference is subtle but important. So in this case, it makes no sense to have an if statement test for changed to because that is an event, not a state.
So as written your code should look like:
var String Message6 = 'Garage | Port 2 har stået åbent i 30 minutter! sent from openHAB'
// This rule sends a telegram notification if garagedoor has been open for 30 min.
rule "Send telegram Notification | Garagedoor 2 open for 30 min."
when
Item Garage_Port_2_State_Closed changed to OEPN
then
if(garagedoor2Timer == null || garagedoor2Timer.hasTerminated) {
garagedoor2Timer = createTimer(now.plusMinutes(30), [|
garagedoor2Timer = null
sendTelegram((TelegramBots), (Message6))
])
}
end
rule "Cancel telegram Notification | Garagedoor 2 open for 30 min."
when
Item Garage_Port_2_State_Closed changed to OPEN
then
garagedoor2Timer.cancel() //Terminate timer if door is closed before the timer ends
end