Http binding howto manage return values

Hi Community,

hope this is a simple question for you…

how can I get the response to a http request (below) into an rule and parse the content of the received JSON object?

Number myStrom2 "Strom [%.2f Watt]" <energy> (gOutput) {http="<[http://192.168.1.105/report:10000:JS(mystrom2.js)]"}

Maybe it would be usefull to have some more background information and how I came to this question.

  1. I connected my openhab to a new power switch controlled over WLAN. This switch offers an REST API to switch it ON and OFF. Quit simple so far.
    Link to the API Document
    https://mystrom.ch/de/mystrom-api
    and calls I use
    http://192.168.254.1/relay?state=1
    http://192.168.254.1/relay?state=0

  2. implementation of the item is straight forward and works fine

     Switch myStrom1 "Strom1 [ %s]" <switch> (gOutput) {http=" >[ON:GET:http://192.168.1.105/relay?state=1] >[OFF:GET:http://192.168.1.105/relay?state=0]"}
    
  3. this switch can be enabled/disabled manually and openhab shall be able to get the current state. Not to difficult as well because I can read the current state of the power switch with following http command
    http://192.168.1.105/report
    the response in JSON format
    { “power”:0, “relay”:false }

  4. I extended my Switch Item and added <[http://192.168.1.105/report:10000:JS(mystrom1.js)] to the line

     Switch myStrom1 "Strom1 [ %s]" <switch> (gOutput) {http="<[http://192.168.1.105/report:10000:JS(mystrom1.js)] >[ON:GET:http://192.168.1.105/relay?state=1] >[OFF:GET:http://192.168.1.105/relay?state=0]"}
    

The Transformation looks a little bit uggly but works

var json_str = eval('('+input+')');
var test=json_str.relay;
if (test == false){
   result = 'OFF';
}else{
   result = 'ON';
}

if some one enables/disables this power switch openhab gets updated, great!
So far everything is fine and up and running. Now here is the point where it’s getting nasty.

  1. the power switch does not only switch ON/OFF power it measures it as well and I would like to accumulate power the device connected to the switch is consuming over time.

I tried to parse the received JSON object into an rule. It looks like that http calls always expect transformation. In case passing through JSON object by in a transformation like this

result = eval('('+input+')');

I do get events in the eventlog

2015-11-28 08:42:10 - myStrom2 state updated to [object Object]

but my rule is never called, so I came to the question above whether it’s possible to receive JSON objects with the http interface and pars them in a rule. Or are there any other possibility to solve this issue in a nice way

rule "myStrom1"
when
   Item myStrom2 received update
then
   logInfo("Test","test mystrom2")
end

Thanks for your help

markus

So my questions are:

Is myStrom2 a String Item, a Number Item or something else?

You should be able to pass through the entire JSON string using a REGEX(.*) transform. But your Item must be a String

If you are wanting some numbers out of the JSON result, why don’t you just parse those numbers into one or more items and then all you have to do in your rule is some simple math to accumulate the value.

For your information I developped a binding for the mystrom devices, the pull request is waiting code review for the openhab 1.8. You can download the binding at Mystrom binding.

If you have any questions…

Sample here, some one tested it and I use it for more than one year now: Sample

@rlkoshak

you put me on the right way!

the myStrom adapter delivers an JSON object.

String myStrom001_update "hidden object " <energy> (gmyStromHide) {http="<[http://192.168.1.105/report:10000:JS(mystrom2.js)]"} 

I modified my transformation file and create a String object out of it

JSON.stringify(JSON.parse(input))

inside the rules i have to convert to JSON again in order to retrieve and parse the JSON object

var String jsonString = (myStrom001_update.state as StringType).toString
var String stateSwitch = transform("JSONPATH","$.relay",jsonString)
var String powerString = transform("JSONPATH","$.power",jsonString)

@coolweb

that’s cool! I understand the binding in that way that it get’s all it’s information from the myStrom server. Therefor myStrom adapter have to be registered over there. So far I don’t have any myStrom account and my adapter is managed within my local network through openhab. But I do see some advantages to have the power consumption calculation performed on an centralized server. I am not sure if that can be done in an efficient way by a rule file.

Both thanks for your help you made may day!

markus