How to get values with json formated http-js file

  • Platform information:

    • Hardware: Hyper-V VMWare 1vcpu, 2GB Mem
    • OS: CentOS7
    • Java Runtime Environment:
      openjdk version "1.8.0_151"
      OpenJDK Runtime Environment (build 1.8.0_151-b12)
      OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)
    • openHAB version: 2.2
    • additional: HTTP Binding, JSONPath Transformation, BasicUI, HABPanel, HomeBuilder, PaperUI,
  • Issue of the topic:

Hi,

I have a webserver with http://webserver/myjsonfile.js with following content (generated by powershell from mobile alerts sensors):

[
{
“Room”: “WZ”,
“SensorID”: “022A7F181984”,
“Time”: “16.01.2018 13:18:50”,
“Data”: “WZTemperatur”,
“Value”: “23,7 C”
},
{
“Room”: “WZ”,
“SensorID”: “064C1706F7B7”,
“Time”: “16.01.2018 13:19:45”,
“Data”: “WZLuftfeuchtigkeit”,
“Value”: “52%”
}
]

I have following set within http.cfg file:
Jason.url=http://192.168.6.20/mobilealerts.js
Jason.updateInterval=10000

I have following item file (test.items):
String Jason “WZ-Temperatur-Jason” { http="<[http://192.168.6.20/mobilealerts.js:1000:JSONPATH($.[1].Value)]" }

Within HabPanel I configured a Selection Widget with the openHAB Item: A Jason

but with this configuration I just get the complete json file as value and not just 23,7 C or 52% (depending on JSONPATH Index)

Hope anyone of you can give me a hint…

I’ve tried with html code and http-binding + Regex … nope …
I’ve tried with json and getvalue.js … nope …
I’ve tried this variant with JSONPATH … nope …

in every case values are NULL or everything :joy:

thanks a lot
BR Martin

P.S.: If you have any other ideas don’t hesitate to post here!
I’ve a custom Object and are able to transform this to json or html code or another custom format.

Please read the prerequisits, you are using a not recommended Java version.

I guess you could use a cron rule to populate the required fields using jsonpath transformation at certain intervals. Some code below to test in a rule

val  jsonString = sendHttpGetRequest("http://192.168.6.20/mobilealerts.js")

val  temp = transform("JSONPATH","$.[0].Value",jsonString)
val  fucht = transform("JSONPATH","$.[1].Value",jsonString)

logInfo("Temp is ",temp)
logInfo("Fucht is ",fucht)

Hi,
thx for your reply, was not able to find a usable documentation (i’m beginner in centos too)
there is no package available on yum and after installing zulu manualy openhab didn’t work.
with install by: yum install java everything seems fine…

may you can give me a hint where i will find a better documentation or post on internet.
thank you
BR Martin

Hi,

Thank you for this rule!
i will give it a try :slight_smile:

BR Martin

Sorry, don‘t know CentOS, my VMs run either Ubuntu LTS version or XUbuntu if I need a UI.

@hmerk: no problem :slight_smile: but i think i’ve done the challenge. Now i’m (i hope so) on zulu.

@all: i’ve found an interesting line within logfiles after changing to debug mode:

org.openhab.core.transform.TransformationException: Invalid path ‘$.[1].Value’ in '[
but i’ve no idea why because json validators say everything is fine within json file ?!

EDIT: @MartinKo: I’ve searched through Logfile but did not found any entry like “Temp is” or “Fucht is”.
What i’ve done is creating a file within $openhabdir/conf/rules/test.rules within:

val jsonString = "[{"Room ": "WZ ",“SensorID”: “022 A7F181984”,“Time”: “16.01 .2018 13: 18: 50”,“Data”: “WZTemperatur”,"Val$

val temp = transform(“JSONPATH”,"$.[0].Value",jsonString)
val fucht = transform(“JSONPATH”,"$.[1].Value",jsonString)

logInfo("Temp is ",temp)
logInfo("Fucht is ",fucht)

OK… the code I submitted is just the code that is supposed to go into a rule. In other words it is missing the rule skeleton.

You have to provide the rest… like this

rule "update clima"

when
        // run every 10 minutes
        Time cron "0 0/10 * 1/1 * ? *"
then
      val  jsonString = sendHttpGetRequest("http://192.168.6.20/mobilealerts.js")

      val  temp = transform("JSONPATH","$.[0].Value",jsonString)
      val  fucht = transform("JSONPATH","$.[1].Value",jsonString)
      logInfo("Temp is ",temp) 
      logInfo("Fucht is ",fucht)
  
end

oh, ok sry!
I am unworthy :rofl:
first time i’ve seen a rule in openhab.
I hope you have patience with me

No worries mate… I´ll check in on you a bit later this evening to see if your progressing

Good morning,

sry wasn’t able to find something yesterday.
But today i got following in my logs:

2018-01-17 09:10:00.011 [INFO ] [ipse.smarthome.model.script.Temp is ] - [
{
“Room”: “WZ”,
“SensorID”: “022A7F181984”,
“Time”: “16.01.2018 13:18:50”,
“Data”: “WZTemperatur”,
“Value”: “23,7 C”
},
{
“Room”: “WZ”,
“SensorID”: “064C1706F7B7”,
“Time”: “16.01.2018 13:19:45”,
“Data”: “WZLuftfeuchtigkeit”,
“Value”: “52%”
}
]

Edit: similar line with Fucht is.

BR Martin

Tested following on Webserver: cat data.js | jq ‘.[0].value’

gives me:
23,7 C

?!

That only confirms that the json path is OK, but we already knew that. Could you try to modify the rule to the following

rule "update clima"

when
        // run every 2 minutes
        Time cron "0 0/2 * 1/1 * ? *"
then
     
val  jsonString = "[{\"Room \": \"WZ \",\"SensorID\": \"022 A7F181984\",\"Time\": \"16.01 .2018 13: 18: 50\",\"Data\": \"WZTemperatur\",\"Value\": \"23,7 C\"}, {\"Room \": \"WZ \",\"SensorID\": \"064 C1706F7B7\",\"Time\": \"16.01 .2018 13: 19: 45\",\"Data\": \"WZLuftfeuchtigkeit\",\"Value\": \"52 % \"}]"

      val  temp = transform("JSONPATH","$.[0].Value",jsonString)
      val  fucht = transform("JSONPATH","$.[1].Value",jsonString)
      logInfo("Temp is ",temp) 
      logInfo("Fucht is ",fucht)
  
end

This is to rule out any problems reading the json from the server and to the OpenHab transformation service. When I run this rule I get the result that is expected

You can do the Job on Server … with JQ … JQ is the best Tool :slight_smile:

Bash Script

#!/bin/sh
current_data="cat data.js | jq '.[0].value'"
curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "$current_data" "http://<OPENHABIP>:8080/rest/items/<OPENHAB_ITEM>"

Add a cronjob on Server for this Bash Script …

1 Like

Hi! That did the job!!! Great! :scream:

For all they want to use powershell:
curl -X PUT --header “Content-Type: text/plain” --header “Accept: application/json” -d “$current_data” “http://:8080/rest/items/<OPENHAB_ITEM>”

=>
$DATA = 23
invoke-restmethod -method POST -body $DATA -contenttype “text/plain” -uri "http://:8080/rest/items/<OPENHAB_ITEM>

Post or Put depend on usecase (but i’m not a Specialist on that => just try…

to receive all items:

invoke-restmethod -method get -uri “http://:8080/rest/items?recursive=false”

SPECIAL THANKS TO @ei_Gelb_Geek @MartinKo & @hmerk!

BR Martin

1 Like