Synology Surveillance Station NVR: Home Mode with node-red and MQTT

Hi all, I’m a long time lurker, first time sharer.

A little while ago I introduced some Xiaofang wifi IP cameras into my ecosystem. I am currently using the very average but sufficiently functional Synology Surveillance Station as an NVR for the CCTV cameras on the outside and the XiaoFang cameras on the inside of my house.

As neither my wife nor I are overly please with the idea of candid CCTV footage of us walking around the house in varying states of dress I was keen to implement a reliable home/away setup with the NVR. Synology provide this functionality using geofencing and the Synology app, however our experience with this was patchy at best.

We already have a fully functioning and almost perfectly reliable occupancy state using home-assistant so I wanted to lean on this. As best as I could gather the Synology Surveillance Station component does not yet provide that functionality, however there is an API available, so a layman like myself might be able to bash a couple of the correctly shaped and coloured rocks together, and produce a workable solution.

So onto the kludge!

I’ve created a rather inelegant flow in node-red that authenticates with the API and then calls the required state. I am sure there are much more optimised and cleaner ways to implement this, and I an eager to hear them so please share!

[{"id":"93d9dae4.3ec168","type":"http request","z":"5b50b4d.077cc4c","name":"API Auth","method":"GET","ret":"txt","url":"http://[[synoIPaddr]]:5000/webapi/auth.cgi?api=SYNO.API.Auth&method=Login&version=3&account=[[username]]&passwd=[[password]]&session=SurveillanceStation&format=sid","tls":"","x":340,"y":620,"wires":[["5ba241ca.ebafa"]]},{"id":"5ba241ca.ebafa","type":"json","z":"5b50b4d.077cc4c","name":"","property":"payload","action":"","pretty":true,"x":490,"y":620,"wires":[["b562565b.8f72c8"]]},{"id":"ac6b92c8.747","type":"http request","z":"5b50b4d.077cc4c","name":"API Call","method":"GET","ret":"txt","url":"{{{payload}}}","tls":"","x":480,"y":680,"wires":[["ffd7327c.92075"]]},{"id":"95ef6cc4.b798a","type":"template","z":"5b50b4d.077cc4c","name":"Home Mode Off","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"http://[[synoIPaddr]]:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=false&_sid={{{payload.data.sid}}}","output":"str","x":320,"y":700,"wires":[["ac6b92c8.747"]]},{"id":"d2f1c6a.4acde38","type":"template","z":"5b50b4d.077cc4c","name":"Home Mode On","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"http://[[synoIPaddr]]:5000/webapi/entry.cgi?api=SYNO.SurveillanceStation.HomeMode&version=1&method=Switch&on=true&_sid={{{payload.data.sid}}}","output":"str","x":320,"y":660,"wires":[["ac6b92c8.747"]]},{"id":"b562565b.8f72c8","type":"switch","z":"5b50b4d.077cc4c","name":"","property":"state","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":170,"y":680,"wires":[["d2f1c6a.4acde38"],["95ef6cc4.b798a"]]},{"id":"f2908695.c890c8","type":"mqtt in","z":"5b50b4d.077cc4c","name":"","topic":"device/synodevice/homemode/cmd","qos":"2","broker":"cd2e83a8.f2969","x":260,"y":560,"wires":[["c9aa7fd2.183b6"]]},{"id":"d3ca1b28.9e0628","type":"debug","z":"5b50b4d.077cc4c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":850,"y":640,"wires":[]},{"id":"c9aa7fd2.183b6","type":"change","z":"5b50b4d.077cc4c","name":"","rules":[{"t":"set","p":"state","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":190,"y":620,"wires":[["93d9dae4.3ec168"]]},{"id":"835c225.a321ee","type":"mqtt out","z":"5b50b4d.077cc4c","name":"CommandStatus","topic":"device/synodevice/homemode/stat","qos":"","retain":"true","broker":"cd2e83a8.f2969","x":870,"y":680,"wires":[]},{"id":"ffd7327c.92075","type":"change","z":"5b50b4d.077cc4c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"state","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":680,"wires":[["835c225.a321ee","d3ca1b28.9e0628"]]},{"id":"cd2e83a8.f2969","type":"mqtt-broker","z":"","name":"mqttbrokername","broker":"[[mqttbrokerip]]","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

I just use a standard MQTT switch on the HA side of things.

  - platform: mqtt
    name: Synology Home Mode
    command_topic: "device/synodevice/homemode/cmd"
    state_topic: "device/synodevice/homemode/stat"
    payload_on: "on"
    payload_off: "off"
    retain: true

And a basic automation to turn Synology Home-Mode ‘off’ (thus enabling recording on the internal cameras)

 - alias: House Away Mode
   trigger: 
     - platform: state
       entity_id: sensor.anyone_home
       to: 'Away'
       for: 00:02:00
   action:
     - service: switch.turn_off
       data:
         entity_id:
           - switch.synology_home_mode
 - alias: House Home Mode
   trigger: 
     - platform: state
       entity_id: sensor.anyone_home
       to: 'Home'
   action:
     - service: switch.turn_on
       entity_id:
         - switch.synology_home_mode

I hope this is useful to someone, and I am keen to receive recommendations or criticism :wink:

Cheers.
Grant

4 Likes

@nemmeh just wanted to say a massive thanks to supplying this. After a little bit of tweaking, I managed to get it working without too much effort.

The first thing to add to this for other people attempting this is:
You must create a user that has access to the Synology Surveillance Centre app and does not have 2FA enabled

Once I did this, I was still getting an error that the auth was failing. After some digging around, I tried using this api call instead of the one you supplied in the API Auth Node:

http://[[synology private url]]:5000/webapi/auth.cgi?api=SYNO.API.Auth&method=login&version=3&account=[[user name]]&passwd=[[password]]&session=SurveillanceStation

this was slightly different to yours, but I’m not sure why yours wasn’t working for me and why this one does. Either way, I wanted to put my 2cents here incase others get stuck.

Lastly: Since your post (now 5years ago) HA has changed how mqtt switches are configured in the yaml. Instead of using the platform syntax, it now looks like this in the configuration.yaml:

mqtt:
  - switch:
      name: Synology Home Mode
      command_topic: "device/synodevice/homemode/cmd"
      state_topic: "device/synodevice/homemode/stat"
      payload_on: "on"
      payload_off: "off"
      retain: true

Cheers again mate! really happy that I can control the home/away mode using HA now :smiley:

1 Like

Since the original post the synology integration also has a switch to set the home mode…