How to bulk restart ESPHome devices?

Hi All,

I have a couple of ESPHome devices. All of them having a restart button:

button:
  - platform: restart
    name: Restart
    id: ${devicename}_restart

I would like to make bulk restart possible. How can I call all the Restart buttons which blongs to ESPHome devices without listing each device one by one? In case of adding new ESPHome device later, I’d like that to be included automatically as well in the “Restart All ESP” action.

Thanks in advance!

If its only 2 why go to the bother?

Add a Restart Button to each device, then in HA write a script or automation that executes those.

As you can see, the Restart button is already there. How should I configure the automation to run against all the ESPHome device’s Restart buttons without adding them one by one?

Sorry - yes I see you have the restart button - it’s late here… :slight_smile:

No I can’t think of a way that would be any different to adding them to an automation. You could probably assign them a label and have a generic automation that acts on that - but no different to adding them manually…

I have tried the label but might did something wrong as it didn’t worked for me.

And then set the action like this:

I confirmed the automation was triggered but did not rebooted the ESPs which restart buttons were labeled. Also tried to set the label on the ESPHome device as well without luck.

Anyways, thanks for the hints!

Just create a regular automation where the action you want to perform is " Button ", then " Press ", then Choose entity. A list will appear of all the buttons you can push in your environment, select the various ESP reboot buttons you have defined

The issue is the OP wanted to find a way where he did not have to pick each device separately and more importantly not need to remember to add any new devices to the automation. Sort of like how a device class works.

Home Assistant Core Integration - Home Assistant

2 Likes

Understood. I went with a quick and easy solution as ( to my mind anyways ) adding a new reboot entity ( from a newly added device ) to an existing automation isn’t much effort.

That said, I liked the OP’s idea. So I cheated and turned to ChatGPT. It came up with the following. Create a script in the UI using YAML mode

alias: Reboot All ESPHome Devices
mode: single
sequence:
  - variables:
      reboot_buttons: >
        {{ states.button
          | selectattr('attributes.friendly_name', 'search', 'Reboot')
          | selectattr('entity_id', 'search', 'esp')
          | map(attribute='entity_id')
          | list }}
  - repeat:
      for_each: "{{ reboot_buttons }}"
      sequence:
        - service: button.press
          target:
            entity_id: "{{ repeat.item }}"

I only have one spare ESP32 hanging around. So I gave it a whirl. True as Bob it works :slight_smile:

Note that I’m using the name Reboot, not Restart

A version that supports the name Restart. Tested successfully

alias: Restart All ESPHome Devices
mode: single
sequence:
  - variables:
      restart_buttons: |
        {{ states.button
          | selectattr('attributes.friendly_name', 'search', 'Restart')
          | selectattr('entity_id', 'search', 'esp')
          | map(attribute='entity_id')
          | list }}
  - repeat:
      for_each: "{{ restart_buttons }}"
      sequence:
        - target:
            entity_id: "{{ repeat.item }}"
          action: button.press
description: ""

This gave me the base of the solution. Thank you very much!
For future referece:
I have updated all of my ESP’s Restart button from Restart to ESP_Restart, to uniquely mark this restart belongs to an ESP device:

button:
  - platform: restart
    name: ESP_Restart
    id: ${devicename}_restart

Then the final script which will collect all ESP based restart buttons:

alias: Restart All ESPHome Devices
mode: single
sequence:
  - repeat:
      for_each: |
        {{ states.button
            | selectattr('attributes.device_class', 'equalto', 'restart')
            | selectattr('attributes.friendly_name', 'search', 'ESP_Restart')
            | map(attribute='entity_id')
            | list }}
      sequence:
        - data:
            entity_id: "{{ repeat.item }}"
          action: button.press

Thank you very much for the grat hints!