[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

http.cfg:

# deCONZ lights
LightsCache.url=http://127.0.0.1:8090/api/0950775C33/lights
LightsCache.updateInterval=2000

deconz.items:

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"
when
    Item OMRbedPolledState changed
then
    if (trigerringItem.state == "false") ...
    else ...
end

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;
})(input)

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.