Rule sequence "WHEN" "BUT ONLY IF" triggers not as expected

Hi,

I do have a device called “AC_AUTOMATIC_TEST_LED” which I want to switch on automatically under the following condition:
“AC_AUTOMATIC_TEST_LED” shall get switched ON if “AUTOMATIC_TEST_ON_OFF_SWITCH = ON” AND “SF_Power_total < -2000”

The code below work half way only.
It does switch the device on if there is a certain sequence only.

a) This sequence works:

  1. “SF_Power_total < -2000” (threshold reached)
  2. “AUTOMATIC_TEST_ON_OFF_SWITCH” = ON

b) This sequence does not work:

  1. “AUTOMATIC_TEST_ON_OFF_SWITCH” = ON
  2. “SF_Power_total < -2000” (threshold not yet reached)
  3. “SF_Power_total < -2000” (threshold reached)
    What can I do to make “b” work too?

configuration: {}
triggers:

  • id: “2”
    configuration:
    itemName: AC_AUTOMATIC_TEST_ON_OFF_SWITCH
    state: ON
    previousState: OFF
    type: core.ItemStateChangeTrigger
    conditions:
  • inputs: {}
    id: “1”
    configuration:
    itemName: SF_Power_total
    state: “-2000”
    operator: <
    type: core.ItemStateCondition
    actions:
  • inputs: {}
    id: “4”
    configuration:
    itemName: AC_AUTOMATIC_TEST_LED
    command: ON
    type: core.ItemCommandAction

The rule will only trigger if the trigger condition is met, so the Switch Item changed it’s state from OFF to ON.
The Action will only be performed if the BUt Only If Condition is met, so the Number has to be less than -2000.

So either build a second rule or add some stuff to your rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: AC_AUTOMATIC_TEST_ON_OFF_SWITCH
      state: ON
    type: core.ItemStateChangeTrigger
  - id: "2"
    configuration:
      itemName: SF_Power_total
    type: core.ItemStateChangeTrigger
conditions:
  - id: "3"
    configuration:
      itemName: AC_AUTOMATIC_TEST_ON_OFF_SWITCH
      operator: =
      state: ON
    type: core.ItemStateCondition
  - id: "4"
    configuration:
      itemName: SF_Power_total
      operator: <
      state: "-2000"
    type: core.ItemStateCondition
actions:
  - id: "5"
    configuration:
      itemName: AC_AUTOMATIC_TEST_LED
      command: ON
    type: core.ItemCommandAction

This will result in many ON commands (whenever Switch is ON and Number changes to less than -2000), so maybe add another But Only If, that is, the Switch Item AC_AUTOMATIC_TEST_LED state is OFF

Just to elaborate on @Udo_Hartmann’s example. The rule only attempts to run based on the triggers. Your a) works because the rule is triggered by the Switch changing. At that time your condition was true and the actions ran.

Your b) doesn’t work because changes to the Number Item do not trigger the rule to run. The only time the rule ran was when the switch changed and at that time the condition was false. If you want the rule to run if the number changes after the switch changes to ON, you must trigger the rule when the number changes too.

1 Like

Thank you, this works.
Now I would like to automtically switch it OFF when SF_Power_total is close to -100.

itemName: SF_Power_total
operator: >
state: “-100”

How can I do that?
And is it possible to add a timer to switch OFF when the condtion is met for min. 20 seconds?

Very much like you did before. Trigger the rule when the SF_Power_total changes. In the condition check to see if SF_Power_total is < -100 and optionally when the switch is ON. For the action command the switch to OFF.

All rules follow this pattern.

  1. Trigger: Something happens (in this case the SF_Power_total changes)
  2. Condition: Check to see if we need to do something (in this case SF_Power_total < -100 and the switch is ON)
  3. Action: Do something (in this case command the Item to OFF)

It’s definitely possible but it means you need to get into Timers which is going to become much more complicated. At a high level:

  • trigger the rule when SF_Power_total changes
  • no conditions
  • in the Action you need to choose “run a script” and create code that does the following (Blockly is an option)
    • if SF_Power_total is < -100, the switch is ON, and a Timer doesn’t already exist create a Timer for 20 seconds which commands the switch to OFF
    • if SF_Power_total is < -100 and the switch is ON and the Timer does exist, exit doing nothing
    • if SF_Power_total is < -100, the switch is OFF and the Timer exists or SF_Power_total >= -100, cancel the timer.

Thx, will try this weekend.