[Solved] Clever way to convert 'true'/'false' strings to ON/OFF switch

I have a http.cfg that polls my Zigbee lights state every 2000ms


# deCONZ lights


String OMRbedPolledState { http="<[LightsCache:900:JSONPATH($.29.state.on)]" }

My rule then does this:

if (OMRbedPolledState.state == "false") { ... }

Is there a clever way to translate the ‘true’/‘false’ string Item to a Switch item?

Unfortunately you can’t “nest” transformation services
So your solution is the correct one. There are no short cuts that I am aware of in this case.

rule "Zigbee"
    Item OMRbedPolledState changed
    if (trigerringItem.state == "false") ...
    else ...

You could use JavaScript to do the trick. As I myself am not very familiar with JavaScript, I can’t give you a working code, but the idea wqould be to do the jsonpath within the JavaScript, then set the output to ON or OFF depending on the result.

At least there are some examples here in the community…

1 Like

Yes of course, A JAVASCRIPT transform
I use them quite a bit…

Let’s go:
Your item:

Switch OMRbedPolledState { http="<[LightsCache:900:JS(zigbee.js)]" }

A new file called zigbee.js in your transform folder
The Javascript transformation installed!!

(function(dataString) {
var data = JSON.parse(dataString);
var lightState = data['29'].state.on;
var reply = "";
if (lightState) reply = "ON";
if (!lightState) reply = "OFF";
return reply;

That should work!
WARNING untested!! :smile:

Not any more :slight_smile:
Worked straight out of ‘the box’.
Very nice, and dare I say clever. Thanks both.

Now, the JavaScript is not generic for all my 31+ lights, but not all lights need polling.
This one does though, because when I turn off my reading light with the Ikea remote, the missus’s light on the other side of the bed is also turned off. Thanks again.
OpenHab makes it possible to learn something new (almost) every day.