Rule that depends on light value delivered by Hue Binding's light sensor

Hello,

I have a Philips hue motion and light sensor that I use via the hue binding in OpenHAB 4.0.

In OpenHAB 2, I could write a rule that depended on the light delivered by the sensor very simply, e.g. using a condition like

I_OG_Deckensensor_Helligkeit.state < 2

However, in OpenHAB 4 this does not work anymore, cause the Item now contains a value that consists of the number and the appended “lx” for “lux”, e.g. “0 lx”. This somehow cannot be handled by me in an OpenHAB rule anymore because everything I try leads to exceptions, e.g.

val int helligkeit = Integer::parseInt(I_OG_Fl_Deckensensor_Helligkeit.state)

results in

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule failed: For input string: “0 lx” in lights

There seems to be no way to extract the number from that number + string concatenation delivered by the sensor to perform comparisons on that. I also tried simple castings, usage of .toString and more, it just does not work. Any ideas how to proceed?

Thanks, Torsten

Oh, we’re just discussing something like that over here.

It’s not that complex, it’s called units of measurement (UoM) or QuantityType, so the value is followed by the unit.
There are two options to work with UoM, either strip the unit or add it.

val helligkeit = (I_OG_Fl_Deckensensor_Helligkeit.state as Number).intValue

will strip the unit.
It’s better not to use primitives. int is a primitive, while Integer is an object, but you don’t need to set the type of the value at all.

The other option is this:

if(I_OG_Fl_Deckensensor_Helligkeit.state < 2 | lx)

Sometimes you’ll have to use quotes:

if(I_OG_Fl_Deckensensor_Helligkeit.state < 2 | "lx")

and to be honest, I prefer the first option :slight_smile: but both are valid.

1 Like

Thanks a lot, worked for me!
I still would call it quite complex, though! :slight_smile:

Yes, but it’s not that it would need another thread to study :slight_smile:

The whole point is to understand the difference between (for instance) 15 and 15 °C.