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
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
Best regards,
Dominik