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

Issue with Shelly Pro 1 #399

Closed Spartacus68 closed 5 months ago

Spartacus68 commented 5 months ago

Describe the bug

after HA reboot, the status of the output is "unknown". After setting it manually, it is working. image

Expected behavior

Status should be "off" as it is on the Shelly Pro 1 PM image Here is also the mqtt-Explorer output:

K05
online = true
announce = {"name":"K05","id":"shellypro1-ec62608b3d04","mac":"EC62608B3D04","slot":0,"model":"SPSW-201XE16EU","gen":2,"fw_id":"20240223-142004/1.2.2-g7c39781","ver":"1.2.2","app":"Pro1","auth_en":false,"auth_domain":null}
status
rpc = {"ble":{},"cloud":{"connected":false},"eth":{"ip":null},"input:0":{"id":0,"state":false},"input:1":{"id":1,"state":false},"mqtt":{"connected":true},"script:1":{"id":1,"running":true,"mem_used":980,"mem_peak":4046,"mem_free":24220},"switch:0":{"id":0,"source":"MQTT","output":false,"temperature":{"tC":26.3,"tF":79.4}},"sys":{"mac":"EC62608B3D04","restart_required":false,"time":"18:51","unixtime":171…
switch:0 = {"id":0, "source":"MQTT", "output":false,"temperature":{"tC":26.5, "tF":79.8}}

Versions:

Shellies Discovery GEN2 automation:

description: ""
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 }}"
mode: queued
max: 999

Shelly device MQTT settings:

image

Debug log:

bieniu commented 5 months ago

This is how Pro/Plus devices without the power metering work. The workaround is a script that is installed on the device. Check if the script is installed and enabled.

Spartacus68 commented 5 months ago

Hi Maciej,

the script is running... image

image

Spartacus68 commented 5 months ago

ok, I have it you mean this script. mqtt-switch-status-announce.js

bieniu commented 5 months ago

I mean this one shellies_discovery_gen2_script_20240216. This script sends status every 30 seconds and the state of the switch will be updated.

Spartacus68 commented 5 months ago

hm, I am confused. The scriot is installes and running, but it seams to be not working correctly with the Pro 1. On my Pro 1 PM everything is fine.

And by the way:

MQTT.setConnectHandler(SendDeviceStatus);
let UpdateTimer = Timer.set(30000, true, SendDeviceStatus);

30.000ms are 5 minutes not 30s, isn´t it?

here is the script which was installed automatically:

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(30000, true, SendDeviceStatus);
bieniu commented 5 months ago

Sorry, I was wrong, the JS script is not able to force the switch states to be updated. It forces updates of the sensor states.

bieniu commented 5 months ago

Switch entities will get the correct state when you run Shellies Announce automation, check if this automation runs after HA starts, if not, you need add some delay.

Spartacus68 commented 5 months ago

Hi Maciej,

I checked it and included a delay, but it is still not working. I also triggerd the script manually and the status of the shelly is not set correctly. image

alias: Shellies Announce Gen2
description: ""
trigger:
  - platform: homeassistant
    event: start
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - repeat:
      for_each: "{{ device_ids }}"
      sequence:
        - service: mqtt.publish
          data:
            topic: "{{ repeat.item }}/rpc"
            payload: "{{ device_info_payload }}"
variables:
  device_info_payload: >-
    {{ {'id': 1, 'src':'shellies_discovery', 'method':'Shelly.GetConfig'} |
    to_json }}
  device_ids:
    - wc
    - bad
    - fynn
    - wohnen.links
    - wohnen.rechts
    - schlafen
    - buero
    - gh.spots.schrank
    - gh.deckenlicht
    - gh.fan
    - gh.halogenstrahler
    - schlafen.charger
    - E01
    - K04
    - K05
Spartacus68 commented 5 months ago

btw. this script works fine, but it is an additional script which runs in parallel. image

bieniu commented 5 months ago

but it is still not working

That means your configuration is wrong.

but it is an additional script which runs in parallel

This is your device and your script. Are you sure the device is configured by Shellies Discovery?

Spartacus68 commented 5 months ago

Hi,

ok, maybe the configuration is wrong! The Device name ist K05, the mqtt prefix is also K05. The Client ID is IR.Sender. In the Shelly announce script (see above) K05 is configured, but not the client-id. Is it wrong?

image image

Spartacus68 commented 5 months ago

but it is still not working

That means your configuration is wrong.

but it is an additional script which runs in parallel

This is your device and your script. Are you sure the device is configured by Shellies Discovery?

yes, it was automatically configured and if I delete the script from the Shelly it will be reconfigured

bieniu commented 5 months ago

Is it wrong?

I don't know, I won't analyze a configuration with two different integration methods. I checked, the script works properly for Pro 2, so it must also work for Pro 1. The problem is somewhere in your configuration and you need to find it.

Spartacus68 commented 5 months ago

ok. then it is like it is

For me everything seams ok. All Pro 1PM are working correctly. Only the Pro 1 has the issue. mqtt-Explorer shows rhe correct status of the output (false)

{
  "ble": {},
  "cloud": {
    "connected": false
  },
  "eth": {
    "ip": null
  },
  "input:0": {
    "id": 0,
    "state": false
  },
  "input:1": {
    "id": 1,
    "state": false
  },
  "mqtt": {
    "connected": true
  },
  "script:2": {
    "id": 2,
    "running": false,
    "mem_free": 24220
  },
  "script:1": {
    "id": 1,
    "running": true,
    "mem_used": 980,
    "mem_peak": 4844,
    "mem_free": 24220
  },
  "switch:0": {
    "id": 0,
    "source": "init",
    "output": false,
    "temperature": {
      "tC": 26.1,
      "tF": 79
    }
  },
  "sys": {
    "mac": "EC62608B3D04",
    "restart_required": false,
    "time": "15:42",
    "unixtime": 1712670167,
    "uptime": 1153,
    "ram_size": 258728,
    "ram_free": 112324,
    "fs_size": 524288,
    "fs_free": 192512,
    "cfg_rev": 28,
    "kvs_rev": 0,
    "schedule_rev": 0,
    "webhook_rev": 0,
    "available_updates": {},
    "reset_reason": 3,
    "installed_version": "1.2.2"
  },
  "wifi": {
    "sta_ip": "172.16.50.81",
    "status": "got ip",
    "ssid": "50gHack",
    "rssi": -68
  },
  "ws": {
    "connected": false
  }
}
Spartacus68 commented 5 months ago

Hi, I do not find the config error: MQTT-Explorer

  {
  "ble": {},
  "cloud": {
    "connected": false
  },
  "eth": {
    "ip": null
  },
  "input:0": {
    "id": 0,
    "state": false
  },
  "input:1": {
    "id": 1,
    "state": false
  },
  "mqtt": {
    "connected": true
  },
  "script:2": {
    "id": 2,
    "running": false,
    "mem_free": 24220
  },
  "script:1": {
    "id": 1,
    "running": true,
    "mem_used": 980,
    "mem_peak": 4256,
    "mem_free": 24220
  },
  "switch:0": {
    "id": 0,
    "source": "init",
    "output": false,
    "temperature": {
      "tC": 26.2,
      "tF": 79.2
    }

in HA it looks like this:

K05 IR.Sender (switch.k05_ir_sender)
MQTT discovery data:
Topic: ~status/rpc
Payload
name: IR.Sender
command_topic: K05/rpc
payload_off: >-
  {"id":1,"src":"home-assistant","method":"Switch.Set","params":{"id":0,"on":false}}
payload_on: >-
  {"id":1,"src":"home-assistant","method":"Switch.Set","params":{"id":0,"on":true}}
state_topic: K05/status/switch:0
value_template: '{%if value_json.output%}on{%else%}off{%endif%}'
state_off: 'off'
state_on: 'on'
availability:
  - topic: K05/online
    payload_available: 'true'
    payload_not_available: 'false'
  - topic: K05/status/rpc
    value_template: '{%if value_json.mqtt.connected%}online{%else%}offline{%endif%}'
unique_id: shellypro1-ec62608b3d04-0
qos: 0
device:
  connections:
    - - mac
      - EC:62:60:8B:3D:04
  name: K05
  model: Shelly Pro 1
  sw_version: 20240223-142004/1.2.2-g7c39781
  hw_version: gen2 (SPSW-001XE16EU)
  manufacturer: Allterco Robotics
  configuration_url: http://shellypro1-ec62608b3d04.local/
origin:
  name: Shellies Discovery Gen2
  sw_version: 2.28.0
  support_url: https://github.com/bieniu/ha-shellies-discovery-gen2
platform: mqtt
bieniu commented 5 months ago

Your Shellies Discovery Gen2 automation looks wrong.

Spartacus68 commented 5 months ago

Hi,

indeet! I never changed this during inital setup in April 2023. Is this what I have to check on a regular basis? I thought it will be modified automatically once an update applies.

I replaced it and now it seems to be working. But only if a delay is configured in the announceGen2. Otherwise the issue is still present.