ESPHome update notifications

Hey there,

do you know whether all your ESPHome-devices are up to date? I had struggles with that and always needed to check the latest ESPHome-version on their website - and to be honest: I didn’t do it very often.
That was the reason for me to automate these steps and as I guess you have similar issues so I am happy to share my setup with everyone :slight_smile:

So let’s get it started!

1. ESPHome: Add version sensor to ESPHome-devices
To be able to determine the latest version of your ESPHome-devices, they need to send their information back to Home Assistant. The good thing is, ESPHome already has versioning in place and can be easily configured using the following code. Just simply add this to all ESPHome device configurations. If you don’t use substitutions, just simply replace the ${device_name} by something related to the name of your ESPHome-device.

# Device information and settings
substitutions:
  device_name: "my-awesome-esp-device"

# .....

# Enable reporting of version
text_sensor:
  - icon: mdi:application-braces-outline
    platform: version
    name: ${device_name}_version

2. Home Assistant: Add sensor for latest ESPHome-version
Our second step is to add a command line sensor to the Home Assistant configuration to gather the latest version of ESPHome based on the image tags published to the Docker Hub. To do so, just add the following command line sensor to your configuration.yaml (or dedicated file according to your setup):

command_line:
  # Sensor for latest ESPHome version (based on docker hub)
  - sensor:
       name: esphome_version
       command: curl -s -X GET https://hub.docker.com/v2/namespaces/esphome/repositories/esphome/tags?page_size=50 | jq -r '.results|.[]|.name' | egrep -i "^[0-9]{4}\.[0-9]{1,2}\.[0-9]{1,2}$" | sort -rn | head -1
       icon: mdi:application-braces-outline
       scan_interval: 21600 # time in seconds, expected refresh every 6 hours (6 hours * 60 minutes * 60 seconds = 21.600)

3. Home Assistant: Add sensor for update indication
As we now know the version of all of the connected ESPHome-devices and the latest version of ESPHome out there, we can simply get the outdated devices and store them as a another sensor.

Long story short: The following code section is a long one but it does all the required things we need. Feel free to adjust the English phrases in the section value_templates to your language.

For developers or all of you who want to know what the code technically does:
It collects all entities belonging to the integration esphome, finds their related devices and checks for an entity with the word version in the name. If you successfully added (and flashed) the configuration from step 1 to your devices, the sensor should find all device versions without any further configuration. To be able to automate things based on the sensor, it also stores the names of the outdated devices as attribute outdated_devices and the number of outdated devices as attribute outdated_devices_number.

sensor:
  - platform: template
    sensors:
      # Sensor for ESPHome updates
      calculated_esphome_updates:
        friendly_name: ESPHome Updates
        attribute_templates: 
          outdated_devices: >-
            {%- set entities =  integration_entities('esphome') -%}
            {%- set ns = namespace(outdated = []) -%}
            {%- for entity in entities -%}
              {%- set device_name = device_attr(device_id(entity), 'name') -%}
              {%- if 'version' in entity and states(entity) != "unavailable" -%}
                {%- if not states('sensor.esphome_version') in states(entity) -%}
                  {%- set ns.outdated = ns.outdated + [ device_name ] -%}
                {%- endif -%}
              {%- endif -%}
            {%- endfor -%}
            {{- ns.outdated -}}
          outdated_devices_number: >-
            {%- set entities =  integration_entities('esphome') -%}
            {%- set ns = namespace(outdated = []) -%}
            {%- for entity in entities -%}
              {%- set device_name = device_attr(device_id(entity), 'name') -%}
              {%- if 'version' in entity and states(entity) != "unavailable" -%}
                {%- if not states('sensor.esphome_version') in states(entity) -%}
                  {%- set ns.outdated = ns.outdated + [ device_name ] -%}
                {%- endif -%}
              {%- endif -%}
            {%- endfor -%}
            {{- ns.outdated | count | int -}}
        icon_template: >-
          {%- set entities =  integration_entities('esphome') -%}
          {%- set ns = namespace(up2date = [], outdated = []) -%}
          {%- for entity in entities -%}
            {%- if 'version' in entity and states(entity) != "unavailable" -%}
              {%- if states('sensor.esphome_version') in states(entity) -%}
                {%- set ns.up2date = ns.up2date + [ entity ] -%}
              {%- else -%}
                {%- set ns.outdated = ns.outdated + [ entity ] -%}
              {%- endif -%}
            {%- endif -%}
          {%- endfor -%}
          {%- if ns.outdated | count >= 1 -%}
            mdi:package-up
          {%- elif ns.up2date | count >= 1 -%}
            mdi:package-check
          {%- else -%}
            mdi:help-box
          {%- endif -%}
        value_template: >-
          {%- set entities =  integration_entities('esphome') -%}
          {%- set ns = namespace(up2date = [], outdated = []) -%}
          {%- for entity in entities -%}
            {%- if 'version' in entity and states(entity) != "unavailable" -%}
              {%- if states('sensor.esphome_version') in states(entity) -%}
                {%- set ns.up2date = ns.up2date + [ entity ] -%}
              {%- else -%}
                {%- set ns.outdated = ns.outdated + [ entity ] -%}
              {%- endif -%}
            {%- endif -%}
          {%- endfor -%}
          {%- if ns.outdated | count == 0 and ns.up2date | count >= 1 -%}
            No update available
          {%- elif ns.outdated | count == 1 -%}
            Update available for one device
          {%- elif ns.outdated | count >= 2 -%}
            {{- "Updates available for " + ns.outdated | count | string + " devices"-}}
          {%- else -%}
            No ESPHome-device found
          {%- endif -%}

4. Home Assistant: Add automation for notification
The last step is to implement the automation which sends the notification to the app on your phone so you know you need to update the ESPHome-devices (again). Please adjust the devices/notification endpoints in the following code to your needs (I mean replace service: notify.mobile_app_xxxxx e.g. by service: notify.mobile_app_dominiks_iphone).

automation: 
  # ------------------------------------------------------------
  # |                                                          |
  # | Sends notifications for new version updates              |
  # | for ESPHome.                                             |
  # |                                                          |
  # ------------------------------------------------------------
  - alias: Sends notifications on new ESPHome versions
    id: notification_esphome_version_updates
    description: Sends a notification to the app of your phone if an update is available for at least one of your ESPHome-devices
    mode: single

    trigger:
      - platform: template
        alias: Triggers if there is an update for at least one ESPHome-device
        value_template: "{%- if state_attr('sensor.calculated_esphome_updates', 'outdated_devices_number') >= 1 -%}true{%- endif -%}"
        for:
          hours: 0
          minutes: 5
          seconds: 0

    action:
      - service: notify.mobile_app_xxxxx
        alias: Sends notification about new ESPHome-version to the app of your phone
        data:
          message: "The new version {{ states('sensor.esphome_version') }} of ESPHome is available"
          title: ESPHome update available
          data:
            group: esphome_version_update

Please note:

  • If you change the names of the sensors used, please remember to update the references as well.
  • After you added a new ESPHome device to your configuration, it either needs a restart of Home Assistant or an update of the ESPHome-version which runs all 6 hours as per configuration to consider it for the sensor of step 3 and therefore, for the automation of step 4.

I hope this also helps you to keep track of the latest ESPHome updates and be able to use their latest (awesome) features!

Please let me know whether there is something to improve in the config :slight_smile:

Best regards,
Dominik

2 Likes

Yes. I just look at the ESPHome add-on web interface. The devices that need updates are clearly marked in blue.

Alternatively, rather than “reinventing the wheel” each ESPHome Device already includes an update entity that is used to generate update notifications in Home Assistant. You can also generate your own notifications using these entities.

1 Like

Hi Tom,
if ESPHome is running all the time and accessible from Home Assistant e.g. if running the Home Assistant OS, it’s visible all the time, you’re totally right.

My setup is based on the Docker-version of Home Assistant and running in a Podman container. Therefore, ESPHome is not directly accessible via Home Assistants web interface or the app and in addition it’s not running all the time on my device. So to start ESPHome and look at the blue boxes requires my attention and the start of the related containers first :slight_smile:

As Tom says, each of my esphome integrations (devices) has an update entity

Did you configure this somewhere?
I run the latest version of ESPHome (2023.11.6) and none of my ESPHome-devices have such an entity:

No but it might be part of the add-on ?

That’s the main part: I am using the Core-version of Home Assistant and ESPHome and therefore, do not have add-ons and also no chance to access ESPHome directly from the Home Assistant web interface.

Yes I saw that, wasn’t sure it was because of that, but guess it must be the update entities are only when you use the addon. Are you using the Native API or MQTT ?

That seems highly unlikely. An addon is just extra software running on the same host. It is the ESPHome integration making the entities. But it is likely you’ll need to use the API and not MQTT, as they are different ways of exposing entities.

However, be careful what you wish for if you have many esp devices :wink:

Home Assistant connects via ESPHome Native API in my setup. Works amazing! :slight_smile:

To be honest, I am using the Native API but no update entities are offered for now. Which kind of Home Assistant setup are you using? The Home Assistant OS?

For now there are 9 ESPHome based devices (mainly ESP8266) - but increasing over the last days and months

Yes, using HAOS and the addon. I have disabled the update entities now as I have 81 devices and get update notifications almost every week for ever .1 release, for all 81 !

I just update all devices at the .0 release

Might be worth asking on the esphome discord ?

Just seen this on discord after a search:
1. ### taskmaster 12/04/2023 01:29
the new fw update entity is run by HA?
2. ### jesserockz 12/04/2023 01:29
Yes, its entirely in HA based on the addon version

EDIT: I think this is the addon update entity !

The update entities should be there from esphome 2023.2 provided your HA instance is also up to that level:

Could it be the entities are only made if you add new ones? Have you tried removing the device and adding them again to HA?

Edit: the above post mentions the addon version, so maybe I’m proven wrong in that respect.

The update check for esphome nodes doesn’t work without the esphome dashboard add-on installed.

By default the add-on communicates with HA and will tell the esphome version which then is checked against the esphome node versions.

I just disabled the communition between the esphome add-on and HA which stops all update notifications for my 60 esphome nodes with just one toggle switch.

Is it this toggle you turn off ? Does it affect anything else ?

Home Assistant Dashboard Integration
Enables/Disables the ESPHome dashboard integrating with Home Assistant for automatic configuration of devices and device updates. If you use multiple version of the ESPHome add-on, make sure it is enabled on a single add-on only.

Yes, exactly

No, nothing I’m aware of. All my esphome nodes continue to be rock solid and work like expected.

Nice, thanks. Does it affect the automatic discovery of new nodes ?

Not at all. The esphome add-on integration doesn’t affect the esphome integration at all.

Essentially this setting just tells the add-on to don’t tell HA what version of the add-on is installed which leads to no more update notifications for all esphome nodes.

This is NOT the esphome integration but a dedicated one for comes between add-on and HA which btw. Isn’t present for core/docker install and that’s why this post exists in the first place!

1 Like

Care to share exactly how you do that?

I asked the same, they mean this toggle in the addon:

Home Assistant Dashboard Integration
Enables/Disables the ESPHome dashboard integrating with Home Assistant for automatic configuration of devices and device updates. If you use multiple version of the ESPHome add-on, make sure it is enabled on a single add-on only.

Fixed a bug which caused unavailable devices to be marked as outdated:
{%- if 'version' in entity -%}
was replaced by
{%- if 'version' in entity and states(entity) != "unavailable" -%}
The code in the original post was updated accordingly :slight_smile: