Node-RED with the INSTAR Cloud Webhook
The INSTAR Cloud supports both the GET and POST method for the smarthome webhook. This article will help you to set up the GET method. For more information on the POST method please continue reading here.
Node-RED GET Webhook
First I have to configure a webhook that serves some dummy HTML. Incomming GET requests will have to be forwarded depending on the URL parameter they carry. For example, I am going to add a parameter called trigger
that will either be cloud
or camera
(if the incomming request came from a camera directly istead of the INSTAR cloud). Additionally there will be a parameter appended by the INSTAR Cloud for the camera ID and the detected object within the alarm video - see Webhook documentation for more details:
Parameter | Value |
---|---|
trigger | cloud or camera |
cam | Camera ID eg. 872345 |
object | Detected object e.g. 1 for a person |
The Node-RED flow looks like this:
Flow Export
[{"id":"38b7bc1d.686704","type":"http response","z":"ed9ce5b007555c56","name":"","x":530,"y":40,"wires":[]},{"id":"eb3f53ec.7ccc8","type":"template","z":"ed9ce5b007555c56","name":"GET Webhook","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n <head>\n <title>GET Webhook</title>\n </head>\n <body>\n <h2>Trigger: {{req.query.trigger}}</h2>\n <h2>Camera: {{req.query.cam}}</h2>\n <h2>Object: {{req.query.object}}</h2>\n </body>\n</html>","x":380,"y":40,"wires":[["38b7bc1d.686704"]]},{"id":"679c2568.c1aa9c","type":"http in","z":"ed9ce5b007555c56","name":"GET Webhook with URL Query","url":"/get-webhook/","method":"get","upload":false,"swaggerDoc":"","x":150,"y":60,"wires":[["eb3f53ec.7ccc8","d47269ed.fe6118"]]},{"id":"bedc0285.722dd","type":"switch","z":"ed9ce5b007555c56","name":"Trigger","property":"payload.trigger","propertyType":"msg","rules":[{"t":"eq","v":"camera","vt":"str"},{"t":"eq","v":"cloud","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":300,"y":120,"wires":[["642a98e9cbc7ac4a"],["376ebc65.1998a4"]]},{"id":"d47269ed.fe6118","type":"function","z":"ed9ce5b007555c56","name":"extract query","func":"msg.payload = msg.req.query;\nreturn msg;","outputs":1,"noerr":0,"x":146,"y":120,"wires":[["bedc0285.722dd"]]},{"id":"376ebc65.1998a4","type":"switch","z":"ed9ce5b007555c56","name":"Cloud","property":"payload.cam","propertyType":"msg","rules":[{"t":"eq","v":"872345","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":170,"y":200,"wires":[["0c56e9b0ed94f542"],["a179fb56e903475f"]]},{"id":"642a98e9cbc7ac4a","type":"debug","z":"ed9ce5b007555c56","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":450,"y":100,"wires":[]},{"id":"cda26d9f76231263","type":"debug","z":"ed9ce5b007555c56","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":180,"wires":[]},{"id":"885b843387e73f9c","type":"switch","z":"ed9ce5b007555c56","name":"Object","property":"payload.object","propertyType":"msg","rules":[{"t":"eq","v":"Unknown","vt":"str"},{"t":"eq","v":"Person","vt":"str"},{"t":"eq","v":"Vehicle","vt":"str"},{"t":"eq","v":"Animal","vt":"str"},{"t":"eq","v":"Person & Vehicle","vt":"str"},{"t":"eq","v":"Vehicle & Animal","vt":"str"},{"t":"eq","v":"Person, Vehicle & Animal","vt":"str"}],"checkall":"true","repair":false,"outputs":7,"x":490,"y":180,"wires":[["cda26d9f76231263"],["cda26d9f76231263"],["cda26d9f76231263"],["cda26d9f76231263"],["cda26d9f76231263"],["cda26d9f76231263"],["cda26d9f76231263"]]},{"id":"0c56e9b0ed94f542","type":"change","z":"ed9ce5b007555c56","name":"Cam 872345","rules":[{"t":"change","p":"payload.object","pt":"msg","from":"0","fromt":"str","to":"Unknown","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"1","fromt":"str","to":"Person","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"2","fromt":"str","to":"Vehicle","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"3","fromt":"str","to":"Person & Vehicle","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"4","fromt":"str","to":"Animal","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"5","fromt":"str","to":"Person & Animal","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"6","fromt":"str","to":"Vehicle & Animal","tot":"str"},{"t":"change","p":"payload.object","pt":"msg","from":"7","fromt":"str","to":"Person, Vehicle & Animal","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":180,"wires":[["885b843387e73f9c"]]},{"id":"a179fb56e903475f","type":"debug","z":"ed9ce5b007555c56","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":310,"y":220,"wires":[]}]
Configuring the INSTAR Cloud
The Webhook configuration can be found under Alarm Settings. Here you have to add the URL that leads to your webhook. We are still connecting to Node-RED directly via the default port 1880
on the address of our server my.server.address
(can be a domain name or an IP address) and the example Node-RED flow above defined a GET Webhook /get-webhook/
. Adding all of this togther will give us the webhook URL:
http://my.server.address:1880/get-webhook/
I am going to add one additional parameter trigger=cloud
, let the INSTAR Cloud automatically append the triggering camera ID and the recognized object that caused the alert:
The username and passwort that needs to be added here is the one that you defined under Node Security httpNodeAuth
. In the example I choose admin
and instar
.
Below you can select what cameras should use the webhook and what detected objects should be reported. Hit Save and follow up with Test to test your webhook:
The test returns a HTTP/1.1 200 OK
and when you check the debug panel in Node-RED you should see that the trigger
parameter was delivered:
Wait for a "real" alarm event and you should see that both the camera ID as well as the detected object will be send to Node-RED:
Now all you have to do is to define different actions depending on the triggering camera and detected object!
Next Steps
If you want to use your Node-RED webhook directly with your INSTAR camera - without the INSTAR Cloud account - please follow our Alarmserver Queries Tutorial.