bieniu / ha-shellies-discovery-gen2

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

Template variable error: 'dict object' has no attribute 'result' when rendering... #429

Open roofburner opened 1 month ago

roofburner commented 1 month ago

I'm getting the below kind of errors in my home-assistant.log

ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'result' when rendering '{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}'
ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'result' when rendering '{%if value_json.result.mqtt.connected%}online{%else%}offline{%endif%}'
ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'result' when rendering '{%if value_json.result.mqtt.connected%}online{%else%}offline{%endif%}'
ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'result' when rendering '{{value_json.result.sys.available_updates.get("stable",{})|to_json}}'

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:  # enter the list of device IDs (MQTT prefixes) here
      - shellyplus1-a8032ab7fbb0
      - shellyplus1-a8032abafd1c
      - shellyplus1-7c87ce58cb34
      - shellyplus1-7c87ce558428
      - shellyplus1-3c610572c824
      - shellyplus1-7c87ce56475c
      - shellyplus1-7c87ce555ac0
      - shellyplus1-7c87ce55840c
      - shellyplus1-08b61fd12ac4
      - shellyplus1-08b61fd0c2d8
  action:
    - delay: 00:00:30
    - 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:

afbeelding

Debug log:

debug.log attached

debug.log

bieniu commented 1 month ago

Show me please your device script (Scripts section in the device web panel) and the payloads that the device sends to the MQTT broker.

roofburner commented 1 month ago

TCPDump (PCAP - shelly.zip) attached. Hope this helps. Or can you explain me how to get hands on the payload? shelly.zip

This is what the script looks like:

Script name: shellies_discovery_gen2_script_20240216

Script

let topic_prefix = null;
let installed_version = null;

Shelly.call("MQTT.GetConfig", {}, function (config) {
    topic_prefix = config.topic_prefix;
});

function SendDeviceStatus() {
    let _device_info = Shelly.getDeviceInfo();
    installed_version = _device_info.ver;
    Shelly.call("Shelly.GetStatus", {}, function (status) {
        status.sys.installed_version = installed_version;
        MQTT.publish(topic_prefix + "/status/rpc", JSON.stringify(status));
    });
};

MQTT.setConnectHandler(SendDeviceStatus);
let UpdateTimer = Timer.set(1800000, true, SendDeviceStatus);
bieniu commented 1 month ago

Or can you explain me how to get hands on the payload?

I meant a screenshot from MQTT Explorer (launch MQTT Explorer, wait 10 minutes and take a screenshot).

roofburner commented 1 month ago

OK. I will provide you a screenshot asap.

bieniu commented 1 month ago

I checked the script code and it does not contain JSON paths like those in your errors:

value_json.result.sys.available_updates.stable
value_json.result.mqtt.connected

The script uses those paths:

value_json.sys.available_updates.stable
value_json.mqtt.connected

So the source of the errors is not Shellies Discovery Gen2.

bieniu commented 1 month ago

You can find the cause of errors by searching in MQTT Explorer if MQTT Discovery was used to configure the device

obraz

roofburner commented 1 month ago

This is the filtered tree view:

192.168.0.102
>homeassistant
>>binary_sensor
>>>shellyplus1-a8032ab7fbb0-firmware
>>>config = {"name": "ShellyGarage Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-a8032ab7fbb0-firmware", "qos": 0, "dev": {"cns": [["mac", "A8:03:2A:B7:FB:B0"]], "name": "ShellyGarage", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Robotics", "…
>>>shellyplus1-a8032abafd1c-firmware
>>>config = {"name": "ShellyTuinhuis Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-a8032abafd1c-firmware", "qos": 0, "dev": {"cns": [["mac", "A8:03:2A:BA:FD:1C"]], "name": "ShellyTuinhuis", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Robotics…
>>>shellyplus1-7c87ce58cb34-firmware
>>>config = {"name": "ShellyBadkamerDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-7c87ce58cb34-firmware", "qos": 0, "dev": {"cns": [["mac", "7C:87:CE:58:CB:34"]], "name": "ShellyBadkamerDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Ro…
>>>shellyplus1-7c87ce558428-firmware
>>>config = {"name": "ShellyDaanDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-7c87ce558428-firmware", "qos": 0, "dev": {"cns": [["mac", "7C:87:CE:55:84:28"]], "name": "ShellyDaanDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Robotics",…
>>>shellyplus1-3c610572c824-firmware
>>>config = {"name": "ShellyRienDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-3c610572c824-firmware", "qos": 0, "dev": {"cns": [["mac", "3C:61:05:72:C8:24"]], "name": "ShellyRienDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Robotics",…
>>>shellyplus1-7c87ce555ac0-firmware
>>>config = {"name": "ShellyZithoekRDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-7c87ce555ac0-firmware", "qos": 0, "dev": {"cns": [["mac", "7C:87:CE:55:5A:C0"]], "name": "ShellyZithoekRDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Ro…
>>>shellyplus1-7c87ce56475c-firmware
>>>config = {"name": "ShellyZithoekLDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-7c87ce56475c-firmware", "qos": 0, "dev": {"cns": [["mac", "7C:87:CE:56:47:5C"]], "name": "ShellyZithoekLDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Ro…
>>>shellyplus1-7c87ce55840c-firmware
>>>config = {"name": "ShellyPoort Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-7c87ce55840c-firmware", "qos": 0, "dev": {"cns": [["mac", "7C:87:CE:55:84:0C"]], "name": "ShellyPoort", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Robotics", "cu…
>>>shellyplus1-08b61fd12ac4-firmware
>>>config = {"name": "ShellyOntbijthoekDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-08b61fd12ac4-firmware", "qos": 0, "dev": {"cns": [["mac", "08:B6:1F:D1:2A:C4"]], "name": "ShellyOntbijthoekDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allte…
>>>shellyplus1-08b61fd0c2d8-firmware
>>>config = {"name": "ShellyLivingDBE Firmware", "val_tpl": "{%if value_json.result.sys.available_updates.stable is defined%}ON{%else%}OFF{%endif%}", "en": "true", "uniq_id": "shellyplus1-08b61fd0c2d8-firmware", "qos": 0, "dev": {"cns": [["mac", "08:B6:1F:D0:C2:D8"]], "name": "ShellyLivingDBE", "mdl": "Shelly Plus 1", "sw": "20240522-112837/1.3.2-g34c651b", "hw": "gen2 (SNSW-001X16EU)", "mf": "Allterco Roboti…
>>button
>>>shellyplus1-a8032ab7fbb0-update_firmware
>>>config = {"name": "ShellyGarage Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-a8032ab7fbb0\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-a8032ab7fbb0-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_templa…
>>>shellyplus1-a8032abafd1c-update_firmware
>>>config = {"name": "ShellyTuinhuis Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-a8032abafd1c\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-a8032abafd1c-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_temp…
>>>shellyplus1-7c87ce58cb34-update_firmware
>>>config = {"name": "ShellyBadkamerDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-7c87ce58cb34\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-7c87ce58cb34-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_t…
>>>shellyplus1-7c87ce558428-update_firmware
>>>config = {"name": "ShellyDaanDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-7c87ce558428\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-7c87ce558428-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_templ…
>>>shellyplus1-3c610572c824-update_firmware
>>>config = {"name": "ShellyRienDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-3c610572c824\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-3c610572c824-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_templ…
>>>shellyplus1-7c87ce555ac0-update_firmware
>>>config = {"name": "ShellyZithoekRDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-7c87ce555ac0\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-7c87ce555ac0-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_t…
>>>shellyplus1-7c87ce56475c-update_firmware
>>>config = {"name": "ShellyZithoekLDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-7c87ce56475c\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-7c87ce56475c-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_t…
>>>shellyplus1-7c87ce55840c-update_firmware
>>>config = {"name": "ShellyPoort Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-7c87ce55840c\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-7c87ce55840c-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_templat…
>>>shellyplus1-08b61fd12ac4-update_firmware
>>>config = {"name": "ShellyOntbijthoekDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-08b61fd12ac4\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-08b61fd12ac4-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "valu…
>>>shellyplus1-08b61fd0c2d8-update_firmware
>>>config = {"name": "ShellyLivingDBE Update Firmware", "cmd_t": "~rpc", "payload_press": "{\"id\":1,\"src\":\"shellyplus1-08b61fd0c2d8\",\"method\":\"Shelly.Update\",\"params\":{\"stage\":\"stable\"}}", "en": "true", "uniq_id": "shellyplus1-08b61fd0c2d8-update_firmware", "qos": 0, "avty": [{"topic": "~online", "payload_available": "true", "payload_not_available": "false"}, {"topic": "~status/rpc", "value_tem…