Getting "only if" statements on items with multiple stats

I’m trying to code some rules affecting my trandfri lights.

On “simple” lights it’s easy. Also on color temperature lights it’s pretty basic.
But when adding a RGB light, you get only 1 channel, which provide 3 numbers for color, saturation and brightness, just like “10,90,10”.

Therefore you can send “simple” commands like “ON” and “OFF”, which will do, what the tame say (providing the last known state on “ON”, which was before “OFF”).
But now i want to create 2 rules, which will just turn the light on and off, when a button is pressed.
As there is only one power button, i need to togge the light, which i thought would be easy, when i do like “when BUTTON pressed, then turn light ON, but only if it was OFF” and vica versa …
But the “but only if” statement does not work, casue (i think) it gets this type of number as feedback, and not the state ON/OFF.
Now there might be a very complex way, i thought of to get a work around (like creating 3 virtual items, and a transformation, which will split this one channel in 3 items, to controll them separatly), but maybe you got a simple idea for me, to manage this?

Hi @renegade2k ,

I don’t know the IKEA(??) trandfri lights. But I have some Hue and a Paulmann light with the Hue-binding. Both provide a channel that can be used as a switch

e.g. Hue

Switch  Einfahrt_3_Toggle       "Einfahrt3 Schalter"      <lightbulb>         { channel="hue:0220:1:bulb3:brightness" }

or Paulmann

Switch  LED1_Toggle     { channel="hue:0210:1:LED1:color" }

What binding are you using and what channels does this binding provide?

I suspect that is probably the case, the built in trigger evaluator is not clever enough to reduce the complex Color type Item to an ON/OFF state.

You don’t need proxy doo-dahs though, just a cleverer rule.
Whenever button pressed …
enter a script that does the comparison, because there you can evaluate a Color state as ON/OFF easily.
Something like

if (myItem.getStateAs(OnOffType) == ON) {
   myItem.sendCommand(OFF)
} else {
   myItem.sendCommand(ON)
}

should also take care of initial conditions like NULL or UNDEF, which can trip up toggling rules.

1 Like

To continue rossko57’s answer, if you only want to allow a Rule to run if the light is ON in a Condition (i.e. the But only if… clause) you would need to use a Script Condition. Then the code would be:

Rules DSL

myItem.getStateAs(OnOffType) == ON

JavaScript

ir.getItem("myItem").getStateAs(OnOffType) == ON;

Note, you might need to import OnOffType but I think this will work. If it doesn’t the Script Condition would be

var OnOffType = Java.type("org.openhab.core.library.types.OnOffType");
ir.getItem("myItem").getStateAs(OnOffType) == ON;

The rule will only run if myItem is ON.

1 Like

@rossko57 & @rlkoshak

Rules DSL

myItem.getStateAs(OnOffType) == ON

Super easy and works like a charm.
Thanks guys :slight_smile:


PS: i don’t know whether this also fits the topic:
My next point to control the light is to dim it.
Easier point at this: I got 2 buttons. 1 to increase and 1 to decrease the brightness.
More complex point: i need to modify just the 3rd value of the stats. So “send command UP” does not work, as (i guess) OH does not know which attribute it has to increase.
Also the most common solution to this, which i found in the community is like

myDimmerItem.sendCommand((myDimmerItem.state as Number) - 5)

Which also does not work, cause OH can’t obviously get the state as one number.

It would be informative to future users if you posted the YAML of the full rule here so they can see the condition in context.

That is actually something that should be handled and handled by the binding typically. As with Switch, a Color Item can be commanded as if it were a Dimmer as well. So you should be able to send Dimmer type commands. But UP and DOWN are not commands supported be a Dimmer. You want INCREASE and DECREASE.

Just like you got the state as an OnOffType, you can get the state as a PercentType (the type carried by a Dimmer).

myItem.getStateAs(PercentType) as Number

And you can send a single number between 0 and 100 as a command to the Color Item and it will interpret it as a Dimmer command.

Furthermore, the HSBType (the state actually carried by the Color Item) has a bunch of methods and properties to get at the individual values and to convert them between formats (e.g. convert the HSB to RGB). See HSBType (openHAB Core 3.1.0-SNAPSHOT API), notice there is a getBrightness() method.

1 Like

Sure. This is what it looks like:

triggers:
  - id: "1"
    configuration:
      itemName: Remote_Button
      state: "1002"
    type: core.ItemStateUpdateTrigger
conditions:
  - inputs: {}
    id: "4"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: Lamp_color.getStateAs(OnOffType) == OFF
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: Lamp_color
      command: 10,90,10
    type: core.ItemCommandAction

This rules affects the lamp, which will turn on to “color 10”, “saturation 90” (which is pretty much like red, as seen in the first post) with brightness 10%

ehm … wow :grimacing:
I’ve tried this too, as i found something about it in the cumminity too, but it didn’t work and the guys exapmle didn’t match my problem that much, so i forgot about it pretty fast …
But indeed, INCREASE does what the name says … the first step is only 2%, but every following step is 10%, so it’s ok for me and i don’t need to modify this even more …

Also here the exampe with full code :wink:

triggers:
  - id: "1"
    configuration:
      itemName: Remote_Button
      state: "2002"
    type: core.ItemStateUpdateTrigger
conditions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: Lamp_color.getStateAs(OnOffType) == ON
    type: script.ScriptCondition
actions:
  - inputs: {}
    id: "2"
    configuration:
      itemName: Lamp_color
      command: INCREASE
    type: core.ItemCommandAction

And again, thanks for the quick hint :slight_smile:

PPS: there seems to be a little bug somewhere, as when i turn on the lamp and change the brightness and want to turn it off, OH handles it like a “turn on” and put the brightness to 10%, but with the second press i can turn it off nevertheless. So it’s not perfect, but works so far :smile:

Often stuff like that is caused by just the way the binding and technology work. Some handle that sort of thing better than others. If you really think it’s a bug, you can file it on the binding, but generally it’s not something that can be fixed. The binding developers tend to be users of the same binding so stuff like that usually gets fixed when it is fixable.

1 Like