Testing the MQTT and HTTP Alarmserver v2 API with Node-RED

Q: I read this article showing how to test my IN-9408 2k+ WQHD cameras alarmserver. How can I can use the alarmserver with Node-RED to automate alarm actions?

A: You can use both - the HTTP and MQTT Alarmserver - in Node-RED to automate reactions depending on the alarm trigger.

HTTP Alarmserver

We already showed how to use the HTTP alarmserver queries in Node-RED. The following is a simplified version of it (the flow can be copied below and imported into Node-RED):

MQTT- und HTTP-Alarmserver v2 API

The flow provides a webhook on the Node-RED server IP + /as-webhook/ that needs to be configured as your cameras alarmserver:

MQTT- und HTTP-Alarmserver v2 API

Your camera will then contact the webhook every time an alarm is triggered and provide the alarm trigger as well as the detected object - IF object detection is enabled!:

MQTT- und HTTP-Alarmserver v2 API

MQTT Alarmserver

The MQTT alarmserver works exactly the same way. Just connect your cameras MQTT Broker with Node-RED and use the MQTT alarmserver topic status/alarm/triggered/triggers to automate different alarm actions depending on the alarm triggers. You can also use the old v1 API on MQTT topic status/alarm/triggered as described here. But the v2 API offers a much finer grained solution:

MQTT- und HTTP-Alarmserver v2 API

Exported Node-RED Flows

HTTP Server

[{"id":"38b7bc1d.686704","type":"http response","z":"87c3a27a0d2959da","name":"","x":430,"y":100,"wires":[]},{"id":"eb3f53ec.7ccc8","type":"template","z":"87c3a27a0d2959da","name":"page","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n    <head></head>\n    <body>\n        <h2>Camera: {{req.query.camera}}</h2>\n        <h2>Test Value 2: {{req.query.query2}}</h2>\n        <h2>Test Value 3: {{req.query.query3}}</h2>\n        <h2>Active: {{req.query.active}}</h2>\n    </body>\n</html>","x":310,"y":100,"wires":[["38b7bc1d.686704"]]},{"id":"679c2568.c1aa9c","type":"http in","z":"87c3a27a0d2959da","name":"Webhook with URL Query","url":"/as-webhook/","method":"get","upload":false,"swaggerDoc":"","x":115,"y":140,"wires":[["eb3f53ec.7ccc8","d47269ed.fe6118"]]},{"id":"d47269ed.fe6118","type":"function","z":"87c3a27a0d2959da","name":"extract query","func":"msg.payload = msg.req.query;\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":180,"wires":[["63db67c3b033fa6d","4b3568fbb80322d3","18496407a4ab3550"]]},{"id":"6524a732f295b841","type":"debug","z":"87c3a27a0d2959da","name":"Old Alarmserver","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":140,"wires":[]},{"id":"63db67c3b033fa6d","type":"change","z":"87c3a27a0d2959da","name":"active","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.trigger","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":140,"wires":[["6524a732f295b841"]]},{"id":"4b3568fbb80322d3","type":"change","z":"87c3a27a0d2959da","name":"trigger","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.trigger","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":180,"wires":[["80bb612971c20704"]]},{"id":"18496407a4ab3550","type":"change","z":"87c3a27a0d2959da","name":"object","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.object","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":220,"wires":[["cf9ae3c715a02492"]]},{"id":"80bb612971c20704","type":"debug","z":"87c3a27a0d2959da","name":"New Alarmserver","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":670,"y":180,"wires":[]},{"id":"cf9ae3c715a02492","type":"debug","z":"87c3a27a0d2959da","name":"Detected Object","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":220,"wires":[]}]

MQTT Server

Please note that the following flow uses an MQTT prefix cameras and a camera ID 201. You can define these values in the MQTT settings of your camera. You can simply replace this string cameras/201/ below with what you use for your camera - before you import the flow into Node-RED. After importing, your MQTT broker must then be added in Node-RED for all MQTT nodes!

[{"id":"b3f2131d.c323e","type":"mqtt in","z":"87c3a27a0d2959da","name":"Alarm Server Triggers","topic":"cameras/201/status/alarm/triggered/triggers","qos":"2","datatype":"json","broker":"996228fe.ababc8","nl":false,"rap":false,"inputs":0,"x":120,"y":760,"wires":[["7e41db00.f5ed94"]]},{"id":"7e41db00.f5ed94","type":"json","z":"87c3a27a0d2959da","name":"","property":"payload","action":"obj","pretty":false,"x":290,"y":760,"wires":[["da03fe2f.869de"]]},{"id":"da03fe2f.869de","type":"switch","z":"87c3a27a0d2959da","name":"Triggers","property":"payload.val","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"4","vt":"str"},{"t":"eq","v":"8","vt":"str"},{"t":"eq","v":"256","vt":"str"},{"t":"eq","v":"512","vt":"str"},{"t":"eq","v":"1024","vt":"str"},{"t":"eq","v":"2048","vt":"str"},{"t":"eq","v":"16","vt":"str"},{"t":"eq","v":"32","vt":"str"},{"t":"eq","v":"64","vt":"str"},{"t":"eq","v":"128","vt":"str"}],"checkall":"true","repair":false,"outputs":13,"x":440,"y":760,"wires":[["195466da1d581e4e"],["682bc199.6b418"],["71a23888cd6d73a8"],["8e7cbf3a99e6c5c9"],["6b1d4f5d11b3449e"],["bf1555bc65181f5d"],["3484d2478ebb6e8e"],["6bad46cad2224847"],["f6651e39b5f4f78b"],["2e78976a797b2ceb"],["2204ca950fbd8d01"],["a3ca640b95b1a902"],["d289a0e3c16e4e74"]]},{"id":"682bc199.6b418","type":"change","z":"87c3a27a0d2959da","name":"Area 1 => Move 10 steps Left","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"10\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":560,"wires":[["5bbe46a4.047128"]]},{"id":"5bbe46a4.047128","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/movesteps/left","topic":"cameras/201/features/ptz/movesteps/left","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1020,"y":560,"wires":[]},{"id":"195466da1d581e4e","type":"change","z":"87c3a27a0d2959da","name":"Idle => Stop Recording / Position 1","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":480,"wires":[["f2724668d2d9146b","07ef65bfc94eb6d0"]]},{"id":"f2724668d2d9146b","type":"mqtt out","z":"87c3a27a0d2959da","name":"task/video/rec/stop","topic":"cameras/201/task/video/rec/stop","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":990,"y":500,"wires":[]},{"id":"71a23888cd6d73a8","type":"change","z":"87c3a27a0d2959da","name":"Area 2 => Move 10 steps Right","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"10\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":600,"wires":[["f3900be8abcc6951"]]},{"id":"bfa500807a82ea06","type":"mqtt out","z":"87c3a27a0d2959da","name":"task/video/rec/start","topic":"cameras/201/task/video/rec/start","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":990,"y":900,"wires":[]},{"id":"f3900be8abcc6951","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/movesteps/right","topic":"cameras/201/features/ptz/movesteps/right","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1020,"y":600,"wires":[]},{"id":"8e7cbf3a99e6c5c9","type":"change","z":"87c3a27a0d2959da","name":"Area 3 => Move 10 steps Up","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"10\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":640,"wires":[["fbead2fa3f167054"]]},{"id":"fbead2fa3f167054","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/movesteps/up","topic":"cameras/201/features/ptz/movesteps/up","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1020,"y":640,"wires":[]},{"id":"6b1d4f5d11b3449e","type":"change","z":"87c3a27a0d2959da","name":"Area 4 => Move 10 steps Down","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"10\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":680,"wires":[["4ee2c74fa6bfba51"]]},{"id":"4ee2c74fa6bfba51","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/movesteps/down","topic":"cameras/201/features/ptz/movesteps/down","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1020,"y":680,"wires":[]},{"id":"bf1555bc65181f5d","type":"change","z":"87c3a27a0d2959da","name":"Area 5 => Move 1 Second Left","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":720,"wires":[["63c0253cdd01bb36"]]},{"id":"3484d2478ebb6e8e","type":"change","z":"87c3a27a0d2959da","name":"Area 6 => Move 1 Second Right","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":760,"wires":[["18a1d7ad9f8628b2"]]},{"id":"6bad46cad2224847","type":"change","z":"87c3a27a0d2959da","name":"Area 7 => Move 1 Second Up","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":800,"wires":[["3cd470bbab730fad"]]},{"id":"f6651e39b5f4f78b","type":"change","z":"87c3a27a0d2959da","name":"Area 8 => Move 10 Second Down","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":840,"wires":[["1340bcbcebebaf9d"]]},{"id":"63c0253cdd01bb36","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/left","topic":"cameras/201/features/ptz/move/left","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1000,"y":720,"wires":[]},{"id":"18a1d7ad9f8628b2","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/right","topic":"cameras/201/features/ptz/move/right","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1000,"y":760,"wires":[]},{"id":"3cd470bbab730fad","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/up","topic":"cameras/201/features/ptz/move/up","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1000,"y":800,"wires":[]},{"id":"1340bcbcebebaf9d","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/down","topic":"cameras/201/features/ptz/move/down","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1010,"y":840,"wires":[]},{"id":"2e78976a797b2ceb","type":"change","z":"87c3a27a0d2959da","name":"PIR Sensor => Start Recording","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"1\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":730,"y":900,"wires":[["bfa500807a82ea06"]]},{"id":"7d4063c20e1e927c","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/rel/pan","topic":"cameras/201/features/ptz/move/rel/pan","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1010,"y":940,"wires":[]},{"id":"2204ca950fbd8d01","type":"change","z":"87c3a27a0d2959da","name":"Alarm Input => Pan Center","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"50\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":940,"wires":[["7d4063c20e1e927c"]]},{"id":"75ec57d578d3d86c","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/rel/tilt","topic":"cameras/201/features/ptz/move/rel/tilt","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1010,"y":980,"wires":[]},{"id":"a3ca640b95b1a902","type":"change","z":"87c3a27a0d2959da","name":"Alarm Input => Tilt Center","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"50\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":980,"wires":[["75ec57d578d3d86c"]]},{"id":"1f45a8118adf9dee","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/move/rel/zoom","topic":"cameras/201/features/ptz/move/rel/zoom","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1020,"y":1020,"wires":[]},{"id":"d289a0e3c16e4e74","type":"change","z":"87c3a27a0d2959da","name":"Manual => Zoom 100%","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"val\":\"100\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":1020,"wires":[["1f45a8118adf9dee"]]},{"id":"07ef65bfc94eb6d0","type":"mqtt out","z":"87c3a27a0d2959da","name":"features/ptz/preset/goto","topic":"cameras/201/features/ptz/preset/goto","qos":"2","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"996228fe.ababc8","x":1010,"y":460,"wires":[]},{"id":"996228fe.ababc8","type":"mqtt-broker","name":"INSTAR MQTT","broker":"192.168.2.201","port":"1883","clientid":"iored","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"5","keepalive":"60","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]