Help with JSONPATH

Tags: #<Tag:0x00007f6173bd4468> #<Tag:0x00007f6173bd43a0> #<Tag:0x00007f6173bd42d8>

Hi,

Please help me. How I can transform JSON from MQTT topic (hsv):
{
“h”: 270,
“s”: 96,
“v”: 100
}

to correct hsv value “270,96,100” for OpenHab?

I try to do:
JSONPATH:$.h,JSONPATH:$.s,JSONPATH:$.v
and
JSONPATH:$.h,$.s,$.v

but it doesn’t work :frowning:

What are you trying to achieve here, please?
Items, devices, payloads…

I have controller for LED strip. OpenHab know it via WiFi Led Binding.
I have Yandex Alice home assistant. It may transform voice commands to MQTT.
I want change color of my LED strip by voice.
I make two things in PaperUI - one for controller (it is work) and one virtual via MQTT binding for Alice.
In my virtual thing I make channels for on-off (work), brightness (work) and HSV color - doesn’t work, because I have JSON with H,S and V and don’t know that I can do wit it :slight_smile:

Use a String channel and get the whole json string in a String Item linked to that channel
Use a rule to extract the 3 values by using the transform action
Parse the 3 values and send them to the LED strip color channel

Ok, I make new channel and already have string {“h”:225,“s”:96,“v”:100}. How extract values? I don’t find transform in rules… May be you can send me address of manual or examples… I’m true beginner, I know.

I don’t know if this is a copy/paste issue or not, but these are the wrong double quote characters. I think the jsonpath transform will choke on those double quote characters.

This works in a rule

    val String sJson = '{ "h": 270, "s": 96, "v": 100 }'
    val String h = transform("JSONPATH", "$.h", sJson)
    val String s = transform("JSONPATH", "$.s", sJson)
    val String v = transform("JSONPATH", "$.v", sJson)
    logInfo("test", "hsv={},{},{}", h, s, v)

Thank’s. This code not for rules in Paper UI, but for native rules, as I understand. Ok,I try to learn native rules now :slight_smile:

I think this could be done with the JavaScript transform without a Rule.

(function(i) {
    var hsb = JSON.parse(i)

    return hsb.h+","+hsb.s+","+hsb.b
})(input)

Apply that to the incoming topic on the Channel.

2 Likes