Http Binding jsonpath Volkszaehler

  • Platform information:
    • Hardware: Docker Image on Synology latest Version
    • OS: what OS is used and which version
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 3.2.
  • Issue of the topic: How to split result from json?
    Hi all
    I want to transfer my existing OH2 into the new OH and want to collect data from my volkszähler.
    IN OH2 I have a Item and a rule to collect the data
    Example OH2
    String Strom_tuples “Tiple Haushalt [%.1f W]” (Current) { http="<[http://192.168.xx.xx/middleware.php/data/.json?from=now&options=raw:60000:JSONPATH($.data.tuples)]" }
    Number Strom_Stand “Zaehlerstand Haushalt [%.1f kWh]” (Current, Current_Chart)

and within a rule I extract the value for the Item “Strom_Stand”
rule “Tuples in Teilstring”

when

Item Strom_tuples changed

then

    var String strTeil2 = Strom_tuples.toString.split(",").get(2)

Strom_Stand.sendCommand(strTeil2)

    //logInfo("Strom", "Tuple getrennt")

end

OH3 Part
What I have created as channel

  • id: Strom_tuples
    channelTypeUID: http:string
    label: Strom_tuples
    description: “”
    configuration:
    mode: READONLY
    stateTransformation: JSONPATH:$.data.tuples

But in the logfile I have this:
Could not transform state ‘UNDEF’ with function ‘JSONPATH:$.data.tuples’ and format ‘%s’

As value I receive in OH2
“12912989182,12222.222” (example)
How I have to configure the channel in OH3 that I can extract the second value (12222.222 from the example)

Hope, somebody can help

You can chain transformations. One solution would be to append a Regex transformation that extracts everything after the comma.

Hi Jan
thanks, but how I have to configure the channel?
Do you have a example?

BR Uwe

Can you provide a full sample of the complete JSON that you expect to receive?

Doesn’t this mean that the binding hadn’t actually pulled in the data that you want in the first place? Perhaps create a temporary String Channel that subscribes to the same URL and don’t add any transformations, just to make sure you are actually getting the data into openHAB.

Hi
another channel works as designed and deliver values, but the difference is the channelTypeUID: http:number

I’m not really sure what type of channelTypeUID I have to use, in OH2 I defined the Item “Strom_Tuple” as string

I believe, that the right type could be the solution

Looks to me like you may have tried to use profiles with JSONPATH on your link(s) between channel and Item? Don’t do that, use default profile and do your transformation in only one place, the channel.

That’s a guess, always worth showing us the complete error log message.

1 Like

Meanwhile I dropped the channel and recreated the channel with the gui. And: it works :slight_smile:

But now I’m struggeling with the rule
I have dropped the existing rule-file in the rules folder
But in the logfile I see only
Script execution of rule with UID ‘strom-1’ failed: The name ‘Strom_tuples’ cannot be resolved to an item or type; line 5, column 31, length 12 in strom

Simple question:
How I can convert the OH2 Rule into a OH3 rule?
Important is here “Strom_tuples.toString.split(”,").get(2)" to put the result into the item “Strom_Stand”

So I have tried to create a new rule
configuration: {}
triggers:

  • id: “1”
    configuration:
    itemName: Smartmeter_Stromtuples
    type: core.ItemStateChangeTrigger
    conditions: []
    actions:
  • inputs: {}
    id: “2”
    configuration:
    itemName: Strom_Stand
    command: Strom_tuples.toString.split(",").get(2)
    type: core.ItemCommandAction

But nothing happens, nothing in the logs and no new value in the item

any idea? At the moment I’m helpless

Can you

I’m pretty sure you can do this all within the Channel - no rule required. Please share the JSON in between code fences.

{"version":"0.3","data":{"tuples":[[1641058597253,12215.447,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641058596347,"to":1641058597253,"average":0,"consumption":0,"rows":2}}

This is the json I receive in the channel
with $data.tuples I collect then “1641058597253,12215.447,1”
But I need only the second part, this is the actual meter reading

JSONPATH:$.data.tuples∩REGEX:,(.*) should be a transformation that first extracts the tuple and then cuts off everything before (and including) the comma.

1 Like

Make sure you have the REGEX Transformation Service installed to use @J-N-K 's example!

1 Like
2022-01-01 17:56:45.722 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641059755351,12215.68,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641059754256,"to":1641059755351,"average":0,"consumption":0,"rows":2}}' with function '$.data.tuples∩REGEX:,(.*)' and format '%s'
2022-01-01 17:56:56.084 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641059815364,12215.692,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641059814241,"to":1641059815364,"average":0,"consumption":0,"rows":2}}' with function '$.data.tuples∩REGEX:,(.*)' and format '%s'
2022-01-01 17:57:56.074 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641059875256,12215.703,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641059874363,"to":1641059875256,"average":0,"consumption":0,"rows":2}}' with function '$.data.tuples∩REGEX:,(.*)' and format '%s'
2022-01-01 17:58:56.086 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641059935350,12215.715,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641059934255,"to":1641059935350,"average":0,"consumption":0,"rows":2}}' with function '$.data.tuples∩REGEX:,(.*)' and format '%s'
2022-01-01 17:59:56.074 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641059995242,12215.726,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641059994350,"to":1641059995242,"average":0,"consumption":0,"rows":2}}' with function '$.data.tuples∩REGEX:,(.*)' and format '%s'
2022-01-01 18:00:56.675 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641060053350,12215.737,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641060052242,"to":1641060053350,"average":0,"consumption":0,"rows":2}}' with function '$.data.tuples∩REGEX:,(.*)' and format '%s'
2022-01-01 18:01:23.140 [WARN ] [ofiles.JSonPathTransformationProfile] - Could not transform state '{"version":"0.3","data":{"tuples":[[1641060053350,12215.737,1]],"uuid":"d397bbc0-6df1-11eb-8215-dd3268773128","from":1641060052242,"to":1641060053350,"average":0,"consumption":0,"rows":2}}' with function 'jsonspath:$.data.tuples∩REGEX:,(.*)' and format '%s'


Now nothing works, see the logfile
I configured one time with leading jsonpath and without leading jsonpath

Update: REGEX is installed

Don‘t use the profile. Use the stateTransformation in the channel config.

1 Like

Maybe I’m lost now
I have a channel, to this channel I have a new item linked which I configured as JSONPATH

Only there I can add the regex

But I create a new channel via code

One time this way:

- id: tupletest
    channelTypeUID: http:number
    label: tupletest
    description: null
    configuration:
      mode: READONLY
      commandTransformation: REGEX:,(.*)
      stateExtension: "&options=raw"
      stateTransformation: JSONPATH:$.data.tuples

and one time

- id: tupletest2
    channelTypeUID: http:number
    label: tupletest2
    description: null
    configuration:
      mode: READONLY
      stateExtension: "&options=raw"
      stateTransformation: JSONPATH:$.data.tuples∩REGEX:,(.*)

But nothing happens

The second one should work. Did you set the profile to “Standard”? If you still have a profile, that’s wrong.

Hi Jan
in OH3 I’m a novice
what does it mean with profile?

I have testwise create a new thing
maybe I make in the creation a mistake?

UID: http:url:3a933c1e3b
label: Tuplesplittest
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://192.168.68.85/middleware.php/data/d397bbc0-6df1-11eb-8215-dd3268773128.json?from=now
  delay: 0
  stateMethod: GET
  refresh: 30
  commandMethod: GET
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: tupletest2
    channelTypeUID: http:number
    label: tupletest2
    description: null
    configuration:
      mode: READONLY
      stateExtension: "&options=raw"
      stateTransformation: JSONPATH:$.data.tuples∩REGEX:,(.*)

I should have had a better look at your JSON first. You only need JSONPATH:

JSONPATH:$.data.tuples[0][1]

Use that as stateTransformation. It’s an array of arrays, you need to define the indices correctly and everything works out automatically. No need to split any strings.

For reference:

JSONPATH:$.data.tuples∩REGEX:.*?,([\d\.]*).*?

would also work.

2 Likes

Thank you so much, this save me a lot of searching, this was the solution
But what I see is that I have to dive deeper into OH 3

Have a nice rest of the evening and

Happy new Year

1 Like