Unattended Update/ Update Button/ Scheduled Restart

I’ve seen a few forum posts around here wanting to put an update button on your dashboard or wanting to be able to run the update automatically (do not do this) or run the update unattended (not recommended).

My Use Case
I run HA OS - if you are running in any other mode, you have much easier access to other tools to do this and I don’t know if this will work in the install method you have chosen.

I also run HACS. The custom integrations its provides often requires a restart of HA before those integrations work. For this reason, I wanted to be able to schedule restarts overnight.

I wanted to run the update unattended when I knew for certain no one would need to use the system to control lights etc. In my particular use case, my google home device which is linked to HA (depending on when it chose to poll HA for updates) would not work for a few minutes in the best case after HA had fully rebooted or in the worse case if google home had just decided to poll HA for its current devices while down might not work for between 10 to 30 minutes (not exactly sure on how the timing works with this). Again, a few minutes downtime is acceptable for me but having it not work 30 minutes later, I would invariably have my partner asking me why can’t I turn on the lights using google home.

Read Me First
If you choose to have unattended updates like me, make sure that you:

  1. Read the patchnotes and breaking changes. Running the update automatically is asking for serious trouble.
  2. Choose a schedule that is convenient for you. I run it over night on a weekend knowing that if anything breaks I will have an hour or so free the next morning to restore a backup.
  3. Understand how to restore from backups.
  4. Know that, despite the above, it is still not recommended by HA to install updates in this way.

I’m relatively new to HA so there may be some other considerations - chime in on the replies and I’ll add them here.

Scheduled Restart
In configuration>automations & scenes>helpers, we are going to set up an input that enables the reboot and an input which sets the time which the reboot will happen.

I have:

Helper
Type: Toggle
Name: Reboot Enable
Icon: mdi:cog-clockwise

and

Helper
Type: Date and/ or Time
Name: Reboot Time
Icon: mdi:cog-clockwise
Input Type: Time

Add these to your lovelace - here is my yaml for that:

type: horizontal-stack
title: Reboot
cards:
  - type: button
    tap_action:
      action: toggle
    entity: input_boolean.reboot_enable
    show_name: false
    show_state: false
    icon_height: 40px
  - type: entities
    entities:
      - entity: input_datetime.reboot_time
        name: ' '
        icon: mdi:none
    show_header_toggle: false
    state_color: true
view_layout:
  position: sidebar

I’m using the horizontal stack but obviously this is only an example and feel free to lay it out how you want. This is what it looks like in lovelace (wish I could fix the alignment of the time but I can’t figure out how to do that):
image
In Configuration>automation & scenes>automations, we are going to set up the automation that makes this work. Here is my yaml:

alias: Restart
description: ''
trigger:
  - platform: time
    at: input_datetime.reboot_time
condition:
  - condition: state
    entity_id: input_boolean.reboot_enable
    state: 'on'
action:
  - service: input_boolean.turn_off
    data: {}
    target:
      entity_id: input_boolean.reboot_enable
  - service: homeassistant.restart
    data: {}
mode: single

At your set time each day, this checks if reboot enable is on. If it is, then it turns reboot enable off and restarts HA.
Scheduled/ Unattended Update
I will be showing my example for a core update but an OS update is similar except replace core everywhere with os.
I wanted to do this so that a vanilla installation of HA could do this without any addons. Again, I’m not experienced with this but the best way I found was to call HA supervisors update process rather than manually trying to update (at least this triggers the internal configuration checks etc).
In your configuration.yaml, set up the following shell commands:

shell_command:
  update_core: 'curl -X POST -ssl -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/core/update'
  create_backup: 'curl -X POST -ssl -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/backups/new/full'

Set up the following input helpers:

Helper
Type: Toggle
Name: Update HA Core Enable
Icon: mdi:update

and

Helper
Type: Date and/ or Time
Name: Update HA Core Time
Icon: mdi:update
Input Type: Time

Add these to your lovelace - example yaml below:

type: horizontal-stack
title: Update HA Core
cards:
  - type: button
    tap_action:
      action: toggle
    entity: input_boolean.update_ha_core_enable
    show_name: false
    show_state: false
    icon_height: 40px
  - type: entities
    entities:
      - entity: input_datetime.update_ha_core_time
        name: ' '
        icon: mdi:none
    show_header_toggle: false
    state_color: true
view_layout:
  position: sidebar

See above restart for what this looks like.
And finally create the following automation:

alias: Update HA Core
description: 'Performs a full backup of HA then updates it'
trigger:
  - platform: time
    at: input_datetime.update_ha_core_time
condition:
  - condition: state
    entity_id: input_boolean.update_ha_core_enable
    state: 'on'
action:
  - service: input_boolean.toggle
    data: {}
    target:
      entity_id: input_boolean.update_ha_core_enable
  - service: shell_command.create_backup
    data: {}
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: shell_command.update_core
    data: {}
mode: single

At your set time each day, this checks if HA core update enable is on. If it is, then it turns core update enable off, performs a full backup, waits 5 minutes for the backup to complete and executes a HA core update.

You may wish to lengthen the backup time if you are running a more complex installation of HA or are running on less powerful hardware (5 minutes is fine on my RPi4).

This does not check if there is in fact an update, I am relying on the fact that I will have read the patch notes and ensured there is an update before turning this on.

2 Likes