Calculating with a Number Item and a Variable in a Rule with ECMA Script 11

Hello I want to use a rule to increment a number item by 1 when a switch is pressed.
I have written it in ECMA, I get a parse error displayed and the rule then of course does not calculate.
I could use some help with this issue. Thanks already

var intervall = 1;
var MinuteNewSet = 0;

var item_Minute_Set = items.getItem('Minute_Settime');

console.log("Set Minute hat Wert: ", item_Minute_Set.state);

console.log("Variable MinuteNewSet hat Wert: ", MinuteNewSet);
console.log("Variable item_Minute_Set hat Wert: ", item_Minute_Set.state);
console.log("intervall hat Wert: ", intervall);

MinuteNewSet = (item_Minute_Set + intervall);

console.log("Set Minute hat Wert NACH +1 zÀhlen: ", MinuteNewSet);


items.getItem('Minute_Settime').sendCommand(MinuteNewSet);

items.getItem('Minute_Settime');
console.log("Set Minute ITEM hat neuen Wert nach update: ", item_Minute_Set.state);

items.getItem('Minute_plus').sendCommand("OFF");
console.log("Schalter Minute plus ausschalten", items.getItem('Minute_plus').state);

Log Output:

20:30:25.969 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command ON
20:30:25.974 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from OFF to ON
20:30:25.983 [WARN ] [g.internal.OpenhabGraalJSScriptEngine] - Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled.
20:30:26.471 [INFO ] [.automation.script.ui.Minute_SET_plus] - Set Minute hat Wert: 0
20:30:26.472 [INFO ] [.automation.script.ui.Minute_SET_plus] - Variable MinuteNewSet hat Wert: 0
20:30:26.474 [INFO ] [.automation.script.ui.Minute_SET_plus] - Variable item_Minute_Set hat Wert: 0
20:30:26.476 [INFO ] [.automation.script.ui.Minute_SET_plus] - intervall hat Wert: 1
20:30:26.477 [INFO ] [.automation.script.ui.Minute_SET_plus] - Set Minute hat Wert NACH +1 zÀhlen: [object Object]1
20:30:26.479 [WARN ] [ernal.defaultscope.ScriptBusEventImpl] - Command ‘[object Object]1’ cannot be parsed for item ‘Minute_Settime (Type=NumberItem, State=0, Label=Minute gesetzt, Category=, Tags=[Point], Groups=[Time_Sets])’.
20:30:26.480 [INFO ] [.automation.script.ui.Minute_SET_plus] - Set Minute ITEM hat neuen Wert nach update: 0
20:30:26.483 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command OFF
20:30:26.483 [INFO ] [.automation.script.ui.Minute_SET_plus] - Schalter Minute plus ausschalten ON
20:30:26.485 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from ON to OFF
20:30:26.486 [INFO ] [.automation.script.ui.Minute_SET_plus] - Set Minute hat Wert: 0
20:30:26.489 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_Settime’ received command 0
20:31:15.958 [INFO ] [openhab.event.RuleUpdatedEvent ] - Rule ‘Minute_SET_plus’ has been updated.

isn’t that causing the problem :

because item_Minute_Set is an object and intervall is an integer 
?

Shouldn’t it be:
MinuteNewSet = (item_Minute_Set.state + intervall);

Jruby syntax

current = Minute_Settime.state || 0
Minute_Settime.update(current + 1)

Minute_plus.off
1 Like

You first check, if your item.state is of type number, and you get a number from a string with parseFloat

if (isNaN(parseFloat(iCounter.state)) ) {do something};
else iCounter.postUpdate(parseFloat(iCounter.state)+1);

1 Like

Thanks for the tip, but I do not get the desired result.

var intervall = 10.0;


var item_Minute_Set = items.getItem('Minute_Settime');
console.log("item_Minute_Set hat Wert: ", item_Minute_Set);
console.log("item_Minute_Set.state hat Wert: ", item_Minute_Set.state);



console.log("intervall hat Wert: ", intervall);

var MinuteNewSet = (item_Minute_Set.state + intervall);

console.log("MinuteNewSet hat Wert NACH +1 zÀhlen: ", MinuteNewSet);


items.getItem('Minute_plus').sendCommand("OFF");
console.log("Schalter Minute plus ausschalten", items.getItem('Minute_plus').state);

Log Output:
18:06:40.450 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command ON
18:06:40.454 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from OFF to ON
18:06:40.479 [WARN ] [g.internal.OpenhabGraalJSScriptEngine] - Failed to retrieve script script dependency listener from engine bindings. Script dependency tracking will be disabled.
18:06:40.988 [INFO ] [.automation.script.ui.Minute_SET_plus] - item_Minute_Set hat Wert: {
“rawItem”: {},
“history”: {
“item”: {}
}
}
18:06:40.991 [INFO ] [.automation.script.ui.Minute_SET_plus] - item_Minute_Set.state hat Wert: 0.0
18:06:40.993 [INFO ] [.automation.script.ui.Minute_SET_plus] - intervall hat Wert: 10
18:06:40.995 [INFO ] [.automation.script.ui.Minute_SET_plus] - MinuteNewSet hat Wert NACH +1 zÀhlen: 0.010
18:06:40.999 [INFO ] [.automation.script.ui.Minute_SET_plus] - Schalter Minute plus ausschalten ON
18:06:40.999 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command OFF
18:06:41.009 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from ON to OFF

I wrote the rule using ECMA 5.1 and it works right off the bat.
For ECMA 11 I still don’t have a solution, but there must be a simple one !
Maybe you have an idea how to convert this rule to ECMA.

JS ECMA 5.1:

var MinSet = 0 ;
var intervall = 1 ;
var NewMinState = 0 ;

    MinSet = itemRegistry.getItem('Minute_Settime').getState () ;
    NewMinState = MinSet + intervall ;

events.postUpdate('Minute_Settime', NewMinState  ) ;
events.sendCommand('Minute_plus', 'OFF');

Log Output:
9:13:50.755 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_Settime’ changed from 1 to 2
19:13:55.910 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command ON
19:13:55.924 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from OFF to ON
19:13:56.046 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_Settime’ changed from 2 to 3
19:13:58.808 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command OFF
19:13:58.815 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from ON to OFF
19:14:00.938 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command ON
19:14:00.948 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from OFF to ON
19:14:00.953 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_Settime’ changed from 3 to 4
19:14:02.199 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command OFF
19:14:02.202 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from ON to OFF

It can be that simple

But definitely also post a solution for JS ECMA 11.
Thanks already

You’ll need to fix that

Thanks for the tip. I’ve already searched for a workaround, but haven’t found anything yet, except that it’s been reported as an issue.
Do you have any idea?

I have no idea. There are a number of posts about it - including the opinion that it’s really just a nuisance with no real impact. So maybe not your issue, after all.

Looks to me like your intended addition has just done a string concatenation. Do you have to parse retrieved states into numbers here or something? Or cast your integer state as decimal?


should be:
var MinuteNewSet = (parseFloat(item_Minute_Set.state) + intervall);
or
var MinuteNewSet = (parseInt(item_Minute_Set.state) + intervall);

depends on whether intervall is an integer or floating point

var intervall = 1; or var intervall = 10.0;
1 Like

Thanks for the tip, that led me to solution. Now the increment item works also in JS ECMA 11.
I thank the community and especially Roberto for the support.

Solution:

var intervall = 1;
var MinuteNewSet = 0;
var item_Minute_Set = 0;
var MinuteNewSet_parse_to_Int = 0;

item_Minute_Set = items.getItem('Minute_Settime'); // item_Minute_Set muss geparst werden
console.log("item_Minute_Set.state hat Wert: ", item_Minute_Set.state); // .state Wert Ok ab nicht mit Variablen verrechenbar (parsepflichtig)
console.log("intervall hat Wert: ", intervall); // HochzÀhlwert

MinuteNewSet_parse_to_Int = (parseInt(item_Minute_Set.state)); // .state Wert parsen auf Int
console.log("item_Minute_Set hat aktuellen Wert nach parsen zu Int: ", MinuteNewSet_parse_to_Int); // Wert als  Int
                    
MinuteNewSet = MinuteNewSet_parse_to_Int + intervall; // Count up
console.log("MinuteNewSet hat Wert NACH +1 zÀhlen: ", MinuteNewSet); // Neuer Wert

console.log("Setze neuen Minutenwert in Item Minute_Settime: ", MinuteNewSet);
items.getItem('Minute_Settime').sendCommand(MinuteNewSet); // Neuer Wert wird in Item geschrieben

console.log("Schalter Minute plus ausschalten", items.getItem('Minute_plus').state);
items.getItem('Minute_plus').sendCommand("OFF"); // Count up Switch ausschalten

Log Output:
18:07:38.763 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command ON
18:07:38.770 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from OFF to ON
18:07:39.303 [INFO ] [.automation.script.ui.Minute_SET_plus] - item_Minute_Set.state hat Wert: 63
18:07:39.305 [INFO ] [.automation.script.ui.Minute_SET_plus] - intervall hat Wert: 1
18:07:39.308 [INFO ] [.automation.script.ui.Minute_SET_plus] - item_Minute_Set hat aktuellen Wert nach parsen zu Int: 63
18:07:39.311 [INFO ] [.automation.script.ui.Minute_SET_plus] - MinuteNewSet hat Wert NACH +1 zÀhlen: 64
18:07:39.313 [INFO ] [.automation.script.ui.Minute_SET_plus] - Setze neuen Minutenwert in Item Minute_Settime: 64
18:07:39.314 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_Settime’ received command 64
18:07:39.317 [INFO ] [.automation.script.ui.Minute_SET_plus] - Schalter Minute plus ausschalten ON
18:07:39.318 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_Settime’ changed from 63 to 64
18:07:39.321 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘Minute_plus’ received command OFF
18:07:39.344 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Minute_plus’ changed from ON to OFF

Merry Christmas :grinning:

That’s something in the add-on which has been fixed in 3.4. It’s unrelated to this issue here. It’s related to dependency tracking and all it meant was if a library file changed that is being used by a rule, the rule wouldn’t be reloaded to import the change.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.