Items lock in JS (ECMAscript 2011) rule

  • Platform information:
    • Hardware: Raspberry
    • OS: Ubuntu
    • openHAB version: 3.4.0
  • Issue of the topic:

Hi all,
I have written a script to update recursively the status of all items of a group (and subgroups).
This is mainly because binding do not accept the “all lights off” from the scenary controller, so i’ve set the scenary controller to also send a specific command to openhab to update the light status.
The script spend about 1,5 sec to run. During this time an item should receive an “ON” command, but the script will overwrite the status although the status has arrived after the script invocation.

In java i can use the ReentrantLock object, but i can’t find anything similar in js scripting.

Any suggestion or help?

Can you please post your rule code?

Hi,
This is the rule code, triggered by a chanel signal:

var translatedStatus = (event.event == "START_PRESS") ? "OFF" : "ON";
var mainGroup = items.getItem("Casa");

function updateRecursive (item, value) {
  
  if (item.type=="DimmerItem") {
    item.postUpdate((value == "OFF")?0:100);
  } else {
    item.postUpdate(value);
  }
  if (item.type == "GroupItem") {
    item.members.forEach(
      (subitem) => (updateRecursive(subitem, value))
    );
  }
}

updateRecursive (mainGroup, translatedStatus);

Do I understand it right that you want to turn off all lights when the scene controller triggers a START_PRESS event?

No, scenaries controller turns lights On/off during scenary activation. Sometime this is done by sending a special comand to all lights to turn all off. This command is however not recognized by openHAB binding so the light are off but the status in openHAB is still on. To overcome this problem i created the script that only update the status of items (do not send the command). Script is invoked everytime the scenaries controller send the ‘All off’ command. Problem is that the script needs about 1,5 sec. to complete and If during this time a light receive another status update, this is overwritten by the script.

What i need is a way to handle concurrency (like in databases for example) so that items will be locked at script start and any other update will wait until the item has been unlocked.

Thanks for the explanation.

„Locking“ Items is not possible and I guess will never be possible.

To solve your usecase, I have two proposals:

  • Is there a reason why you cannot send a „OFF“ command to the Light group? This would be faster than recursively updating all group members to OFF.
  • The binding should support that scenario instead of trying to lock Items from getting state updates.

I tried but my house switch to a disco :slight_smile:

The scenaries controller is faster than openHAB so it send an ‘all off’ to all light (and lights switch off) and few milliseconds later it send a light On to specific lights and that switch On.
After few milliseconds openHAB send a switch off to all lights as the rule triggers, and all lights switch off again

I know but this is out of my control.

Then a command is no one option.

I am wondering why the lights don’t report their status back correctly to openHAB. E.g. my KNX installation reports the status of a light when that light changes, no matter of the reason.

What binding are you using and which system are your lights?