Automations
With the integration connected, Home Assistant can look after your containers by itself: tell you when a new version is out, update the containers you choose at night, warn you when one stops. The examples on this page are ready to paste.
What an automation can do
The integration has no actions of its own: its entities work with the actions Home Assistant already has.
| To | Action | Entity |
|---|---|---|
| Update one container | update.install | The Update entity of the container, such as update.jellyfin_update |
| Update every container | button.press | Update all, button.d2ha_update_all |
| Look for new versions now | button.press | Check for updates, button.d2ha_check_for_updates |
| Delete the unused images | button.press | Clean images, button.d2ha_clean_images |
An automation can start from the State sensor of a container, such as sensor.jellyfin_state, or from the Updates available counter, sensor.d2ha_updates_available. What you get lists every entity with its attributes.
Add an example
- In Home Assistant, open Settings → Automations & scenes and press Create automation, then Create new automation.
- Open the menu ⋮ and choose Edit in YAML.
- Paste the example in place of what is there, put in your entity ids and Save.
The examples use the ids of a Home Assistant in English and of two containers called jellyfin and vaultwarden. Yours depend on the language and on the names of your containers: find them in Settings → Devices & services → D2HA. Replace notify.notify too, with your own notification action: for the phone app it is notify.mobile_app_ followed by the name of the phone.
A message when new updates appear
alias: "D2HA: new container updates"
triggers:
- trigger: state
entity_id: sensor.d2ha_updates_available
not_from:
- unknown
- unavailable
conditions:
- condition: template
value_template: >-
{{ trigger.from_state is not none and
trigger.to_state.state | int(0) > trigger.from_state.state | int(0) }}
actions:
- action: notify.notify
data:
title: Container updates
message: >-
{{ state_attr('sensor.d2ha_updates_available', 'containers') | join(', ') }}
The message leaves every time the number of updates grows, and names the containers that are waiting. It does not leave when the number goes down after an update, or when Home Assistant or D2HA restarts.
The number counts every container of D2HA, while the names are only those of the containers you show to Home Assistant.
Update the containers you choose at night
alias: "D2HA: update chosen containers at night"
triggers:
- trigger: time
at: "04:00:00"
actions:
- action: update.install
target:
entity_id: >-
{{ ['update.jellyfin_update', 'update.vaultwarden_update']
| select('is_state', 'on') | list }}
Every night at 4:00, the containers of the list that have an update are updated, one after the other. With nothing to update, nothing happens.
select('is_state', 'on') keeps the containers that have an update. Without it, a container of the list that is already up to date ends the run with the error No update available, even though the other containers are updated.
Put in the list only containers that have Install in Home Assistant. D2HA cannot update a container whose image was built on your machine, and updates nothing in read-only mode.
Leave out the breaking changes
D2HA marks an update when its release notes have a Breaking changes section. To keep those for yourself, use this action in place of the one above:
actions:
- action: update.install
target:
entity_id: >-
{{ ['update.jellyfin_update', 'update.vaultwarden_update']
| select('is_state', 'on')
| reject('is_state_attr', 'breaking_changes', true) | list }}
Update everything at night
alias: "D2HA: update everything at night"
triggers:
- trigger: time
at: "04:00:00"
actions:
- action: button.press
target:
entity_id: button.d2ha_update_all
Update all updates every container that has an update, one at a time: also the containers you hide from Home Assistant, and also the updates with breaking changes. It never updates D2HA itself or its socket proxy. With nothing to update, nothing happens.
A message when a container stops
alias: "D2HA: a container stopped"
triggers:
- trigger: state
entity_id:
- sensor.jellyfin_state
- sensor.vaultwarden_state
to:
- exited
- dead
for: "00:02:00"
actions:
- action: notify.notify
data:
title: Container stopped
message: >-
{{ device_attr(trigger.entity_id, 'name') }}:
{{ state_attr(trigger.entity_id, 'status') }}
The message leaves when one of the containers has been stopped for two minutes, and says what Docker reports about it, such as Exited (137) 2 minutes ago. The two minutes leave out a restart and an update, where the container is stopped only for a moment. When Home Assistant loses D2HA and finds it again, as after a restart of D2HA, a container that is still stopped is announced again.
An unhealthy container
A container with a health check can be unhealthy while it still runs: its State sensor stays on running, and the health is in the health attribute. This second automation tells you when a container has been unhealthy for two minutes:
alias: "D2HA: a container is unhealthy"
triggers:
- trigger: state
entity_id:
- sensor.jellyfin_state
- sensor.vaultwarden_state
attribute: health
to: unhealthy
for: "00:02:00"
actions:
- action: notify.notify
data:
title: Container unhealthy
message: "{{ device_attr(trigger.entity_id, 'name') }}"
D2HA can also send these messages by itself: new versions, containers that stop with an error and unhealthy ones. See Telegram.
Clean the images every week
alias: "D2HA: clean images every week"
triggers:
- trigger: time
at: "05:00:00"
conditions:
- condition: time
weekday:
- sun
actions:
- action: button.press
target:
entity_id: button.d2ha_clean_images
Every Sunday at 5:00, D2HA deletes the images that no container uses, running or stopped, as Clean images does in the web interface.
When an automation does not run
Open the automation and press Traces: every run is there step by step, with the error when one stopped it. update.install waits for the update to end: if it fails, D2HA puts the old container back and the run ends with the error.
Something wrong or missing on this page? Open an issue.