RogerSelwyn / mqtt_discoverystream_ha

Extension of HA mqtt_statestream integration with discovery config publishing
3 stars 5 forks source link

Intermittent issues with a templated switch entity #12

Closed miguelangel-nubla closed 5 months ago

miguelangel-nubla commented 5 months ago

I am trying to create a automation to check periodically if everything is working fine. The idea is to have a switch entity exposed to the slave, because the entity I want to make sure works is also a switch entity. When the "ping" switch is turned on from the slave HA instance it should propagate to master, turn on as expected and turn off after 2 seconds, then check that state change was propagated to the slave. It worked fine for a few hours, then had intermittent issues. Now it is not working at all. Tried the usual reboot and connectivity check.

Here is the config used:

input_boolean:
  homeassistant_public_ping_helper:
    name: homeassistant_public_ping_helper
    icon: mdi:lock-alert

switch:
  - platform: template
    switches:
      homeassistant_public_ping:
        value_template: "{{ is_state('input_boolean.homeassistant_public_ping_helper', 'on') }}"
        turn_on:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.homeassistant_public_ping_helper
          - delay:
              seconds: 2
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.homeassistant_public_ping_helper
        turn_off: []

mqtt_discoverystream_alt:
  base_topic: homeassistant_public
  local_status:
    topic: homeassistant/status
  publish_attributes: false
  publish_timestamps: true
  publish_discovery: true
  include:
    entities:
      - switch.homeassistant_public_ping
      - input_boolean.homeassistant_public_ping_helper # added for debug only
      ...

Here is what I found so far:

MQTT state: Screenshot 2024-04-26 073612

Logs do not show anything strange:

2024-04-26 07:32:10.991 INFO (MainThread) [custom_components.mqtt_discoverystream_alt.classes.base_entity] MQTT 'cover' subscribe successful
2024-04-26 07:32:11.453 INFO (MainThread) [custom_components.mqtt_discoverystream_alt.classes.base_entity] MQTT 'switch' subscribe successful
2024-04-26 07:32:11.842 INFO (MainThread) [custom_components.mqtt_discoverystream_alt.classes.base_entity] MQTT 'input_boolean' subscribe successful
2024-04-26 07:32:11.849 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] Discovery published
2024-04-26 07:32:15.358 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] States published
2024-04-26 07:36:35.078 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] Discovery published
2024-04-26 07:36:36.621 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] States published
2024-04-26 07:41:36.670 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] Discovery published
2024-04-26 07:41:38.218 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] States published
2024-04-26 07:46:38.259 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] Discovery published
2024-04-26 07:46:39.867 DEBUG (MainThread) [custom_components.mqtt_discoverystream_alt.publisher] States published
RogerSelwyn commented 5 months ago

I’m on the road at the moment and away from home for a week so not easy for new to test your specific config.

I’ll try to read your post when I have a big screen and see if I can understand what is going on.

RogerSelwyn commented 5 months ago

I think your switch doesn't appear in the entity registry (no I don't understand the details) because it doesn't have a unique_id. Basically I scan the whole entity registry when creating the discovery configs, so I suspect it will only get config sent when the value changes, which then may create a race condition.

Add a unique_id and you should be sorted I think:

      homeassistant_public_ping:
        unique_id: homeassistant_public_ping
        value_template: "{{ is_state('input_boolean.homeassistant_public_ping_helper', 'on') }}"
miguelangel-nubla commented 5 months ago

Spot-on. The switch now works as expected.

If unique_id in every entity is to be required, maybe logging a warning "entity.id does not have a unique_id, will be ignored" and ignore it completelly makes sense.

Regarding:

  • Somehow topics for homeassistant_public_ping_helper are on <discovery_topic>/switch/homeassistant_public_ping and <discovery_topic>/input_boolean/homeassistant_public_ping ???

Just noticed the new "Entity Type" -> "Entity Created" table. My bad. Everything working as expected.

Thanks for your hard work on this.

RogerSelwyn commented 5 months ago

If unique_id in every entity is to be required, maybe logging a warning "entity.id does not have a unique_id, will be ignored" and ignore it completelly makes sense.

Probably easier said than done, since the entity list in the config is a standard HA filter which can include various globs, whole domains, etc. I need to do some more testing when I get back and see if there is a method for getting all entities rather than this in the entity registry.

  • Somehow topics for homeassistant_public_ping_helper are on <discovery_topic>/switch/homeassistant_public_ping and <discovery_topic>/input_boolean/homeassistant_public_ping ???

Just noticed the new "Entity Type" -> "Entity Created" table. My bad. Everything working as expected.

Yep, input_boolean is not supported by HA MQTT (I looked at adding support for it to HA, but decided it was too complex), so it gets created as a switch.