OH 4.1.1 rule condition not working

I have a rule that turns on music in my workshop but only if the lights are on. The rule turns everything on fine, but is ignoring the status of the lights.

configuration: {}
triggers: []
conditions:
  - inputs: {}
    id: "4"
    configuration:
      itemName: Workshop_Lights_Power
      state: ON
      operator: =
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "3"
    configuration:
      itemName: JazzMusic
      command: OFF
    type: core.ItemCommandAction
  - inputs: {}
    id: "5"
    configuration:
      itemName: PeterNeroMusic
      command: OFF
    type: core.ItemCommandAction
  - inputs: {}
    id: "6"
    configuration:
      itemName: YanniMusic
      command: OFF
    type: core.ItemCommandAction
  - inputs: {}
    id: "2"
    configuration:
      itemName: Workshop_Echo_TextCommand
      command: Play iheart 70s on Workshop
    type: core.ItemCommandAction
  - inputs: {}
    id: "7"
    configuration:
      itemName: _70sMusic
      command: ON
    type: core.ItemCommandAction


From the docs:

These rules take the high level form of When t happens, if c then do a , where t are the triggers that cause the rule run, c are the conditions that must be true for the rule to run, and a are actions to perform when the rule is triggered and allowed to run.

The rule has no triggers. Without triggers there’s no event to cause the rule to actually run. In this case you’d want to trigger the rule when Workshop_Lights_Power changes to ON. (Note, as currently implemented with this trigger you won’t need the condition)

And you’ll need another rule to handle turning off the music when Workshop_Lights_Power changes to OFF.

Or you can handle it all in one rule but you’ll need to use a Script Action to turn things on/off based on what Workshop_Lights_Power has changed to.

1 Like

I am using a label card in my Overview page to trigger the rule.

Maybe if I do it as a script instead of a rule?

Thanks

When you trigger the rule directly from the UI like that the condition is not applied.

A Script is just a rule without triggers or conditions. It doesn’t really fix the problem.

You’ll need to use a script action and test the state of the Item and do the appropriate action based on the state of the Item. You can’t use a condition for a rule triggered in this way.

I got it to work with using the scripts.

if (items.getItem('Workshop_Lights_Power').state != 'OFF') {
  items.getItem('Workshop_Echo_TextCommand').sendCommand('Play iheart 70s on Workshop');
  items.getItem('_70sMusic').sendCommand('ON');
  items.getItem('JazzMusic').sendCommand('OFF');
  items.getItem('PeterNeroMusic').sendCommand('OFF');
  items.getItem('YanniMusic').sendCommand('OFF');
}

I changed the Label Card to run the script instead of the rule.

It will not play the music with the lights off now.

I just did not want to take a chance on the music playing when I was out of the room and accidently click the label from another location.

Here is what my screen looks like.

Just to make sure you understand the parts of OH rules, you could have used the exact same Blockly code in the original rule in place of the commands to the Items. Just choose “Inline Script” as the Action. Give how you are using this it makes sense to have this be a Script. I just don’t want you or future readers thinking that the only way to code a script like this is in the Settings → Scripts. Code like this can be an Action or even a Condition in Settings → Rules as well.

1 Like

I do use many scripts in my rules as well. But those all have triggers, and the conditions work well.
My favorite one is for the gates around my house. If a gate is opened after “dusk”, it turns on multiple lights, sends me a notice on my phone and cancels a share timer. Then if the gate remains open the lights stay on. When the gate is closed it waits 10 minutes and turns off all the lights. But if the gate is opened back up while the lights are on it resets the timer back to 10 minutes.

Thank you for your help in pushing me in the right direction.

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