[RESOLVED] Timer does not work in rule

Hi!
I’ve got example from here: http://www.homeautomationforgeeks.com/openhab_rules.shtml
And did my test rule:

var Timer sendCO2WaitTimer = null
var boolean sentCO2 = false
rule "Telegram send CO2"
when
    Item Bedroom_CO2 changed
then
    if (!sentCO2 && Bedroom_CO2.state > 1000)
    sendTelegram("MySuperMegaBot", Bedroom_CO2.name + " warning: %s", Bedroom_CO2.state.toString)
    sentCO2 = true
        sendCO2WaitTimer = createTimer(now.plusHours(12))[|
            sentCO2 = false
        ]
end

I receive messages in Telegram, but I receive them every time Bedroom_CO2 changed no matter if sentCO2 true or false. What am I doing wrong?

BTW Bedroom_CO2.name returns item name like Bedroom_CO2
I have item:

Number Bedroom_CO2 "Bedroom CO2" (BEDROOM) {mqtt="<[mainmqtt:/sensor/home/bedroom/co2:state:default]"}

And wanna see string Bedroom CO2 in Telegram message not Bedroom_CO2. Hot to do that, what should I write in rule?

sendTelegram("MySuperMegaBot", Bedroom_CO2.name.replace("_", " ") + " warning: %s", Bedroom_CO2.state.toString)

Cool, thanks, but that’s not exactly what I want.
What if my item is:

Number Bedroom_CO2 "My super mega description" (BEDROOM) {mqtt="<[mainmqtt:/sensor/home/bedroom/co2:state:default]"}

Is it possible to get this description somehow to use it in rules and scripts?

PS: The main question is about not working timer in rule.

No. You don’t have access to an Item’s label from scripts or rules.

First of all you should use indentation and curly brackets “{ }” to make it clear what your code is supposed to be doing.

This is what you have implemented with proper indentation and curly brackets added:

if(!sendCO2 && Bedroom_CO2.state > 1000) {
    sendTelegram("MySuperMegaBot", Bedroom_CO2.name + " warning: %s", Bedroom_CO2.state.toString)
}

sentCO2 = true
sendCO2WaitTimer = createTimer(now.plusHours(12)) [|
    sentCO2 = false
]

With the above sentCO2 gets set to true and a timer gets created for every change to Bedroom_CO2.

What you probably want is:

if(!sendCO2 && Bedroom_CO2.state > 1000) {
    sendTelegram("MySuperMegaBot", Bedroom_CO2.name + " warning: %s", Bedroom_CO2.state.toString)

    sentCO2 = true
    sendCO2WaitTimer = createTimer(now.plusHours(12)) [|
        sentCO2 = false
    ]
}

Notice how this matches the example at the link you provided. The indentation and curly brackets are actually meaningful. You can’t just skip them.

You may need to cast the state to a DecimalType as follows:

if (!sentCO2 && (Bedroom_CO2.state as DecimalType) > 1000) {

Thanks! That was an issue - the indentation and curly brackets - now I now it’s important! :smile: