Read value from JSON-response

Hello all,

After multiple hours of reading and trying, I decided to ask you guys for help.

I’d like to read the actual power of my ‘HomeWizard Energy Sockets’. When I do a GetRequest to http://192.168.3.8/api/v1/data/ I get the following response:

image

As I’m only interested in ‘active_power_w’ I’d like to read this specific value, but I can’t get this done.

I wrote this rule:

rule "get active power"
when
    Item TEST_HomeWizard_GetEnergySocketValue changed
then
    var String json = sendHttpGetRequest("http://192.168.3.8/api/v1/data/")

    var String value = transform("JSONPATH", "$.active_power_w", json)
    
    logInfo("JSON_ReturnValue", value )

end

I only want ‘active power’ to be written in the logging, but whatever I try, I do continuously get the whole response printed:

2024-11-17 15:15:29.410 [INFO ] [b.core.model.script.JSON_ReturnValue] - {"wifi_ssid":"H369AC1BF55_IoT","wifi_strength":54,"total_power_import_kwh":306.190,"total_power_import_t1_kwh":306.190,"total_power_export_kwh":0.000,"total_power_export_t1_kwh":0.000,"active_power_w":28.313,"active_power_l1_w":28.313,"active_voltage_v":231.313,"active_current_a":0.151,"active_frequency_hz":50.010}

Does anyone have any idea what I do wrong?

This is really strange…
is the jsonpath addon installed?

And you could do this per http binding I think…

Greets…

First things first: JSONPATH installed?
Then: Why using a rule at all? You won’t need no rule, but the http addon and the jsonpath addon, then configure a http thing:

Thing http:url:sockets "HomeWizard Energy Socket" [
    baseURL="http://192.168.3.8/api/v1/data/",
    refresh=60
] {
    Channels:
    Type string : json     "JSON"                   []
    Type number : strength "Signal strength"        [stateTransformation="JSONPATH:$.wifi_strength",            unit="%"]
    Type number : inTotal  "total import"           [stateTransformation="JSONPATH:$.total_power_import_kwh",   unit="kWh"]
    Type number : inT1     "t1 import"              [stateTransformation="JSONPATH:$.total_power_import_t1_kwh",unit="kWh"]
    Type number : exTotal  "total export"           [stateTransformation="JSONPATH:$.total_power_export_kwh",   unit="kWh"]
    Type number : exT1     "t1 export"              [stateTransformation="JSONPATH:$.total_power_export_t1_kwh",unit="kWh"]
    Type number : actPower "active power"           [stateTransformation="JSONPATH:$.active_power_w",           unit="W"]
    Type number : actPowL1 "active power L1"        [stateTransformation="JSONPATH:$.active_power_l1_w",        unit="W"]
    Type number : actV     "active voltage"         [stateTransformation="JSONPATH:$.active_voltage_v",         unit="V"]
    Type number : actC     "active current"         [stateTransformation="JSONPATH:$.active_current_a",         unit="A"]
    Type number : actF     "active frequency"       [stateTransformation="JSONPATH:$.active_frequency_hz",      unit="Hz"]
}

This is yaml style:

UID: http:url:sockets
label: HomeWizard Energy Socket
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://192.168.3.8/api/v1/data/
  delay: 0
  stateMethod: GET
  refresh: 60
  commandMethod: GET
  timeout: 3000
  bufferSize: 2048
channels:
  - id: last-failure
    channelTypeUID: http:request-date-time
    label: Last Failure
    configuration: {}
  - id: last-success
    channelTypeUID: http:request-date-time
    label: Last Success
    configuration: {}
  - id: json
    channelTypeUID: http:string
    label: JSON
    configuration:
      mode: READWRITE
  - id: strength
    channelTypeUID: http:number
    label: Signal strength
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.wifi_strength
      unit: "%"
  - id: inTotal
    channelTypeUID: http:number
    label: total import
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.total_power_import_kwh
      unit: kWh
  - id: inT1
    channelTypeUID: http:number
    label: t1 import
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.total_power_import_t1_kwh
      unit: kWh
  - id: exTotal
    channelTypeUID: http:number
    label: total export
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.total_power_export_kwh
      unit: kWh
  - id: exT1
    channelTypeUID: http:number
    label: t1 export
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.total_power_export_t1_kwh
      unit: kWh
  - id: actPower
    channelTypeUID: http:number
    label: active power
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.active_power_w
      unit: W
  - id: actPowL1
    channelTypeUID: http:number
    label: active power L1
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.active_power_l1_w
      unit: W
  - id: actV
    channelTypeUID: http:number
    label: active voltage
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.active_voltage_v
      unit: V
  - id: actC
    channelTypeUID: http:number
    label: active current
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.active_current_a
      unit: A
  - id: actF
    channelTypeUID: http:number
    label: active frequency
    configuration:
      mode: READWRITE
      stateTransformation:
        - JSONPATH:$.active_frequency_hz
      unit: Hz
2 Likes

You ‘hit the nail on its head’ twice @Udo_Hartmann. The JSONPATH-transformation service wasn’t installed. After installing it, the right value for ‘active_power_w’ was returned immediately.

I also tried to get the values by the HTTP-method, this also worked immediately and is indeed more easy that getting the value by a rule.

Thank you very much for your help, it’s appreciated !

1 Like