HTTP Binding Motion detection for Reolink IP camera

Hello,

i have a URL wehre is can get motion detection status from my Reolink cam.
http://192.168.X.X/api.cgi?cmd=GetMdState&user=admin&password=123456

There i get


[
   {
      "cmd" : "GetMdState",
      "code" : 0,
      "value" : {
         "state" : 0
      }
   }
]

How could i poll it for example every second and parse the state oif motion sensor?

Or maybe i need another binbding for it?

Thanks!

Have you read the HTTP binding docs? I expect the useful parts for you would be about updateInterval, and about JSONPATH

Note that this is an old version 1.x binding, so you need to configure by editing files, not with PaperUI

1 Like

Yes i have read and did not understood.

This is the item i have made, but it do noit seems to work

String MotionTest "Motion Test [%s]" { http="<http://192.168.178.36/api.cgi?cmd=GetMdState&user=admin&password=123456:1000:JSONPATH($.value.state)]" } 

What i get is

2019-11-17 15:29:22.805 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type 'http' of item 'MotionTest' could not be parsed correctly.

org.eclipse.smarthome.model.item.BindingConfigParseException: bindingConfig '<http://192.168.X.X/api.cgi?cmd=GetMdState&user=admin&password=123456:1000:JSONPATH($.value)]' doesn't contain a valid binding configuration

I have config in Paper UI, but it seems that i do not need to configure it, defaults looks good http://prntscr.com/py4ofh

And one more thing, i would like to test it first of all without jsonpath, just to get the string of answer. But i also have not found in manual how to get it.

I have solved configuration error.
Now i have problem with jsonpath.

Here is right item definition

String MotionTest "Motion Test [%s]" { http="<[http://192.168.XX.XX/api.cgi?cmd=GetMdState&user=admin&password=123456:5000:JSONPATH($.value.state)]" } 

But this part seems to be wrong

JSONPATH($.value.state)

I get

[WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JSONPATH($.value.state)' threw an exception. [response=[

   {

      "cmd" : "GetMdState",

      "code" : 0,

      "value" : {

         "state" : 0

      }

   }

]

]

org.openhab.core.transform.TransformationException: Invalid path '$.value.state' in '[

   {

      "cmd" : "GetMdState",

      "code" : 0,

      "value" : {

         "state" : 0

      }

   }

]

Is it the binding fault or i have made smtg wrong?

As i understand the prob;em in [ ] outside the response. I need to put them away or use right jsonpath expresson.

Anyone knows it?

Yes, your results is actually an array of JSON objects with only one array member.
You need to do something like
JSONPATH($[0].value.state)
but I’m not at all sure about the syntax

1 Like

Thanks a lot!
I have made so
$…state - also works!

Here is the good website to test jsonpath patterns http://jsonpath.com/

1 Like

That’s a bit odd really; I thought
JSONPATH($.xxx
defaulted to
JSONPATH($[0].xxx
by itself.

If you post your working htttp.cfg and Item definitions, it may help others later who run into similar problems.

1 Like

http.cfg is unchanged. All is commented.

Item is here

String Camera1Motion "Camera 1 Motion [%s]" { http="<[http://192.168.XX.XX/api.cgi?cmd=GetMdState&user=admin&password=123456:1000:JSONPATH($[0].value.state)]" } 

But it is not the last edition. This “0” and “1” must be converted to “ON” and “OFF” and item type must be “switch” i think. Now i am looking for the way how to do it. It must be map transformation, but how to put it insode JSONPATH i can not understand.

Sure that you know)

Do it the other way around - use a javascript transform instead of JSONPATH transform, that does the JSON parsing and uses that to choose the return string ON/OFF

1 Like

So i make an item this way

String MotionTest "Current Channel [%s]" { http="<http://192.168.178.36/api.cgi?cmd=GetMdState&user=admin&password=123456:1000:JS(getValue.js)]" }

but i am not good in javascript, need your help how do i parse it inside the getValue.js ?

There is an example in the HTTP binding docs, right alongside the JSONPATH example.

1 Like

Yes i have found it but it do not work for me

My getValue.js

JSON.parse(input).$[0].value.state;

I made simple function like to check

(function(i){ 
	return i;
})(input)

but it do not work(((

By the way Javascript Trans is installed http://prntscr.com/py5qa1

1 Like

Made it!

Here is all

.item

Switch Camera1Motion "Camera 1 Motion [%s]" { http="<[http://192.168.XX.XX/api.cgi?cmd=GetMdState&user=admin&password=123456:1000:JS(cameraMotion.js)]" }

cameraMotion.js

(function(x){
    var json = JSON.parse(x);
    if (json[0].value.state)
    {
        result="ON";
    }
    else
    {
        result="OFF";
    }
    return  result;
})(input)  

.sitemap

Frame label="Camera Motion" {   
    Default item=Camera1Motion
} 

rossko57, thanks for help!

3 Likes

Hello more of preformance Q…

@rossko57, i have set this up in node-red
image

working great! but i notice that its very fast going from true to false
i am planing on somthing like 12 cameras , and when my DIY alram is on
i am not home and , i will starting querying the the cameras

but still i am worried , will this kind of traffic damge/slow my network ?
what kind of intravel will you set ?
is there any way to make the camera change a litle slower

thanks!

I don’t know anything about node red, I’m afraid.

i was showing node red as an expmle …

i assume that the JS script also will send a GET request each time he wants a reply?
or maybe it is listening to changes?
just trying to understand if there is any diffrance between what i am doing and above…

The JS script doesn’t send anything. It just extracts data from incoming HTTP.

@thisisIO Item is set up to send HTTP request once a second.

so this approch will also send HTTP request each sec…to every camera
i am by no way a network expert, i am just asking

lets say 12 camers will send 12 requests in a sec time period (thats 1036800 a calls a day)
do you think this a problem ,or this is simaple request and the network can handle this ?
i have a beefy network switch , but maybe its worth looking on other approch

for now i just have two reolink camras …