Identifying if an addon is running and toggle it in the interface

Hi there, (first post!)
I was wondering whether it’s possible to trigger the starting, stopping, and status identification of a hass.io addon (RPy3) via a switch / automation. Use case here is I’ve got a USB webcam powered by the motion addon in hass.io that I’d like to be able to have turn on when I’m out… Initially happy to make this a manual switch something like below for now, any clues what I need to replace ‘–reference to addon–’ with / if it’s possible? Thanks!

switch:
  - platform: template
    switches:
      skylight:
        value_template: "{{ is_state('--reference to addon--', 'on') }}"
        turn_on:
          service: hassio.addon_start
          data:
            addon: 657d34ca_motion
        turn_off:
          service: hassio.addon_stop
          data:
            addon: 657d34ca_motion

Hi dangrin0, do you find a solution?

You could use a command line binary sensor to check if the add-on is running:

binary_sensor:
  - platform: command_line
    name: MQTT addon Running
    command: 'curl http://supervisor/supervisor/info -H "Authorization: Bearer $(printenv SUPERVISOR_TOKEN)" | jq ''{"addons":[.data.addons[] | select(.name == "Mosquitto broker")]}'''
    value_template: "{{ value_json.addons[0].state}}"
    payload_on: "started"
    payload_off: "stopped"

This would monitor the mqtt add-on

To avoid this being checked regularly, you could add a long scan_interval and then use homeassistant.update_entity to update the sensor only when needed