Rule and getting Parameters from a rest call

I’m on OpenHab4 and want to pull daily consumption and total generation values from my Kaco NX.
By calling:
http://192.168.0.177:8484/getdevdata.cgi?device=2&sn=10.0NX312009307&HTTP/1.0\r\1\r\1
a JSON is returned containing:
{“flg”:1,“tim”:“20240206094132”,“tmp”:406,“fac”:4997,“pac”:42,“sac”:42,“qac”:0,“eto”:88017,“etd”:0,“hto”:5330,“pf”:100,“wan”:0,“err”:0,“vac”:[2299,2305,2323],“iac”:[5,5,5],“vpv”:[1677,1872],“ipv”:[15,17],“str”:}

etd is today’s generated kWh, eto is the overall kWh generation.

I’ve set up a rule in order to make every 15 seconds the http call against my Kaco, the rule is executed as expected but I don’t get out any values, any idea why it’s not working ?


import java.util.concurrent.locks.ReentrantLock
import java.util.concurrent.locks.Lock

var ReentrantLock lock = new ReentrantLock()

rule “KacoWR”
when
Time cron “0/15 * * * * ?” // every 15s
then
lock.lock()
try {
// etd= daily production eto=overall production
// HTTP-Call durchführen und Antwort speichern
val response = sendHttpGetRequest(“http://192.168.0.177:8484/getdevdata.cgi?device=2&sn=10.0NX312009307&HTTP/1.0\\r\\1\\r\\1”)

    // Parse der Antwort, um die Variablen zu extrahieren
    val etd = Integer::parseInt(transform("JSONPATH", "$.etd", response))
    val eto = Integer::parseInt(transform("JSONPATH", "$.eto", response))

} finally {
    lock.unlock()
}

end


Remarks:

  • etd and eto items are defined as numbers
  • display layout looks like this:

sitemap watch label=“Watch” {

}
Frame label=“Gesamt/Year/Max” {
Text item=eto label=“PV Gesamt [%.2f kWh]” icon=“energy”
Text item=etd label=“Erzeugung heute [%.2f kWh]” icon=“energy” valuecolor=[<30=“red”,<50=“yellow”,>=50=“green”]

}
}

Please always mark code as code (json object is also code) as otherwise discourse (the software running the community) may change the text to something completely different.
```

Your Code goes here

```

First things first: Did you install the JSONPATH Addon? Sounds stupid question, but…

Another thing: It’s way easier to do this without a rule, just by creating a http thing and the corresponding channels:

Thing http:url:kaco "Kaco NX" [
    baseURL="http://192.168.0.177:8484/getdevdata.cgi?device=2&sn=10.0NX312009307&HTTP/1.0\\r\\1\\r\\1",
    refresh=15
 ] {
    Channels:
    Type number : etd "Tagesproduktion" [ stateTransformation="JSONPATH:$.etd", unit="kWh"]
    Type number : eto "Totalproduktion" [ stateTransformation="JSONPATH:$.eto", unit="kWh"]
 }

As text file definition. Or via Main UI (yaml Code):

UID: http:url:kaco
label: Kaco NX
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: false
  baseURL: http://192.168.0.177:8484/getdevdata.cgi?device=2&sn=10.0NX312009307&HTTP/1.0\r\1\r\1
  delay: 0
  stateMethod: GET
  refresh: 15
  commandMethod: GET
  timeout: 3000
  bufferSize: 2048
channels:
  - id: etd
    channelTypeUID: http:number
    label: Tagesproduktion
    description: null
    configuration:
      mode: READWRITE
      escapedUrl: false
      unit: kWh
      stateTransformation: JSONPATH:$.etd
  - id: eto
    channelTypeUID: http:number
    label: Totalproduktion
    description: null
    configuration:
      mode: READWRITE
      escapedUrl: false
      unit: kWh
      stateTransformation: JSONPATH:$.eto

Maybe the baseURL can be set slightly simpler by omitting the &HTTP/1.0\r\1\r\1

But your main reason why not getting any output is, you missed an important detail, that is to create and use Items :slight_smile:

Number:Energy etd "Tagesproduktion" {channel="http:url:kaco:etd", unit="kWh", stateDescription=""[pattern="%.1f kWh"]}
Number:Energy eto "Totalproduktion" {channel="http:url:kaco:eto", unit="kWh", stateDescription=""[pattern="%.1f kWh"]}

You can also create these Items via Main UI.

If you want to use the rule anyway, you have to use Item methods (or - not recommended - actions) to put the values into the Items (in that case you can omit links to channels):

rule "Kaco WR"
when
    Time cron "0/15 * * * * ?" // every 15s
then
    //etd = daily production eto = overall production
    // HTTP-Call durchführen und Antwort speichern
    val response = sendHttpGetRequest("http://192.168.0.177:8484/getdevdata.cgi?device=2&sn=10.0NX312009307&HTTP/1.0\\r\\1\\r\\1")

    // Parse der Antwort, um die Variablen zu extrahieren
    etd.postUpdate(Integer.parseInt(transform("JSONPATH", "$.etd", response)))
    eto.postUpdate(Integer.parseInt(transform("JSONPATH", "$.eto", response)))
end

Please be aware that you don’t need the reentrant lock at all, as the rule won’t be executed more than once at a time. Maybe Integer.parseInt() is not necessary at all.

2 Likes

thx for your reply, and yes the JSONPATH addon was set. Your way of setting up a thing/channel didn’t come to my mind, still an newbie. Will try that out asap.

1 Like