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.
-
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” -
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]"}
-
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 } -
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.
- 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