Sensor for latest docker image versions

I’ve been playing around with regex and here are some examples in case others would like to use them.
Use with @Lapatoc’s code.

Find the latest tag that does not include characters (original):
{% for result in results if not result.name |regex_search("[a-z]",ignorecase=TRUE) -%}

Find the latest tag named #.#.# (eg. 0.88.2)
{% for result in results if result.name |regex_search("\d+\.\d+\.\d+$") -%}

Find the latest tag named #.#.#something (eg. 0.89.2b2)
{% for result in results if result.name |regex_search("\d+\.\d+\.\d+") -%}

Find the latest tag named #.# (eg. 2.6)
{% for result in results if result.name |regex_search("\d+\.\d+$") -%}

Find the latest version named amd64-#.#.# (eg. amd64-2.05.59)
{% for result in results if result.name |regex_search("amd64-\d+\.\d+\.\d+$") -%}

Example notification for @Lapatoc’s code:

- alias: 'Notify - Home Assistant docker tag changed'
  trigger:
    - platform: state
      entity_id: sensor.ha_docker_version
  action:
    - service: persistent_notification.create
      data_template:
        message: "Home Assistant docker tag changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}"
2 Likes

Minor tweak: Don’t trigger if sensor is unavailable.

  condition:
    condition: template
    value_template: >
      {{ trigger.to_state.state not in [trigger.from_state.state, 'unavailable'] }}

Has anyone figured out how to get the versions of containers in other things apart from HA?
I have Zigbee2MQTT and NginX running and I’d like to get those if possible. I’ve had a look at docker inspect and unfortunately Zigbee2MQTT doesn’t report it back. I have found it in the log however I’ve no idea how I’d parse that back to HA.

With NginX I can get the build version but it’s in a really long format:
Linuxserver.io version:- 0.32.0-ls15 Build-date:- 2019-03-30T16:26:24+00:100:
This doesn’t seem to work in HA either

 - platform: command_line
   name: docker_version_NginX
   command: "docker inspect -f '{{index .Config.Labels.build_version }}' NginX"
   scan_interval: 3600

Can you post the code for this? Including the sensors?

This lovelace card with standard sensors.
Sensors:

sensor:

  - platform: template
    sensors:
      ha_startup_time:
        value_template: >-
          {{ as_timestamp((states.automation.startup_notification.attributes|default).last_triggered|default(None))|timestamp_local or 'unknown' }}

  - platform: version
    beta: false
    source: docker
    name: ha_version_docker_stable

  - platform: version
    name: ha_version
    source: local
    beta: false

  - platform: version
    name: ha_pypi_version
    source: pypi
    beta: false

  - platform: rest
    name: ha_github_version
    resource: 'https://api.github.com/repos/home-assistant/home-assistant/releases/latest'
    headers:
      accept: application/vnd.github.v3+json
      content-type: application/json
      user-agent: home-assistant/rest-sensor
    value_template: >-
      {{ value_json.tag_name if value_json.tag_name.split('.')|length != 2 else value_json.tag_name ~ '.0' }}
    scan_interval: 1800

card:

- type: vertical-stack
    cards:
      - type: entities
        title: Home-Assistant
        show_header_toggle: false
        entities:
          - sensor.ha_startup_time
          - sensor.ha_version
          - sensor.ha_github_version
          - sensor.ha_pypi_version
          - sensor.ha_version_docker_stable
2 Likes

Do you know what the secret is to getting version info from “Official” repositories?

This is the one I am trying to get updated info for; I could try to pull from this page but I like to keep all my sensors similar. I have seven others that work great but there is something about this one that gets me.

https://hub.docker.com/_/eclipse-mosquitto

Sorry, dont use this anymore. Update the containers with portainer now.

I was thinking about this today.
I use a docker container called DIUN (Docker Image Update Notifier) - https://github.com/crazy-max/diun
This monitors your docker containers and looks for updated images on a regular basis. It can send notifications and, having checked today, it can also sent notifications to MQTT or WebHook, which means it could be used to trigger HASS.
Watchtower could well do this as well, but doesn’t have MQTT or Webhook - I’ve raised a new feature request on their GitHub for this.

3 Likes

I ended up going a different route on this.
I have a cronjob script run to download the newest image (just download), since docker will only download if there is an updated version of whatever tag you are using.
Then I compare the number of docker images with the number of containers. If there are more images then containers then I have a container to update. This works for me based on my setup; I have all my containers restart (this works for all except MaryTTS sometimes for some reason). I don’t have containers that don’t run (most of the time).
I am debating, since I think I am stable at the number of containers I run, just grep the name of images and if the count is more than one then that one is in need of updating.
Doing it this way has the benefit of not having to wait on a container to download when I find an update is available.

That being said, I will look into Diun.

If you used docker-compose it would do all that for you…

I didn’t like certain aspects of docker-compose; I found it difficult to manage images independently and still have them interact on the same docker network. I’m sure if I play with it long enough I could get it to work, but my way works for me (currently).

Then you were doing it wrong lol.

I don’t disagree.

Can I ask how you use your labels, I always had an issue with the exclusion label having problems with double quoting.

Hi,

I have the same need.
(tracking down my Docker containers to get notified when updates are available)
and I ended up with my own solution: What’s up Docker?

It integrates seamlessly with Home-assistant using Mqtt Discovery.

I run it at home for some time and now some other Home-assistant addicts seem to enjoy it as well :slight_smile:.

Here is the topic around the tool.

You can give it a try if you wish.
And feel free to ask for support or raise issues on Github if needed!

7 Likes

Hi All,

i didin’t know why, but i’ve the error above to the definition of the sensor template.

AssertionError: Tried to resolve a name to a reference that was unknown to the frame ('result')

Did anyone have this error in the last HA version?

Thanks

Has anyone integrated diun with home assistant via mqtt?

No, but What’s Up Docker integrates easily!

Thanks, but It doesn’t work on kubernetes. I use home assistant deployed on k3s.

I did try integrating DIUN with HASS once. You can do it either via sending a webhook to HASS, or by using MQTT.
It worked, but I found it annoying that I had to setup individual entities for each and every container individually to achieve what I wanted.