bieniu / ha-shellies-discovery-gen2

Script that adds MQTT discovery support for Shellies Gen2 devices
Apache License 2.0
107 stars 20 forks source link

Discovery of 2pm device not working #508

Closed lusluslus closed 4 weeks ago

lusluslus commented 4 weeks ago

Describe the bug I followed the instructions at https://github.com/bieniu/ha-shellies-discovery-gen2/blob/master/README.md to install and configure Shellies Discovery Gen2

I already had gen1 devices setup through the other script.

I copy and pasted the config and modifed the device-id to match mine. See config below.

After restarting homeassistant nothing happened, I enabled the debug logging but it didn't say anything useful, it triggered the shellies_discovery_gen2 but it didn't do anything beyond that.

I ran mqtt explorer to see what was going on. I could see my device was sending messages under its own topic, but it was not configured as a homeassistant device.

I then looked at the automation to see what had happend there, and I noticed that it tried to execute it and failed image image

Next I went to mqtt explorer and tried to manually send the {'id': 1, 'src': 'shellies_discovery', 'method': 'Shelly.GetConfig'} payload to shellyplus2pm-08b61fccde74/rpc to see if it would trigger the script and what the result would be. To my surprise it triggered the full setup flow and the device was successfully configured!

I think there is a bug in the instructions and/or that the automation with multiple mqtt publish action at once is causing it to fail. I see that there was a recent update to the instructions in https://github.com/bieniu/ha-shellies-discovery-gen2/pull/483 and I suspect that this broke something.

Expected behavior That it would get recognized automatically once added to device_ids

Versions:

Shellies Discovery GEN2 automations:

- id: shellies_announce_gen2
  alias: "Shellies Announce Gen2"
  triggers:
    - platform: homeassistant
      event: start
  variables:
    get_config_payload: "{{ {'id': 1, 'src': 'shellies_discovery', 'method': 'Shelly.GetConfig'} | to_json }}"
    get_components_payload: "{{ {'id': 1, 'src': 'shellies_discovery', 'method':'Shelly.GetComponents'} | to_json }}"
    device_ids:  # enter the list of device IDs (MQTT prefixes) here
      - shellyplus2pm-08b61fccde74
  actions:
    - repeat:
        for_each: "{{ device_ids }}"
        sequence:
          - action: mqtt.publish
            data:
              topic: "{{ repeat.item }}/rpc"
              payload: "{{ get_config_payload }}"
          - service: mqtt.publish
            data:
              topic: "{{ repeat.item }}/rpc"
              payload: "{{ get_components_payload }}"

- id: shellies_discovery_gen2
  alias: "Shellies Discovery Gen2"
  mode: queued
  max: 999
  triggers:
    - platform: mqtt
      topic: shellies_discovery/rpc
  actions:
    - condition: template
      value_template: "{{ 'mqtt' in trigger.payload_json.result }}"
    - action: python_script.shellies_discovery_gen2
      data:
        id: "{{ trigger.payload_json.src }}"
        device_config: "{{ trigger.payload_json.result }}"
    - service: mqtt.publish
      data:
        topic: "{{ trigger.payload_json.result.mqtt.topic_prefix }}/command"
        payload: "status_update"

Shelly device MQTT settings: Screenshot from 2024-10-29 13-15-41

Debug log:

Please not that this log is after I got it to work so it will show the device as a homeassistant device home-assistant.log

lusluslus commented 4 weeks ago

I setup a second device, and I expirenced the same behavoir. The automation doesn't trigger it to setup, but when I send a manual mqtt message to the shellyplus2pm-d4d4da09c9bc/rpc it triggers the full flow

Screenshot 2024-10-29 at 13 52 37

home-assistant.log

bieniu commented 4 weeks ago

MQTT integration discovered your devices

[homeassistant.components.mqtt.discovery] Found new component: sensor shellyplus2pm-08b61fccde74-0-power_factor from external application Shellies Discovery Gen2, version: 3.3.0, support URL: https://github.com/bieniu/ha-shellies-discovery-gen2
lusluslus commented 4 weeks ago

Yes, but only after I manually broadcasted the mqtt message {'id': 1, 'src': 'shellies_discovery', 'method': 'Shelly.GetConfig'}

I think there is something wrong with the HA automation scripts that makes it not work automatically

bieniu commented 4 weeks ago

Probably MQTT component is not ready when announce automation starts, you can check the trace for this automation and you can add some delay. From my point of view the script is working properly.

lusluslus commented 4 weeks ago

Thanks, I doubt it's the MQTT component though.

Here is a full reproduction when adding a 3rd device

First I'm setting mqtt setttings on the shelly shelly-mqtt-b0b4

It shows up in mqtt-explorer: mqtt-explorer-hello-b0b4

Next I update automations.yaml with this:

- id: shellies_announce_gen2
  alias: "Shellies Announce Gen2"
  triggers:
    - platform: homeassistant
      event: start
  variables:
    get_config_payload: "{{ {'id': 1, 'src': 'shellies_discovery', 'method': 'Shelly.GetConfig'} | to_json }}"
    get_components_payload: "{{ {'id': 1, 'src': 'shellies_discovery', 'method':'Shelly.GetComponents'} | to_json }}"
    device_ids:  # enter the list of device IDs (MQTT prefixes) here
      - shellyplus2pm-80646fcfb0b4
  actions:
    - repeat:
        for_each: "{{ device_ids }}"
        sequence:
          - action: mqtt.publish
            data:
              topic: "{{ repeat.item }}/rpc"
              payload: "{{ get_config_payload }}"
          - service: mqtt.publish
            data:
              topic: "{{ repeat.item }}/rpc"
              payload: "{{ get_components_payload }}"

- id: shellies_discovery_gen2
  alias: "Shellies Discovery Gen2"
  mode: queued
  max: 999
  triggers:
    - platform: mqtt
      topic: shellies_discovery/rpc
  actions:
    - action: python_script.shellies_discovery_gen2
      data:
        id: "{{ trigger.payload_json.src }}"
        device_config: "{{ trigger.payload_json.result }}"
    - condition: template
      value_template: "{{ 'mqtt' in trigger.payload_json.result }}"
    - service: mqtt.publish
      data:
        topic: "{{ trigger.payload_json.result.mqtt.topic_prefix }}/command"
        payload: "status_update"

Reboot HA, here is the full log (I also set automation to DEBUG) home-assistant.log

When I look in mqtt-explorer I see both messages successfully sent through the automation: mqtt-explorer-sent-b0b4

I also see the responses coming back to shellies_discovery/rpc image

That's where the trail stops, for some reason that response doesn't trigger.

Is it simple as needing to load the discovery script before the annonce script?

bieniu commented 4 weeks ago

You can't manually run shellies_dicovery automation. You can only run shellies_announce automation. There is nothing in the log so from my point of view the response from the device didn't reach Home Assistant.

As I said before you need to analyze shellies_discovery automation trace.

lusluslus commented 4 weeks ago

There is no trace from shellies_discovery, it doesn't trigger. I think it is because it is not loaded when the mqtt messages come back.

See at the end of the log:

[homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Running automation actions
2024-10-30 13:20:04.307 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 1 of 1 with item: 'shellyplus2pm-80646fcfb0b4'
2024-10-30 13:20:04.307 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-10-30 13:20:04.307 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
2024-10-30 13:20:04.308 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Initialized trigger Shellies Announce Gen2
2024-10-30 13:20:04.308 INFO (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Initialized trigger Shellies Discovery Gen2
2024-10-30 13:20:04.316 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service

shellies_announce_gen2 sends the messages before 2024-10-30 13:20:04.308 INFO (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Initialized trigger Shellies Discovery Gen2 is registered

bieniu commented 3 weeks ago

obraz

Add 10-20 seconds delay to announce automation.