Automatically update Zigbee switches over Zigbee2MQTT

So, here is my challenge, I have about 30 switches (Hue dimmer switches) and door sensor (Aqara door sensors) in my environment, all are connected via Zigbee2MQTT to my home assistant. Now, when all the Hue Dimmer switches have an update available, I need to wake up each one, and then start the upgrade.

What if could start the upgrade automatically if the switch is pressed (awakened).

At least, I have to make sure there are not a ton of updates running already.

Guess what… I have made a working automation that does just that:

- alias: "Update switch when triggered"
  triggers:
    - trigger: event
      event_type: state_changed
  conditions:
    - and:
      - condition: template
        value_template: "{{
            trigger.event.data.entity_id in (
              states.update |
              selectattr('state','eq','on') |
              map(attribute='entity_id') |
              select('in',integration_entities('mqtt')) |
              select('match', '.*([sS]witch|[dD]immer).*') |
              map('regex_replace','^update\\.','sensor.') |
              map('regex_replace','$','_action') |
              list
            )
          }}"
      - condition: template
        value_template: "{{
            (
              states.update |
              selectattr('attributes.in_progress', 'eq', True) |
              map(attribute='entity_id') |
              select('in', integration_entities('mqtt')) |
              list |
              count
            ) < 2
          }}"
  actions:
    - action: system_log.write
      data:
        message: >
          Updating id: {{
            trigger.event.data.entity_id |
            regex_replace('^sensor\\.','update.') |
            regex_replace('_action$','') |
            string
          }}
        logger: UPDATE ZIGBEE SWITCH
        level: info
    - action: update.install
      target:
        entity_id: "{{
            trigger.event.data.entity_id |
            regex_replace('^sensor\\.','update.') |
            regex_replace('_action$','') |
            string
          }}"

All my switches and door sensors follow the same naming convention:

  • They all have Switch or Dimmer in the name

Now, if there are less than 2 updates running, an update will be started, if it is available, when the switch is pressed or triggered. (It also sends a log entry)

There is 1 point to keep in mind (so far):

  • it might take a while for the update to actually start so in theory you could start many updates if you trigger many switches before 2 updates are actually running

Just wanted to share this in case others might need it.

2 Likes