Add connection state (online/offline) to every z2m device (or other MQTT device)

Hi all,

Some time ago I published this thread: https://community.home-assistant.io/t/add-network-state-online-offline-to-every-z2m-device-in-3-easy-steps. In that post, I explained how to add a connection state sensor using templates. While it worked, everything had to be maintained by hand — and as soon as something changed, things would break :warning:.

So I set out to make a custom component that has all the features that I was missing. This new custom integration can:

  • :mag: Discover MQTT devices with connection topics
  • :arrows_counterclockwise: Auto-update when device information or topics change
  • :rotating_light: Raise Repair issues when a device becomes orphaned
  • :zap: Use actions for bulk setup in new installs
  • :bell: Easily trigger notification automations using events

I’d love to hear your feedback, ideas, or bug reports :speech_balloon:

Check it out here:

Kind regards,
- Ingrid

Edits
  1. Moderator move to custom_components (Thanks!)
  2. Added new feature: Events
  3. Take it out of beta.
6 Likes

Ingrid, you are amazing! I was just revisiting your original concept, since I added a few new devices to my network. Usually I stay away from betas, but I will give it a try and report any issues on the repo - if any :slight_smile:
Thanks so much for your work! Hope this may be adopted some time into the core or z2m :slight_smile:

Am I correct to first delete the manual yaml to avoid any collisions?

Hi,

The old template sensors were named “network state”, so existing entity IDs will look like binary_sensor.bathroom_network_state.
In the new integration, entities are named “connection” — which I think better reflects their actual purpose — and will use the entity ID binary_sensor.bathroom_connection_state.
The integration also checks whether binary_sensor.bathroom_connection_state already exists. If it does, it will automatically create binary_sensor.bathroom_connection_state_2.

This makes the risk of entity ID collisions minimal. That said, you will need to update your automations to use the new entity IDs.

Kind regards,
- Ingrid

1 Like

I just released version beta-0.3.0, the last one for now. I’ve added “events” that can be used to trigger automations:

Example automation:

alias: "Notify conecction state offline"
description: "Using the mqtt_conecction_state events for monitoring"
mode: parallel
max: 50
trace:
  stored_traces: 50
triggers:
  - trigger: event
    event_type: mqtt_connection_state_changed
    event_data:
      state: offline
conditions:
  - condition: state
    entity_id: binary_sensor.zigbee2mqtt_bridge_connection_state
    state: "on"
    for:
      minutes: 1
actions:
  - alias: Report offline devices in HA notifications
    action: persistent_notification.create
    data:
      title: Device offline
      message: >-
        {{ trigger.event.data.device_name }} went offline</br></br>
        <small><a href="/config/devices/device/{{ trigger.event.data.device_id }}">Device</a></small></br>
        <small>{{ now()  | as_timestamp | timestamp_custom('%H:%M') }}</small>
  - alias: If important device send notification to phone
    if:
      - condition: template
        value_template: >-
          {% set important_devices = [
            'binary_sensor.livingroom_motion_connection_state',
            'binary_sensor.livingroom_climate_connection_state'
          ] %}
          {{ trigger.event.data.entity_id in important_devices }}
    then:
      - action: notify.mobile_app_iphone
        data:
          title: Device offline
          message: "{{ trigger.event.data.device_name }} went offline"

All features I wanted to add in the beta are now live. So please update to beta-0.3.0 and test the automation.

If I have enough feedback from people using it, without any bugs, I will take it out of beta.

Kind regards,
- Ingrid

1 Like

Seems to work just fine for me.

I do have other mqtt integrations though (like HASS.Agent), and they all say Disconnected.

Btw; added a PR with the swedish translation to Github.

Thanks for your reply, I have only tested a few other MQTT integrations. I will look at HASS.Agent.
I merged your translation into the beta-0.3.1, which will release around next week.

EDIT: HASS.Agent published availability data in a different format, quick fix :slight_smile:. Will also go live in next release.

Kind regards,
- Ingrid

1 Like

OK, seems to be the same problem with my own firmwares. What did you need to change?

Hi Jonny,

Thank you for providing feedback!

I just did a prerelease; that should resolve the error.
HACS > mqtt_connection state > three dots menu > redownload > different version > beta-0.3.1pre > restart.

You will get an alert that there is a new version available; this always triggers if the version you installed (beta-0.3.1pre) does not match the last release (beta-0.3.0). Just ignore it.

If the error is still there, can you please send me your debug log?
Settings > Integrations > mqtt_connection_state > three dots menu > enable debug logging.

Do a restart, wait for the sensor to say “Disconnected”, then click Disable, and a log will be downloaded. Please send me that file, so I can evaluate the messages received.

Kind regards,
- Ingrid

I did some rewrites and bug fixes. And I’m happy to say I took the integration out of beta and just did the first stable release.

Kind regards,
- Ingrid

2 Likes

Hello Ingrid.
After removing the Integration log continuously displays error:

Logger: homeassistant.components.websocket_api.commands
Source: components/websocket_api/commands.py:670
integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 11:23:32 (6 occurrences)
Last logged: 11:51:11

Unable to get manifest for integration mqtt_connection_state: Integration 'mqtt_connection_state' not found.

Started digging inside , .storage/core.entity_registry has left followed lines for entities:
{“aliases”:,“area_id”:null,“categories”:{},“config_entry_id”:“xxxxxxxxx”,“config_subentry_id”:null,“created_at”:“2026-02-06T07:37:28.628116+00:00”,“device_class”:null,“disabled_by”:null,“disabled_by_undefined”:false,“entity_id”:“update.mqtt_connection_state_update”,“hidden_by”:null,“hidden_by_undefined”:false,“icon”:null,“id”:“xxxxxx”,“labels”:,“modified_at”:“2026-02-08T08:34:02.569841+00:00”,“name”:null,“options”:{“conversation”:{“should_expose”:false}},“options_undefined”:false,“orphaned_timestamp”:null,“platform”:“hacs”,“unique_id”:“xxxxxx”},

Looks like it’s not the best approach of implementation. Besides the reason I started to remove the integration was that something is blocking Home Assistant from wrapping up the startup phase.

Pressed to clean up manually:

config/.storage/core.config_entries
config/.storage/core.entity_registry

But any way,

Ingrid many thanks for your efforts!!!

Hi Roma,

The first error should be resolved after you have force removed the config_entries and entity_registry.
I did extensive adding and removing tests, and I did not encouter this error.

Please send the whole debug log, so I can debug what happend that caused this error.
It would be even better if you can find the log from before the remove, than I can debug what was causing the block in the startup fase.

Kind regards,
- Ingrid