How to compare an item value with anothers

I want to compare brightness with a certain value in an item, if this is <= the value, a switch shoult turn on. But I can’t work this out:

configuration: {}
triggers:
  - id: "1"
    configuration:
      event: changed
      itemName: fiktive_helligkeit
    type: core.ItemStateChangeTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: fiktive_helligkeit
      state: daemmerungsschwelle_lichter
      operator: <=
    type: core.ItemStateCondition
actions:
  - id: "3"
    configuration:
      itemName: test_switch
      command: ON
    type: core.ItemCommandAction

I’ve tried to find a solution here, but I didn’t.

One approach is to set up a rule triggered by the value item.
I assume this documentation link might help you, it also takes tresholds into consideration

what do you mean exactly JensHauser?

I´m not sure how to interpret your use case, so let me give an example.

    1. “itemA” is the sensor item, so “itemA.state” is its value
  • “threshold” is the fixed threshold value. Could be an item or, like here, a fixed value
  • “test_switch” is a light bulb

In my mind, the switch needed to go ON and OFF, if being above/below “threshold”.

You could set up a rule, triggered by “itemA changed”. Advantage is that in this rule you could also switch the bulb OFF.
In the rule, let´s say in ECMAscript 11, I´d write something like this (just typed in, didn´t test)

var threshold=123
console.log("itemA="+itemA.state)

if (itemA.rawState >= threshold){
 console.log("Bulb on")
 items.test_switch.sendCommand("ON")
} else {
  console.log("Bulb off")
  items.test_switch.sendCommand("OFF")
}

Later, a hysteresis might be worth to be implemented to avoid switching on and off around the threshold. E.g., like this

Happy New Year!

Thanks for your answer, I think I should show you my case in this way. I defined a switch for dusk, which I set to a static value of 179 Lux. This means, if brightness falls to a value of 179 or below, the switch changes its status to ON.

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: KNXSicherheitWetter_Helligkeit
    type: core.ItemStateChangeTrigger
conditions:
  - inputs: {}
    id: "4"
    configuration:
      itemName: KNXSicherheitWetter_Helligkeit
      state: "179"
      operator: <=
    type: core.ItemStateCondition
  - inputs: {}
    id: "5"
    configuration:
      itemName: switch_daemmerung
      state: ON
      operator: "!="
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "3"
    configuration:
      command: ON
      itemName: switch_daemmerung
    type: core.ItemCommandAction

What I want now is to set a dynamic value with an setpoint-item (daemmerungsschwelle) that I can control without changing the rule itself. I’ve tried several approches, but I can’t get this work.

I don´t think this way leads to success.
In my understanding the trigger needed to be an event, not a treshold.

The scale transformation service could be something for you or this thread.

See Struggling with threshold variables for rule in OH3

I solved it by writing a rule (see above). Think also of switch your test_switch back to OFF

when you add the condition, choose “script condition” instead of “item condition”

Maybe worth to mention that a new item (type number) is necessary, which should e.g be placed on a page with a “Knob & Rounded Slider” cell. So at least here you can adhust the item value to later compare it.

My example in Post#5 illustrates the current functionality. However, as I mentioned, I intend to use a setpoint item to manage the threshold for when the item ‘switch_daemmerung’ should switch to ON. In this case, I’ll require a Knob & Rounded Slider. Couldn’t it be considered an event when a value falls below a certain level?

@Oliver2: I’m not sure what you mean. It appears I may have been too vague; however, the rule I created is functioning as intended.

In order to to use a setpoint-item I think it might work if you selected in “But only if condition” an inline script instead of item. This way you can replace the setpoint value by an item state

I tried, but unfortunately, that doesn’t work either.

I did a test and it works without any problems. Please post your rule.

configuration: {}
triggers:
  - id: "1"
    configuration:
      event: changed
      itemName: fiktive_helligkeit
    type: core.ItemStateChangeTrigger
conditions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: fiktive_helligkeit.state as Number <= daemmerungsschwelle_lichter.state
        as Number
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "3"
    configuration:
      command: ON
      itemName: test_switch
    type: core.ItemCommandAction

I am not a DSL expert, maybe set brackets to make sure comparison works right:

(fiktive_helligkeit.state as Number) <= (daemmerungsschwelle_lichter.state as Number)

Furthermore, could you log out
(fiktive_helligkeit.state as Number).toString()
and
(daemmerungsschwelle_lichter.state as Number).toString()
so that we can see the result (UoM, etc)

Incredible, it works! I spent weeks trying to get it working. Thank you very much!
brackets, lol

1 Like

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