bieniu / ha-shellies-discovery-gen2

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

gen2 device doesn't appear, gen1 works fine #424

Closed CaptMilkbeard closed 2 months ago

CaptMilkbeard commented 2 months ago

Describe the bug

gen2 devices doesn't appear via MQTT. Settings are set as mention in ReadMe. Devices appear in MQTT Explorer. Using also your gen1 Shellies-dicovery which is working fine. Therefore i'm sure python is setup correct ;)

Expected behavior

Expecting same behaviour as known from gen1, devices appear in HA -MQTTT

Versions:

Shellies Discovery GEN2 automation:


- id: shellies_announce_gen2
  alias: Shellies Announce Gen2
  trigger:
  - platform: homeassistant
    event: start
  variables:
    device_info_payload: '{{ {''id'': 1, ''src'':''shellies_discovery'', ''method'':''Shelly.GetConfig''}
      | to_json }}'
    device_ids:
    - shellyplus2pm-485519a1ff8c
    - shellyplus1pm-84cca877155c
    - custom-prefix/shelly-kitchen
  action:
  - repeat:
      for_each: '{{ device_ids }}'
      sequence:
      - service: mqtt.publish
        data:
          topic: '{{ repeat.item }}/rpc'
          payload: '{{ device_info_payload }}'
- id: shellies_discovery_gen2
  alias: Shellies Discovery Gen2
  mode: queued
  max: 999
  trigger:
  - platform: mqtt
    topic: shellies_discovery/rpc
  action:
  - service: 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:

shelly mqttexplorer

Debug log:

logger: default: error

2024-06-15 08:19:07.761 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'payload_json' when rendering '{{ trigger.payload_json.src }}'
2024-06-15 08:19:07.764 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Shellies Discovery Gen2: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'
2024-06-15 08:19:07.764 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Error while executing automation automation.shellies_discovery_gen2: Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'

logs: homeassistant.components.python_script: debug


2024-06-15 08:00:01.238 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] {'topic': 'homeassistant/sensor/shelly1l-3C6105E38291-ext-temperature-1/config', 'payload': '', 'retain': True, 'qos': 0}
2024-06-15 08:00:01.238 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: homeassistant/sensor/shelly1l-3C6105E38291-ext-temperature-1/config 
2024-06-15 08:00:01.239 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: event shelly1l-3C6105E38291-input-1, sending update from external application Shellies Discovery, version: 5.0.0, support URL: https://github.com/bieniu/ha-shellies-discovery
2024-06-15 08:00:01.240 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] {'topic': 'homeassistant/sensor/shelly1l-3C6105E38291-ext-temperature-2/config', 'payload': '', 'retain': True, 'qos': 0}
2024-06-15 08:00:01.240 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: homeassistant/sensor/shelly1l-3C6105E38291-ext-temperature-2/config 
2024-06-15 08:00:01.241 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] {'topic': 'homeassistant/sensor/shelly1l-3C6105E38291-ext-humidity-0/config', 'payload': '', 'retain': True, 'qos': 0}
2024-06-15 08:00:01.241 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: homeassistant/sensor/shelly1l-3C6105E38291-ext-humidity-0/config 
2024-06-15 08:00:01.242 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] {'topic': 'homeassistant/binary_sensor/shelly1l-3C6105E38291-input-0/config', 'payload': '{"name": "Input 0", "stat_t": "~input/0", "en": "false", "uniq_id": "shelly1l-3c6105e38291-input-0", "qos": 0, "dev": {"cns": [["mac", "3c:61:05:e3:82:91"]], "name": "Shelly 1L 3C6105E38291", "mdl": "Shelly 1L", "sw": "20230913-112054/v1.14.0-gcb84623", "hw": "gen1 (SHSW-L)", "mf": "Allterco Robotics", "cu": "http://192.168.11.27/"}, "o": {"name": "Shellies Discovery", "sw": "5.0.0", "url": "https://github.com/bieniu/ha-shellies-discovery"}, "~": "shellies/shelly1l-3C6105E38291/", "pl_on": "1", "pl_off": "0", "avty": [{"t": "~online", "pl_avail": "true", "pl_not_avail": "false"}, {"t": "~info", "val_tpl": "{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}"}]}', 'retain': True, 'qos': 0}
2024-06-15 08:00:01.242 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: homeassistant/binary_sensor/shelly1l-3C6105E38291-input-0/config {"name": "Input 0", "stat_t": "~input/0", "en": "false", "uniq_id": "shelly1l-3c6105e38291-input-0", "qos": 0, "dev": {"cns": [["mac", "3c:61:05:e3:82:91"]], "name": "Shelly 1L 3C6105E38291", "mdl": "Shelly 1L", "sw": "20230913-112054/v1.14.0-gcb84623", "hw": "gen1 (SHSW-L)", "mf": "Allterco Robotics", "cu": "http://192.168.11.27/"}, "o": {"name": "Shellies Discovery", "sw": "5.0.0", "url": "https://github.com/bieniu/ha-shellies-discovery"}, "~": "shellies/shelly1l-3C6105E38291/", "pl_on": "1", "pl_off": "0", "avty": [{"t": "~online", "pl_avail": "true", "pl_not_avail": "false"}, {"t": "~info", "val_tpl": "{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}"}]}
2024-06-15 08:00:01.243 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] {'topic': 'homeassistant/binary_sensor/shelly1l-3C6105E38291-input-1/config', 'payload': '{"name": "Input 1", "stat_t": "~input/1", "en": "false", "uniq_id": "shelly1l-3c6105e38291-input-1", "qos": 0, "dev": {"cns": [["mac", "3c:61:05:e3:82:91"]], "name": "Shelly 1L 3C6105E38291", "mdl": "Shelly 1L", "sw": "20230913-112054/v1.14.0-gcb84623", "hw": "gen1 (SHSW-L)", "mf": "Allterco Robotics", "cu": "http://192.168.11.27/"}, "o": {"name": "Shellies Discovery", "sw": "5.0.0", "url": "https://github.com/bieniu/ha-shellies-discovery"}, "~": "shellies/shelly1l-3C6105E38291/", "pl_on": "1", "pl_off": "0", "avty": [{"t": "~online", "pl_avail": "true", "pl_not_avail": "false"}, {"t": "~info", "val_tpl": "{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}"}]}', 'retain': True, 'qos': 0}
2024-06-15 08:00:01.243 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: homeassistant/binary_sensor/shelly1l-3C6105E38291-input-1/config {"name": "Input 1", "stat_t": "~input/1", "en": "false", "uniq_id": "shelly1l-3c6105e38291-input-1", "qos": 0, "dev": {"cns": [["mac", "3c:61:05:e3:82:91"]], "name": "Shelly 1L 3C6105E38291", "mdl": "Shelly 1L", "sw": "20230913-112054/v1.14.0-gcb84623", "hw": "gen1 (SHSW-L)", "mf": "Allterco Robotics", "cu": "http://192.168.11.27/"}, "o": {"name": "Shellies Discovery", "sw": "5.0.0", "url": "https://github.com/bieniu/ha-shellies-discovery"}, "~": "shellies/shelly1l-3C6105E38291/", "pl_on": "1", "pl_off": "0", "avty": [{"t": "~online", "pl_avail": "true", "pl_not_avail": "false"}, {"t": "~info", "val_tpl": "{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}"}]}
2024-06-15 08:00:01.244 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] {'topic': 'homeassistant/binary_sensor/shelly1l-3C6105E38291-overtemperature/config', 'payload': '{"name": "Overtemperature", "stat_t": "~overtemperature", "en": "true", "uniq_id": "shelly1l-3c6105e38291-overtemperature", "qos": 0, "dev": {"cns": [["mac", "3c:61:05:e3:82:91"]], "name": "Shelly 1L 3C6105E38291", "mdl": "Shelly 1L", "sw": "20230913-112054/v1.14.0-gcb84623", "hw": "gen1 (SHSW-L)", "mf": "Allterco Robotics", "cu": "http://192.168.11.27/"}, "o": {"name": "Shellies Discovery", "sw": "5.0.0", "url": "https://github.com/bieniu/ha-shellies-discovery"}, "~": "shellies/shelly1l-3C6105E38291/", "ent_cat": "diagnostic", "pl_on": "1", "pl_off": "0", "avty": [{"t": "~online", "pl_avail": "true", "pl_not_avail": "false"}, {"t": "~info", "val_tpl": "{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}"}], "dev_cla": "problem"}', 'retain': True, 'qos': 0}
2024-06-15 08:00:01.244 DEBUG (SyncWorker_5) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: homeassistant/binary_sensor/shelly1l-3C6105E38291-overtemperature/config {"name": "Overtemperature", "stat_t": "~overtemperature", "en": "true", "uniq_id": "shelly1l-3c6105e38291-overtemperature", "qos": 0, "dev": {"cns": [["mac", "3c:61:05:e3:82:91"]], "name": "Shelly 1L 3C6105E38291", "mdl": "Shelly 1L", "sw": "20230913-112054/v1.14.0-gcb84623", "hw": "gen1 (SHSW-L)", "mf": "Allterco Robotics", "cu": "http://192.168.11.27/"}, "o": {"name": "Shellies Discovery", "sw": "5.0.0", "url": "https://github.com/bieniu/ha-shellies-discovery"}, "~": "shellies/shelly1l-3C6105E38291/", "ent_cat": "diagnostic", "pl_on": "1", "pl_off": "0", "avty": [{"t": "~online", "pl_avail": "true", "pl_not_avail": "false"}, {"t": "~info", "val_tpl": "{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}"}], "dev_cla": "problem"}
2024-06-15 08:00:01.244 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shelly1l-3C6105E38291-input-0 from external application Shellies Discovery, version: 5.0.0, support URL: https://github.com/bieniu/ha-shellies-discovery
2024-06-15 08:00:01.245 DEBUG (SyncWorker_5) [homeassistant.components.python_script] Output of python_script: `shellies_discovery.py`:

homeassistant.components.automation: info


2024-06-15 08:19:05.236 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Running automation actions
2024-06-15 08:19:05.236 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 1 of 3 with item: 'shellyplus2pm-485519a1ff8c'
2024-06-15 08:19:05.236 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-06-15 08:19:05.236 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
2024-06-15 08:19:05.238 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 2 of 3 with item: 'shellyplus1pm-84cca877155c'
2024-06-15 08:19:05.238 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-06-15 08:19:05.238 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
2024-06-15 08:19:05.241 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 3 of 3 with item: 'custom-prefix/shelly-kitchen'
2024-06-15 08:19:05.241 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-06-15 08:19:05.242 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
2024-06-15 08:19:07.760 INFO (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Shellies Discovery Gen2: Running automation actions
2024-06-15 08:19:07.760 INFO (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Shellies Discovery Gen2: Executing step call service
2024-06-15 08:19:07.761 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'payload_json' when rendering '{{ trigger.payload_json.src }}'
2024-06-15 08:19:07.764 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Shellies Discovery Gen2: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'
2024-06-15 08:19:07.764 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Error while executing automation automation.shellies_discovery_gen2: Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'

homeassistant.components.mqtt.discovery: info


2024-06-15 08:00:01.237 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shelly1l-3C6105E38291-input-1 button_short_press, sending update
2024-06-15 08:00:01.239 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: event shelly1l-3C6105E38291-input-1, sending update from external application Shellies Discovery, version: 5.0.0, support URL: https://github.com/bieniu/ha-shellies-discovery
2024-06-15 08:00:01.244 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shelly1l-3C6105E38291-input-0 from external application Shellies Discovery, version: 5.0.0, support URL: https://github.com/bieniu/ha-shellies-discovery
2024-06-15 08:00:01.245 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shelly1l-3C6105E38291-input-1 from external application Shellies Discovery, version: 5.0.0, support URL: https://github.com/bieniu/ha-shellies-discovery
2024-06-15 08:00:01.286 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: binary_sensor shelly1l-3C6105E38291-overtemperature, sending update from external application Shellies Discovery, version: 5.0.0, support URL: https://github.com/bieniu/ha-shellies-discovery
bieniu commented 2 months ago

Why are you showing the content of the log file for Shellies Discovery Gen1 when you have a problem with Shellies Discovery Gen2?

Device IDs look wrong: obraz

2024-06-15 08:19:07.761 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'payload_json' when rendering '{{ trigger.payload_json.src }}'

Did you run discovery automation manually?

You didn't attach the log file, so I can only guess that the devices didn't respond to the announce, so I don't really have anything to analyze.

CaptMilkbeard commented 2 months ago

Since I cannot judge which message belongs to which version, I took the necessary parts from the log that corresponded to the filter. These are the messages of the last hour before my post.

the IDs are from the config itself, I just added an ID (MQTT prefix) of my device when I tested if this helps to recognise it (it didn't).

full logfile is now attached home-assistant.log

bieniu commented 2 months ago

the IDs are from the config itself

shellyplus2pm-485519a1ff8c comes from the example, I doubt your device uses this ID.

Nothing interesting in the log 🤷‍♂️

CaptMilkbeard commented 2 months ago

Yes, sorry, I wasn't precise enough. I meant the config from Github. I have included the examples to see if I get any reaction or at least an error that a device is not found. but there is just nothing.

nothing interesting also means you have no idea why it doesn't work?

bieniu commented 2 months ago

Yea, I have an idea, there is no response from Shelly device. Why's that? I don't know, your configuration has to be wrong. You can set the log level to debug for MQTT component. Maybe this will help.

CaptMilkbeard commented 2 months ago

attached is the new log, I hope you see something helpful. I don't want to rule out that I've done something wrong, I just don't see it.

home-assistant (1).log

bieniu commented 2 months ago

Your automation is still using non-existent IDs, you still can't see if MQTT topics are being sent and the real device is still not responding...

2024-06-16 16:33:28.466 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 1 of 3 with item: 'shellyplus2pm-485519a1ff8c'
2024-06-16 16:33:28.466 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-06-16 16:33:28.466 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
2024-06-16 16:33:28.467 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Initialized trigger Shellies Announce Gen2
2024-06-16 16:33:28.467 INFO (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Initialized trigger Shellies Discovery Gen2
2024-06-16 16:33:28.468 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Shellies Announce: Running automation actions
2024-06-16 16:33:28.468 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Shellies Announce: Executing step call service
2024-06-16 16:33:28.468 INFO (MainThread) [homeassistant.components.automation.shellies_announce] Initialized trigger Shellies Announce
2024-06-16 16:33:28.469 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Initialized trigger Shellies Discovery
2024-06-16 16:33:28.487 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 2 of 3 with item: 'shellyplus1pm-84cca877155c'
2024-06-16 16:33:28.487 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-06-16 16:33:28.487 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
2024-06-16 16:33:28.496 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 3 of 3 with item: 'custom-prefix/shelly-kitchen'
2024-06-16 16:33:28.496 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2024-06-16 16:33:28.497 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service
CaptMilkbeard commented 2 months ago

one unit appears now in mqtt explorer

mqttexplorer2

i added 3 ID of my units and removed your examples now

home-assistant (2).log

bieniu commented 2 months ago

announce automation send correct payload to correct topics but there is no response from Shelly devices.

2024-06-17 10:02:46.530 DEBUG (MainThread) [homeassistant.components.mqtt.client] Transmitting message on shellyplus1pm-441793d483b4/rpc: '{"id":1,"src":"shellies_discovery","method":"Shelly.GetConfig"}', mid: 5, qos: 0
2024-06-17 10:02:46.540 DEBUG (MainThread) [homeassistant.components.mqtt.client] Transmitting message on shellyplus1-4855199c8b20/rpc: '{"id":1,"src":"shellies_discovery","method":"Shelly.GetConfig"}', mid: 6, qos: 0

Run Shellies Announce Gen2 automation manually and show me the log.

CaptMilkbeard commented 2 months ago

log attached with manuall announce

Further, one of the three units appeared now (shellyplus1pm-84cca877155c). home-assistant (3).log

bieniu commented 2 months ago

It looks like your HA triggers the announce automation when MQTT is not fully available. You need to add a delay.

CaptMilkbeard commented 2 months ago

I have tested it with a time pattern like the Gen1, but currently only 2 out of 3 PM devices appear and the Plus devices do not appear at all.

in mqtt explorer they all appear, so it still seems to be a problem in HA. but these Gen2 devices do not appear in shellies like the Gen1 but as individual devices. But you can already see this in the screenshots before.

bieniu commented 2 months ago

I can see in the log that the device has been correctly recognized and configured. There were no errors. From my point of view, there is nothing I can fix in this case. The problem is due to configuration or is specific to your HA instance. The configuration process sends a GetConfig request to the device, the device responds to the shellies_discovery topic and then the real configuration occurs. For some reason, your devices don't respond or the answer doesn't reach HA.

CaptMilkbeard commented 2 months ago

that's at least one approach I should look for. I'll try resetting one of the shells and see if that changes anything. The HA system was actually reinstalled 2 months ago, so everything should be fine.

I'm surprised that at least some devices are now being displayed, which rules out configuration problems in the addon, Python, config and so on.

Thanks so far. If I find out anything else I would share it.