[SOLVED] How to assign a value to global variable/phantom item in a rule?

rule “countdown init”
when
    Item Wallplug_TV changed from OFF to ON
then
    postUpdate.cdRancilio(30)
end

rule “Minute counter”
when
    Time cron “0 * * ? * *”
then
    if (cdRancilio.state > 0) {postUpdate.cdRancilio(cdRancilio.state - 1)}
end

rule “countdown finished”
when
    Item cdRancilio changed to 0
then
    Wallplug_TV.sendCommand(OFF)
end
Setpoint item=cdRancilio minValue=0 maxValue=30 step=1

PS: use code fences please for post you code

Sorry @hr3 but would it not be?
I think @SveinHa wants a variable so it would go:

var Number cdRancilio

rule “countdown init”
when
    Item Wallplug_TV changed from OFF to ON
then
    cdRancilio = 30
end

rule “Minute counter”
when
    Time cron “0 * * ? * *”
then
    if (cdRancilio >= 0) {
        if (cdRancilio == 0) Wallplug_TV.sendCommand(OFF)
        if (cdRancilio > 0) cdRancilio = cdRancilio.state - 1
    }
end

@vzorglub are you the proofreader today?

by the way:
I don’t checked this line

    if (cdRancilio.state > 0) {postUpdate.cdRancilio(cdRancilio.state - 1)}

and I don’t corrected the marks

...
rule "Minute counter"
when
    Time cron "0 * * ? * *"
...

Thanks so far. Still no-go…

Copy/Pasted your post, @hr3, corrected the quotation marks and got:

2018-08-31 12:57:00.031 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0

don’t use default

sitemap tmp label="TMP" {
    Switch   item=Wallplug_TV
    Switch   item=Rancilio
    Setpoint item=cdRancilio minValue=0 maxValue=30 step=1
}
rule "countdown init"
when
    Item Wallplug_TV changed from OFF to ON
then
    postUpdate.cdRancilio(30)
end

rule "Minute counter"
when
    Time cron "0 * * ? * *"
then
    if (cdRancilio.state > 0) {postUpdate.cdRancilio(cdRancilio.state - 1)}
end

rule "countdown finished"
when
    Item cdRancilio changed to 0
then
    Wallplug_TV.sendCommand(OFF)
end

I checked this and it works. Please post you actual code.

This is my actual code:

rule "countdown init"
when
    Item Wallplug_TV changed from OFF to ON
then
    postUpdate.cdRancilio(30)
end

rule "Minute counter"
when
    Time cron "0 * * ? * *"
then
    if (cdRancilio.state > 0) {postUpdate.cdRancilio(cdRancilio.state - 1)}
end

rule "countdown finished"
when
    Item cdRancilio changed to 0
then
    Wallplug_TV.sendCommand(OFF)
end

No difference with tmp sitemap. I use 2.4.0 SNAPSHOT (to get my Sonos system working for testing), may that be a problem?

I also do edits on Windows10/Notepad++ and Samba connection to my RPi3b+ but I checked the rules in ssh/nano and it is identical to the rule on my WindowsPC.

    val Number num  = cdRancilio.state - 1
    val Number numX = num - 1
    logInfo("___",num.toString +" "+ numX.toString)
    if (num > 0) {postUpdate.cdRancilio(numX)}

Try this.

 2018-08-31 13:35:00.022 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 19, length 20
    val int num  = (cdRancilio.state as DecimalType).intValue
    val int numX = num - 1
    logInfo("___",num.toString +" "+ numX.toString)
    if (num > 0) {postUpdate.cdRancilio(numX)}
2018-08-31 13:38:00.650 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0

All the things that, in my head, should be working, doesn’t… Thanks for your effort but i’m close to give up OH2…

A bit more of the openhab.log file:

[ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 23, length 20
2018-08-31 13:32:00.030 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 23, length 20
2018-08-31 13:33:00.021 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 23, length 20
2018-08-31 13:33:50.251 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'default.rules'
2018-08-31 13:34:00.749 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 19, length 20
2018-08-31 13:35:00.022 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 19, length 20
2018-08-31 13:36:00.024 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 19, length 20
2018-08-31 13:37:00.025 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '-'; line 12, column 19, length 20
2018-08-31 13:37:36.439 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'default.rules'
2018-08-31 13:38:00.641 [INFO ] [g.eclipse.smarthome.model.script.___] - 20 19
2018-08-31 13:38:00.650 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0
2018-08-31 13:39:00.035 [INFO ] [g.eclipse.smarthome.model.script.___] - 20 19
2018-08-31 13:39:00.046 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0
2018-08-31 13:40:00.040 [INFO ] [g.eclipse.smarthome.model.script.___] - 20 19
2018-08-31 13:40:00.050 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0
2018-08-31 13:41:00.038 [INFO ] [g.eclipse.smarthome.model.script.___] - 20 19
2018-08-31 13:41:00.050 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0
2018-08-31 13:42:00.038 [INFO ] [g.eclipse.smarthome.model.script.___] - 20 19
2018-08-31 13:42:00.048 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0
2018-08-31 13:43:00.038 [INFO ] [g.eclipse.smarthome.model.script.___] - 20 19
2018-08-31 13:43:00.047 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': An error occurred during the script execution: index=0, size=0

The cdRancilio is set in the sitemap to 20 so that part is working at least…

rule "countdown init"
when
    Item Wallplug_TV changed from OFF to ON
then
    cdRancilio.postUpdate(30)
end

rule "Minute counter"
when
    Time cron "0 * * ? * *"
then
    if (cdRancilio.state > 0) { cdRancilio,postUpdate((cdRancilio.state as Number) - 1) }
end

rule "countdown finished"
when
    Item cdRancilio changed to 0
then
    Wallplug_TV.sendCommand(OFF)
end
if (cdRancilio.state > 0) {cdRancilio.postUpdate((cdRancilio.state as DecimalType) - 1)}

Now we’re talking. Finally all 3 parts of the rule is working. Thanks very much both of you.

please change

    cdRancilio.postUpdate(30)

Yes, I’ll find some suitable value. Thanks, it works just as expected now.

Have you seen this?

Uses an Item, not a global. But that allows you to share it across many rule files, easily manage groups of Items with associated individual timers, etc.

Using cron, remember it “ticks” on whole-minute boundary, so that first ‘minute’ might be 0.1 or 59.9 seconds long. Not often an issue.

Yes, I read that. The solution in this thread is EXACTLY what I need, it was just a matter of getting it to work.