Automation of addons

I would like to request the ability for automations to retrieve the status of Addons and to be able to stop and start them or even ask the addon to take a particular action.
The main driver for my request is the issue of corrupted Mariadb databases when restoring a snapshot which was taken whilst Mariadb was running. Currently the only workaround for this problem is to MANUALLY stop Mariadb, take the snapshot and restart Mariadb. Seems counter to the ethos of an automation system to require manual intervention to take a backup.
I would like to be able to do something like:
Issue stop command for Mariadb
wait until Mariadb is stopped
Tell the Samba snapshot addon to take a snapshot.
when snapshot complete, restart Mariadb

I am sure there are other scenarios where knowing the status of an addon and being able to stop and start it would be useful, for example using a notify function to warn of the failure of an addon.

you can use a rest sensor to get the status of an add-on and use that state in an automation. See bleow. I’m assuming this is the add-on you are running https://github.com/thomasmauerer/hassio-addons/blob/386e92ee659fb584cbf615f1677b6c12a8ce42dd/samba-backup/DOCS.md

Also, not sure if its going to cause issues stopping the maria db addon while HA is running. This automation won’t catch any exceptions (ie. failed backup).

You will need to update the IP in the sensor to match yours. Also, double check the states of the samba addon sensor. The docs say ‘RUNNING’ is the state, however, if its ‘running’ in the states tab it won’t trigger.

#Sensor
  - platform: rest
    resource: http://192.168.1.2:8123/api/hassio/addons/core_mariadb/info
    name: mariadb
    value_template: '{{ value_json.data.state }}'
    scan_interval: 60
    headers:
      Authorization: !secret llt
      Content-Type: application/json

#Automation
  - alias: Stop and Snapshot
    trigger:      
      - platform: time
        at: '00:01:00'
    action:
      - service: hassio.addon_stop
        data:
          addon: core_mariadb
      - wait_for_trigger:
          - platform: state
            entity_id: sensor.mariadb
            to: 'stopped'
      - service: hassio.addon_stdin
        data:
          addon: 15d21743_samba_backup
          input: trigger
      - wait_for_trigger:
          - platform: state
            entity_id: sensor.samba_backup
            from: 'RUNNING'
            to: 'SUCCEEDED'
      - service: hassio.addon_start
        data:
          addon: core_mariadb
1 Like

See this thread for additional information on setting up the sensor.

Silvrr,
Thank you for the example sensor and automation code. In the last trigger, should this not refer to a sensor for the status of the Samba backup addon, rather than mariadb?
Cheers

Yes it should!