JsonPath transformation as Profile

  • Platform information:
    • Hardware: amd64 vm > docker
    • OS: docker
    • Java Runtime Environment: whatever is in the official docker image
    • openHAB version: 3.4.2
  • Issue of the topic: please be detailed explaining your issue

I have my thing configuration:

Bridge mqtt:broker:mqttbroker [ host="domain", protocol="TCP", qos=2, secure=false, clientid="openhab_mqttBridge", keepAlive=60, enableDiscovery=true ]

Thing mqtt:topic:mqttchannels "MQTT Channels" (mqtt:broker:mqttbroker) {
    Channels:
    Type string : washer "Waschmaschine" [ stateTopic="zigbee2mqtt/Waschmaschine" ]
}

And the item config:

Number washerpower "Waschmaschine Watt" <washingmachine_2> ["Measurement", "Power"] { channel="mqtt:topic:mqttchannels:washer"[profile="transform:JSONPATH", function="$.power"] }

The data on the mqtt topic is this:

{
  "child_lock": "UNLOCK",
  "current": 0,
  "indicator_mode": "off/on",
  "last_seen": "2023-04-29T22:37:08+02:00",
  "linkquality": 48,
  "power": 0,
  "power_outage_memory": "restore",
  "state": "ON",
  "update": {
    "installed_version": 77,
    "latest_version": 192,
    "state": "available"
  },
  "voltage": 233
}

when is the JSON transformation applied? Before the data reaches the item or after?
The thing is the json is a string and my item is configured as Number.
The end result with $.power would be a number but I only get NULL with this config.

A profile specifically applies to the link between the channel and the item, so the value is transformed before it reaches the item.

Do you have the JSONpath transform installed already?

1 Like

Ok that was what I think it does. So before its a string (ofc. json) and after its a number like the item.

Yes I use it in other channels. But I never used it in the link with a profile.

Just to be sure its this one right? JsonPath - Transformation Services | openHAB

That’s the correct transform, and your configuration looks correct to me. The next thing I would test would be to double check and make sure that the channel is actually returning the json string that you are expecting. If the JSONpath transform fails to find a match for your path definition then it returns the whole json string, not NULL. The fact that you are still getting NULL is an indication that possibly the channel is sending NULL in the first place.

1 Like

I changed the Item type to String then I get the full JSON object as the value, as you are describing the whole json string is returned in case the transforrm failes to find a match?

Thats thechanged item where I get the full json as the value:

String washerpower "Waschmaschine Watt" <washingmachine_2> ["Measurement", "Power"] { channel="mqtt:topic:mqttchannels:washer"[profile="transform:JSONPATH", function="$.power"]}

Also looks fine to me in the UI:
Link:

Item:

The N/A in the State Format field is unusual. I don’t use text definitions, but if you use the UI to create the profile, then State Format remains blank:

  1. Do you see any errors in the logs when this item changes?

  2. Can you go to the API Explorer and under Links → Get put the item name washerpower in the itemName string field? It should return a JSON like this that has no stateFormat key:

[
  {
    "editable": true,
    "channelUID": "http:url:ffd3ed03ac:Incoming_Message",
    "configuration": {
      "function": "$.ocs.data[1].referenceId",
      "profile": "transform:JSONPATH"
    },
    "itemName": "NextcloudChat_IncomingMessage_ID"
  }
]
1 Like
  1. No there are no errors when I change this item, I just see that the item got changed.

  2. Looks good to me…?

[
  {
    "editable": false,
    "channelUID": "mqtt:topic:mqttchannels:washer",
    "configuration": {
      "profile": "transform:JSONPATH",
      "function": "$.power"
    },
    "itemName": "washerpower"
  }
]

Edit1:

Also tried to add the Default state for stateFormat:

[
  {
    "editable": false,
    "channelUID": "mqtt:topic:mqttchannels:washer",
    "configuration": {
      "sourceFormat": "%s",
      "profile": "transform:JSONPATH",
      "function": "$.power"
    },
    "itemName": "washerpower"
  }
]

Still the same result

Edit2:
I also just read in the docs


But I get null in return, is that a bug? Or is this behaviour because I try to put the output result into a Number thus resulting in null?

OK the solution was to restart openhab now it is working…

Here my working item config if somebody needs it after all =)

Number:Power washerpower "Waschmaschine Watt" <washingmachine_2> ["Measurement", "Power"] { channel="mqtt:topic:mqttchannels:washer"[profile="transform:JSONPATH", function="$.power", sourceFormat="%s"]}

Thank you for your help @JustinG

1 Like