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

Please help a OH2 Rookie with the most basic function… I’ve been working with industrial automation in 40 years and the last year i have set up one Zipatile system and one Fibaro HC system. Now, I’ve started with a Raspberry Pi 3B+ and OH2 (2.4.0 SNAPSHOT to make my Sonos system work) with Aeon Z-Wave dongle. The system is up and running, two different wallplugs (Nexa and Fibaro) is up and running fine, I have a small sitemap where I can control these two devices via Z-Wave but I’m totally lost on how to get my rule to work.

The function I want is simple:
1: When I turn on one wallplug (either via sitemap or the button on the device) I want to initialize one global variable to the value of f.ex. 60.
2: The variable should count down every minute and I should be able to manually alter the varible to higher or lower value using the sitemap (sitemap works fine).
3: When the variable reaches 0, the wallplug shoult turn off.

This all looks very easy to me but I can’t get my rule-logic to work…

//Items

Switch Wallplug_TV "Wallplug TV" <poweroutlet> { channel="zwave:device:34e0ed8b:node2:switch_binary" }
Switch Rancilio "Rancilio" <poweroutlet> { channel="zwave:device:34e0ed8b:node3:switch_binary" }
Number cdRancilio "Resttid [%s]"

//Sitemap

sitemap default label="Dalatest 3 sitemap"
{
	Switch item=Wallplug_TV label="Test TV"
	Switch item=Rancilio label="Rancilio"
	Setpoint item=cdRancilio label="Resttid" minValue=0 maxValue=30 step=1
}

//Rule part 1

rule "countdown init"
when
	item Wallplug_TV changed from OFF to ON
		then
			cdRancilio = 30
end

…is generating this error:

2018-08-31 11:45:16.201 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'default.rules' has errors, therefore ignoring it: [3,2]: no viable alternative at input 'item'

//And rule part 2:

rule "Minute counter"
when
	Time cron "0 * * ? * *"
	then 
		if (cdRancilio > 0) cdRancilio = cdRancilio - 1
end

…is generating this error:

2018-08-31 12:06:00.190 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Minute counter': Unknown variable or command '&gt;'; line 5, column 7, length 14

//Rule part 3

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

…does nothing…

I’ve read a lot and tried lots of variations on this rule but it seems that some very basic knowledge is missing in my head…

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.