Synology DSM: custom sensors for automatic start and shutdown

Hi everybody,

I have tried to find information on this on a synology forum, but without the answers I was hoping for. While some if this can be achieved by using this modified synology integration, this does not work for me (and, based on the github issues, neither for other people) reliably.

So I am trying to create what I need with custom sensors. Here’s an overview what I need and what I got so far:

  • Boot DS00 at certain time (WOL, input_datetime)
  • Power off DS00 at certain time (native synology integration button press, input_datetime)
  • Boot DS1 at certain time
  • Power off DS01 at certain time
  • kind off determine how many backup tasks are currently running on DS01
    • determine whether or not DS01 can be shut down when there are no backup tasks
  • determine whether DS01 can be shut down at set certain time (depending on media still being consumed or not)
  • determine hyper backup values (for each existing backup task)
    • name
    • next scheduled start
    • last state (successful / failure)
  • determine when to WOL DS01 based on next scheduled backup start

Backups are going from DS00 to DS01; so I’ll use the sensor to shut down DS00 as well as DS01. If DS01 is done backing up, there cannot be another backup running on DS00.

Boot and power off are no-brainers, so I’ll skip these here. But then here’s the other stuff:

determine backup tasks on DS01

  - sensor:
      command: ssh -o UserKnownHostsFile=/config/packages/known_hosts -p 1222 -i /config/packages/hassos [email protected] "ps aux | grep backup | grep '\-B.*k' | wc -l"
      name: DS413 Backup Count
      scan_interval: 300
      unique_id: 20240603081900

This will be zero when no backups are running, otherwise it will be a number value. So then

  - binary_sensor:
      - name: "DS413 Backup done?"
        unique_id: 20240603083000 
        state: >
          {% set ds = states('sensor.ds413_backup_count') | int %}
          {% if ds == 0 %}
          {% elif ds >= 1 %}
          {% else %}
          {% endif %}

can DS01 be shut down?

  - binary_sensor:
      - name: "Synology Media Mode"
        unique_id: 20240603083025
        state: >
          {{ is_state('input_boolean.unten_wohnzimmer_mode_kino', 'off') and is_state('input_boolean.unten_wohnzimmer_mode_tv', 'off') }}

I created modes for TV and cinema in the living room; if those are not off, media is still being consumed. If this is the case, wait until TV mode and cinema mode are both off:

(the and condition at the beginning doesn’t make sense, I was going to add another condition later though)

alias: DS918 Aus
  - condition: and
      - condition: trigger
          - ds918aus
  - if:
      - condition: state
        entity_id: binary_sensor.synology_media_mode
        state: "off"
      - service:
          entity_id: button.wakaluba_shutdown
        data: {}
      - wait_template: "{{ states(\"binary_sensor.synology_media_mode\") == \"on\" }}"
        continue_on_timeout: false
      - service:
          entity_id: button.wakaluba_shutdown
        data: {}

At the input_datetime time this is supposed to happen, backups are definitely finished, so I am not checking for them. But perhaps I should add this so that, if, for some reason, there was a backup, the automation would wait for it to finish.

DSM won’t shut down when backups are running anyway, but it won’t automatically shut down when they are done, so yeah, I’ll have to add that.

determine next backup start

Currently, DS00 starts at 6:30 in the morning and DS01 starts at 11:45; first backup is scheduled at 12:00. But these times are determined by manually set input_datetime entities. It’d be nice to check when the next backup is due, subtract 15m (or so), and automatically update the input_datetime to WOL DS01 at this time.

So if I were to change the backup schedule on DS00 (for example, add another backup task at 11:22), I’d like DS01 to WOL at 11:07, without me having to manually set this time via input_datetime.

The above mentioned modified integration as capable of gathering details to all these backups (time, current status (active / inactive) etc.), but it fails constantly. Restarting that modified integration doesn’t fix the issue, so one has to restart Home Assistant to get the integration working again.

Not quick restart (which I could live with, even though it’d be an awful solution), but a full restart is required to make the integration work again. So I switched back to the official synology integration – which does not provide any information about backup tasks.

I don’t like the solution I’ve found for determine whether or not backups are running. It does work, but it feels so inelegant. While I have looked into utilizing the synology API to request this instead, I couldn’t get it working. Trying to follow the DSM API guide, I wasn’t even able to log into the API, let alone receive any information this way.

Are there any of you also integration (multiple) Synology Disk Station(s) and have similar setups? If so, would you be willing to share your setup? Thanks for your input :slight_smile:

Just started trying to figure out a backup process where I have a 3.5" external HDD hooked up to my NAS and a smart socket. The idea is that USB Copy is set to take backup when drive is connected and then eject itself when backup is complete so I can turn off the smart socket again.

However, the ‘eject after backup’ doesn’t spin down my external HDDs and I don’t want to power them off when they’re spinning. I’ve figured out a couple commands that work but I haven’t figured out a good way to run those. They require root so I don’t really want to run them via ssh through HA. Maybe it’s possible to ssh in with a different user and run a scheduled task instead. But at least I’ve figured out a nice and easy way of triggering on backup complete:

I set up a webhook like shown here: Synology - send System Notifications to Homeassistant & Companion App

I modified the subject of USB Copy Event ‘Completed a task’ to include %TASKNAME% (since webhooks only contains subject of an event). Then I can set up an automation to power down external HDDs if {{trigger.json.text}} contains ‘USB Copy task Bla bla bla is complete’.