Stairway switch one way rules

Hi, what’s wrong with these rules - sometimes they get into a loop and lights are blinking. Typicall stair-way 1 way

ON RULE

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Light_5a_2x_Sensor_Switch_select_state
      state: ON
      previousState: OFF
    type: core.ItemStateChangeTrigger
  - id: "3"
    configuration:
      itemName: Light_5b_2x_Sensor_Switch_select_state
      state: ON
      previousState: OFF
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      command: ON
      itemName: Light_5b_2x_Sensor_Switch_select_state
    type: core.ItemCommandAction
  - inputs: {}
    id: "4"
    configuration:
      command: ON
      itemName: Light_5a_2x_Sensor_Switch_select_state
    type: core.ItemCommandAction

OFF RULE


configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: Light_5a_2x_Sensor_Switch_select_state
      state: OFF
      previousState: ON
    type: core.ItemStateChangeTrigger
  - id: "3"
    configuration:
      itemName: Light_5b_2x_Sensor_Switch_select_state
      state: OFF
      previousState: ON
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      command: OFF
      itemName: Light_5b_2x_Sensor_Switch_select_state
    type: core.ItemCommandAction
  - inputs: {}
    id: "4"
    configuration:
      command: OFF
      itemName: Light_5a_2x_Sensor_Switch_select_state
    type: core.ItemCommandAction

not sure when and why they are going crazy

If I understand these rules correctly, you want to set both items to OFF when one of them changes to OFF (with the OFF RULE) and you want to set both items to ON when one of them changes to ON (with the ON RULE).

Maybe using update instead of command you can solve your issue: see this section of the documentation for the difference between the two ways to set the state of an item.

I don’t think you can handle this case using simple UI rules without getting into an infinite loop unless you expand it into four rules. In order to break the loop you need to prevent the rule from triggering again when the second Item is updated to get it into sync with the Item that changed.

You’d need the following rules to do this with simple UI rules:

  1. When Item1 changed to ON but only if Item2 != ON then update Item2 to ON
  2. When Item1 changed to OFF but only if Item2 != OFF then update Item2 to OFF
  3. When Item2 changed to ON but only if Item1 != ON then update Item1 to ON
  4. When Item2 changed to ON but only if Item1 != OFF then update Item1 to OFF

Its the condition that prevents the loop.

In a single Blockly rule the logic would look something like this:

  • trigger the rule on changes to either Item

Or if you want to be more generic you can put all the Switches that need to be synchronized into a Group. Trigger the rule using Member of Group changed.

Using postupdate at the end is not sending the ON command to a physical zigbee device which stays in off state at the end (item is linked to a zigbee device). That’s why i decided to use sendCommand. i can do it in javascript but still and always check the state of an item befor sending a ON / OFF command to breake the infinitve loop

Just be careful with sending the command. Depending on how it’s wired commanding the device might toggle the light which isn’t what you want. Usually when you have two switches on one light, the actual state of the individual switches doesn’t really matter. If the light is on when you flip the switch ON or OFF, it turns off the light, and vice versa. Commanding the switch might mess up that usual behvior.

Were I to implement something like this in OH, I wouldn’t try to keep the switches in sync at all. Assuming the Switches are not connected to the the light item itself, I’d treat any change to ON or OFF of either of the Switches as a command to toggle the light ON or OFF. The states of the switches themselves don’t matter.

Yes you are right but there is an illuminance on the physical switch which tells you if it’s on or not (OFF = low light, ON = bright light) and i want to achieve that both of the stair way are in “bright” when they are on. I will try to do it in js

I am testing this rule and it seems to be OK


rules.JSRule({
  name: `Schodowki 13 - wlaczniki schodowe`,
  id: `Gate_onn13`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Schodowe 13`,
  triggers: [triggers.ItemStateChangeTrigger('Light_13a_2x_Sensor_Switch_select_state'),triggers.ItemStateChangeTrigger('Light_13b_2x_Sensor_Switch_select_state')],
  execute: (event) => {
        console.log(event.newState)

        if (event.newState === "ON"){
        console.log("jeden z 2 dostał ON")

                if ( items.getItem(`Light_13a_2x_Sensor_Switch_select_state`).state  != "ON" ) {
                  console.log("stan 13a to OFF - wysylam ON")
                  items.getItem(`Light_13a_2x_Sensor_Switch_select_state`).sendCommand('ON');
                }
                if ( items.getItem(`Light_13b_2x_Sensor_Switch_select_state`).state  != "ON" ) {
                    console.log("stan 13b to OFF - Wysyłam ON ")
                items.getItem(`Light_13b_2x_Sensor_Switch_select_state`).sendCommand('ON');
                }
        } else {
                console.log("jeden z 2 dostał OFF")
                if ( items.getItem(`Light_13a_2x_Sensor_Switch_select_state`).state  == "ON" ) {
                  console.log("stan 13a to ON - wysylam OFF")
                  items.getItem(`Light_13a_2x_Sensor_Switch_select_state`).sendCommand('OFF');
                }
                if ( items.getItem(`Light_13b_2x_Sensor_Switch_select_state`).state  == "ON" ) {
                    console.log("stan 13b to OFF - Wysyłam ON ")
                items.getItem(`Light_13b_2x_Sensor_Switch_select_state`).sendCommand('OFF');
                }
                }
}});

Since you are using JS Scripting you can use items.Light_13b_2x_Sensor_Switch_select_state.commandIfDifferent('ON') and you can avoid the if statement. Though if you want the corresponding log you’ll still need the if statement.

Couple that with the use of event.itemState and the whole action could reduce to:

console.info(event.itemName + ' changed to ' + event.newState + ' synchronizing switches');
items.Light_13a_2x_Sensor_Switch_select_state.commandIfDifferent(event.itemState);
items.Light_13b_2x_Sensor_Switch_select_state.commandIfDifferent(event.itemState);
1 Like

Here is my version I have added that the rule is only executed again after a time x so that the switch signals do not bounce.

@Jacek_Kaczmarczyk Maybe you can change the titel to stairway for easier finding?

Than you all, so what i end up is bouncing switches. It seems like the zigbee network is not very stable causing the automation to fire and fire again. not sure if i can handle this by the openhab site

2024-06-18 11:32:48.747 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:48.749 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:48.752 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:48.763 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:48.765 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:48.768 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:48.777 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:48.802 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:48.805 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:48.817 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:48.819 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:48.822 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:48.839 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:48.865 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:48.877 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:48.879 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:48.882 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:48.901 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:48.913 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:48.924 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:48.930 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:48.953 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:48.955 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:48.967 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:48.969 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:48.973 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:48.979 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:48.981 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:48.986 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:49.020 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:49.022 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:49.029 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:49.030 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:49.033 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.037 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:49.060 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:49.078 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:49.080 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.084 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:49.106 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:49.108 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:49.112 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.116 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:49.135 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:49.145 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:49.154 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:49.156 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:49.159 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.168 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:49.176 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:49.188 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:49.189 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:49.205 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:49.233 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:49.235 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:49.236 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.300 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:49.317 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:49.329 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:49.355 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:49.358 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:49.361 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.365 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON
2024-06-18 11:32:49.379 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:49.390 [INFO ] [.automation.script.file.schodowki.js] - ON
2024-06-18 11:32:49.424 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał ON
2024-06-18 11:32:49.426 [INFO ] [.automation.script.file.schodowki.js] - stan 7c to OFF - Wysylam ON
2024-06-18 11:32:49.454 [INFO ] [.automation.script.file.schodowki.js] - OFF
2024-06-18 11:32:49.471 [INFO ] [.automation.script.file.schodowki.js] - jeden z 3 dostał OFF
2024-06-18 11:32:49.475 [INFO ] [.automation.script.file.schodowki.js] - stan 7a to OFF - wysylam ON
2024-06-18 11:32:49.483 [INFO ] [.automation.script.file.schodowki.js] - stan 7b to OFF - Wysyłam ON

the time/limit seems to be the solution but not sure if i will be able to do it in javascript rules

Can you just toggle one item and have the second item’s profile to follow the first one?

That would only work if only one of the physical switches can be manually toggled. Unfortunately the follow profile only works one way and if you set up two Items with the follow profile so they both follow each other, you will end up with loops.

I doublt it has to do with the stability of the network and it probably goes back to that warning I mentioned above concerning what the switch does when it receives a command. It looks like it’s doing what I warned about, meaning it’s treating any change in state as a command to toggle the light.

I think no - the switch has a ON and OFF state and the device is turning ON only after receving ON command. i can not trace exactly the problem because when i turn on my rules and make end-to-end test it is working ok both ways physical/command one way and another. The bouncing is starting after a longer period (not sure why and when exactly) but it is ok for at leaste few hours

i need some help to implement this rate limit into my javascript, not sure how to move this rules from blockly to js

Check the documentation for blockly

var eventItemName, eventItemState, item;

function getOrInstantiate(cache, name, condition, create) {
  condition = (condition === undefined || condition === null) ? ((manager) => false) : condition;
  let manager = cache.get(name);
  if(manager === null || condition(manager)) {
    manager = create();
    cache.put(name, manager);
  }
  return manager;
}

var OHRT = (() => {
  const ohrt = require('openhab_rules_tools');
  ohrt.helpers.validateLibraries('4.1.0', '2.0.3');
  return ohrt;
})();

function getRateLimit(cache, name) {
  return getOrInstantiate(cache, name, null, () => OHRT.RateLimit());
}


eventItemName = event.itemName;
console.info(('Event item: ' + String(event.itemName)));
eventItemState = event.itemState.toString();
console.info(('Event state: ' + String(items.getItem(event.itemName).state)));
getRateLimit(cache.private, 'ruleTimer').run(function() {
    var item_list = items.getItem('gTreppenhausSchalter_Switch').members;
  for (var item_index in item_list) {
    item = item_list[item_index];
    if (eventItemName != item.name) {
      console.info(('Item to change: ' + String(item.name)));
      item.sendCommand(eventItemState);
    }
  }
  }, 'PT' + 1 + 'S');

Thank you, i ended up with these code for 2 stairway in javascript, tomorrow i will test it on production


rules.JSRule({
  name: `Stairway 12`,
  id: `stairway12`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Schodowe 12`,
  triggers: [triggers.ItemStateChangeTrigger('Light_12a_2x_Sensor_Switch_select_state'),triggers.ItemStateChangeTrigger('Light_12b_2x_Sensor_Switch_select_state')],
  execute: (event) => {

function getOrInstantiate(cache, name, condition, create) {
  condition = (condition === undefined || condition === null) ? ((manager) => false) : condition;
  let manager = cache.get(name);
  if(manager === null || condition(manager)) {
    manager = create();
    cache.put(name, manager);
  }
  return manager;
}

var OHRT = (() => {
  const ohrt = require('openhab_rules_tools');
  ohrt.helpers.validateLibraries('4.1.0', '2.0.3');
  return ohrt;
})();

function getRateLimit(cache, name) {
  return getOrInstantiate(cache, name, null, () => OHRT.RateLimit());
}



        console.log(">----NEW-12---" + event.itemName + " Received: " + event.newState)
        getRateLimit(cache.private, 'ruleTimer12').run(function() {


            if (event.newState === "ON"){
            console.log("no rate limit - 12 - Received ON")

                if ( items.getItem(`Light_12a_2x_Sensor_Switch_select_state`).state  != "ON" ) {
                  console.log("12a state is OFF - Sending ON")
                  items.getItem(`Light_12a_2x_Sensor_Switch_select_state`).sendCommand('ON');
                }
                if ( items.getItem(`Light_12b_2x_Sensor_Switch_select_state`).state  != "ON" ) {
                    console.log("12b state is OFF - Sending ON ")
                items.getItem(`Light_12b_2x_Sensor_Switch_select_state`).sendCommand('ON');
                }
            } else {
                console.log("no rate limit - 12 - Received OFF")
                if ( items.getItem(`Light_12a_2x_Sensor_Switch_select_state`).state  == "ON" ) {
                  console.log("12a state is ON - Sending OFF")
                  items.getItem(`Light_12a_2x_Sensor_Switch_select_state`).sendCommand('OFF');
                }
                if ( items.getItem(`Light_12b_2x_Sensor_Switch_select_state`).state  == "ON" ) {
                    console.log("12b ate is OFF - Sending ON ")
                items.getItem(`Light_12b_2x_Sensor_Switch_select_state`).sendCommand('OFF');
                }
                }


        }, 'PT' + 1 + 'S');

}});

rules.JSRule({
  name: `Stairway 5`,
  id: `stairway5`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Stairway switches 5`,
  triggers: [triggers.ItemStateChangeTrigger('Light_5a_2x_Sensor_Switch_select_state'),triggers.ItemStateChangeTrigger('Light_5b_2x_Sensor_Switch_select_state')],
  execute: (event) => {

function getOrInstantiate(cache, name, condition, create) {
  condition = (condition === undefined || condition === null) ? ((manager) => false) : condition;
  let manager = cache.get(name);
  if(manager === null || condition(manager)) {
    manager = create();
    cache.put(name, manager);
  }
  return manager;
}

var OHRT = (() => {
  const ohrt = require('openhab_rules_tools');
  ohrt.helpers.validateLibraries('4.1.0', '2.0.3');
  return ohrt;
})();

function getRateLimit(cache, name) {
  return getOrInstantiate(cache, name, null, () => OHRT.RateLimit());
}



        console.log(">----NEW-5---" + event.itemName + " Received: " + event.newState)
        getRateLimit(cache.private, 'ruleTimer5').run(function() {
            if (event.newState === "ON"){
            console.log("no rate limit - 5 - Received ON")

                if ( items.getItem(`Light_5a_2x_Sensor_Switch_select_state`).state  != "ON" ) {
                  console.log("5a state is OFF - Sending ON")
                  items.getItem(`Light_5a_2x_Sensor_Switch_select_state`).sendCommand('ON');
                }
                if ( items.getItem(`Light_5b_2x_Sensor_Switch_select_state`).state  != "ON" ) {
                    console.log("5b state is  OFF - Sending ON ")
                items.getItem(`Light_5b_2x_Sensor_Switch_select_state`).sendCommand('ON');
                }
            } else {
                console.log("no rate limit - 5 - Received OFF")
                if ( items.getItem(`Light_5a_2x_Sensor_Switch_select_state`).state  == "ON" ) {
                  console.log("5a state is ON - Sending OFF")
                  items.getItem(`Light_5a_2x_Sensor_Switch_select_state`).sendCommand('OFF');
                }
                if ( items.getItem(`Light_5b_2x_Sensor_Switch_select_state`).state  == "ON" ) {
                    console.log("5b state is ON - Sending OFF ")
                items.getItem(`Light_5b_2x_Sensor_Switch_select_state`).sendCommand('OFF');
                }
                }
        }, 'PT' + 1 + 'S');
}});

There are some tricks that need to be done in the code to make the OHRT library work in Blockly which results in some over done code.

In a text based JS this code can be reduced to:

const ohrt = require('openhab_rules_tools');
// validating the library versions is not something you'd typically need to do

rules.JSRule({
  name: `Stairway 12`,
  id: `stairway12`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Schodowe 12`,
  triggers: [triggers.ItemStateChangeTrigger('Light_12a_2x_Sensor_Switch_select_state'),triggers.ItemStateChangeTrigger('Light_12b_2x_Sensor_Switch_select_state')],
  execute: (event) => {
    console.info(">----NEW-12---" + event.itemName + " Received: " + event.newState)
    cache.private.get('ruleTimer12', () => ohrt.RateLimit()).run(() => {
        console.info("no rate limit - 12 - Received ON");
        var a = items.Light_12a_2x_Sensor_Switch_select_state;
        var b = items.Light_12b_2x_Sensor_Switch_select_state;
        if(a.state != event.newState) {
          console.info("12a state is " + a.state + " - Sending " + event.newState);
          a.sendCommand(event.itemState);
        }
        if(b.state != event.newState) {
          console.info("12b state is " + b.state + " - Sending " + event.newState);
          b.sendCommand(event.itemState);
        }        
    }, 'PT1S')
});

rules.JSRule({
  name: `Stairway 5`,
  id: `stairway5`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Stairway switches 5`,
  triggers: [triggers.ItemStateChangeTrigger('Light_5a_2x_Sensor_Switch_select_state'),triggers.ItemStateChangeTrigger('Light_5b_2x_Sensor_Switch_select_state')],
  execute: (event) => {
    console.info(">----NEW-12---" + event.itemName + " Received: " + event.newState)
    cache.private.get('ruleTimer12', () => ohrt.RateLimit()).run(() => {
        console.info("no rate limit - 12 - Received ON");
        var a = items.Light_5a_2x_Sensor_Switch_select_state;
        var b = items.Light_5b_2x_Sensor_Switch_select_state;
        if(a.state != event.newState) {
          console.info("12a state is " + a.state + " - Sending " + event.newState);
          a.sendCommand(event.itemState);
        }
        if(b.state != event.newState) {
          console.info("12b state is " + b.state + " - Sending " + event.newState);
          b.sendCommand(event.itemState);
        }        
    }, 'PT1S')
})

Though both of these rules are essentially identical and could be easily reduced into one rule which will be way easier to maintain in the long run.

const ohrt = require('openhab_rules_tools');

rules.JSRule({
  name: `Stairway 12`,
  id: `stairway12`,
  tags: [`_DYNAMIC_`],
  overwrite: `true`,
  description: `Schodowe 12`,
  triggers: [triggers.ItemStateChangeTrigger('Light_12a_2x_Sensor_Switch_select_state'),
             triggers.ItemStateChangeTrigger('Light_12b_2x_Sensor_Switch_select_state'),
             triggers.ItemStateChangeTrigger('Light_5a_2x_Sensor_Switch_select_state'),
             triggers.ItemStateChangeTrigger('Light_5b_2x_Sensor_Switch_select_state')],
  execute: (event) => {
    console.info(">----NEW-12---" + event.itemName + " Received: " + event.newState)
    cache.private.get('ruleTimer12', () => ohrt.RateLimit()).run(() => {
        console.info("no rate limit - 12 - Received ON");
        // Apply the associated items design pattern
        var a = (event.itemName.includes('a_')) ? event.itemName : event.itemName('b_', 'a_'); 
        var b = (event.itemName.includes('b_')) ? event.itemName : event.itemName('a_', 'b');
        if(a.state != event.newState) {
          console.info(a.name + " state is " + a.state + " - Sending " + event.newState);
          a.sendCommand(event.itemState);
        }
        if(b.state != event.newState) {
          console.info(b.name + " state is " + b.state + " - Sending " + event.newState);
          b.sendCommand(event.itemState);
        }        
    }, 'PT1S')
});

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