bieniu/ha-shellies-discovery-gen2

KeyError: 'mqtt' on python script

Closed this issue · 6 comments

CumpsD commented

Describe the bug

After pushing the announce, my Shelly Plus H&T picks up the MQTT message and publishes the discovery info on shellies_discovery/rpc. HA picks this up and tries to exec the Python script. This seems to trigger an error however, see below for the error.

Do you have any idea what could cause it?

Expected behavior

No error

Versions:

  • Home Assistant: 2023.9.0
  • Shellies Discovery GEN2: VERSION = "2.18.0"

Shellies Discovery GEN2 automation:

- id: shellies_discovery_gen2
  alias: "[Shelly] 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 }}"

Debug log:

2023-09-08 12:44:24.852 INFO (SyncWorker_38) [homeassistant.components.python_script] Executing shellies_discovery_gen2.py: {'id': 'shellyplusht-80646fe13ccc', 'device_config': {'enable': True, 'server': 'xxx.xxx.be:1883', 'client_id': 'shellyplusht-80646fe13ccc', 'user': 'shelly', 'topic_prefix': 'shellies/shellyplusht-80646fe13ccc', 'rpc_ntf': True, 'status_ntf': True, 'use_client_cert': False, 'enable_rpc': True, 'enable_control': True}}
2023-09-08 12:44:24.853 ERROR (SyncWorker_38) [homeassistant.components.python_script.shellies_discovery_gen2.py] Error executing script: 'mqtt'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)  # noqa: S102
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "shellies_discovery_gen2.py", line 2432, in <module>
  File "/usr/local/lib/python3.11/site-packages/RestrictedPython/Eval.py", line 28, in default_guarded_getitem
    return ob[index]
           ~~^^^^^^^
KeyError: 'mqtt'
bieniu commented

The response from your device doesn't contain mqtt key. This is unusual. What firmware version does the device use? Show me please your announce automation.

CumpsD commented

Hmmm, that could be it, I recently changed it:

- id: shellies_announce_gen2
  alias: "[Shelly] Announce Gen2"

  trigger:
    - platform: homeassistant
      event: start
    - platform: time_pattern
      hours: "/6"

  action:
    - service: mqtt.publish
      data:
        retain: true
        topic: "shellies/shellyplusht-80646fe13ccc/rpc"
        payload: "{{ {'id': 21, 'src':'shellies_discovery/rpc', 'method':'MQTT.GetConfig'} | to_json }}"

It used to be Shelly.GetConfig I think, but the devices didn't respond. After some testing it turned out I had to retain the announce because the battery powered devices go into deep sleep and didn't see it

I will test again with the old method

bieniu commented

You have an answer. Wrong automation.
Remember that the devise must be awake when you run the announce automation.

CumpsD commented

I tried to run the automation again because my devices (the one above) suddenly said it was not being controlled by mqtt anymore

CumpsD commented

The original automation I used to set them up:

- id: shellies_announce_gen2
  alias: "[Shelly] Announce Gen2"

  trigger:
    - platform: homeassistant
      event: start

  variables:
    device_info_payload:  "{{ {'id': 1, 'src':'shellies_discovery', 'method':'Shelly.GetConfig'} | to_json }}"
    device_ids:
      - shellies/shellyplusht-80646fe137f0
      - shellies/shellyplusht-80646fe13ccc

  action:
  - repeat:
      for_each: "{{ device_ids }}"
      sequence:
        - service: mqtt.publish
          data:
            topic: "{{ repeat.item }}/rpc"
            payload: "{{ device_info_payload }}"

- id: shellies_discovery_gen2
  alias: "[Shelly] 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 }}"

The entities it had set up:

image

The problem I ran into:

image

This is why I tried to announce and discover again, hoping it would fix it

bieniu commented

I’m not able to help you without the debug log. If you have another problem, please open a new issue with a detailed description and a log.