bieniu / ha-shellies-discovery-gen2

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

Shelly Pro2 unknown state until turned on #69

Closed davidw3591 closed 2 years ago

davidw3591 commented 2 years ago

Describe the bug I have a Shelly Pro4PM and a Shelly Pro2 in my house. After a HomeAssistant reboot the Shelly Pro2 entities are unknown while the Pro4PM are updated.

The light entities of the Pro2 have the state 'unavailable' under /developer-tools/state. But they are available.

Schermafbeelding 2022-06-23 165959 PNG

When I click the "off lightning bolt" in HASS, it does nothing. When clicking the "on lightning bolt" the light turns on and HASS shows a toggle button instead of the lightning bolts. Then everything works normal.

When turning the lights on in the Shelly device UI, the lightning bolts also changes to a toggle button with the right feedback.

Expected behavior Shelly Pro2 entity state gets updated after Shelly Discovery script has run.

Versions:

Shellies Discovery GEN2 automation:

##################################################
## Automation
##################################################
automation:
  - alias: "Shelly Discovery Gen 2 - Announce"
    trigger:
      - platform: homeassistant
        event: start
    variables:
      device_info_payload: "{{ {'id': 1, 'src':'shellies_discovery', 'method':'Shelly.GetConfig'} | to_json }}"
    action:
      # Pro4PM
      - service: mqtt.publish
        data:
          topic: "shellypro4pm-84cca87e3f84/rpc"
          payload: "{{ device_info_payload }}"

      # Pro2
      - service: mqtt.publish
        data:
          topic: "shellypro2-30c6f784b9b8/rpc"
          payload: "{{ device_info_payload }}"

  ##########

  - alias: "Shelly Discovery Gen 2 - Run discovery python script"
    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 }}"

  ##########

  - alias: "Shelly Discovery Gen 2 - Status"
    trigger:
      - platform: time_pattern
        minutes: "/15"

      - platform: homeassistant
        event: start
    action:
      # Pro4PM
      - service: mqtt.publish
        data:
          topic: shellypro4pm-84cca87e3f84/rpc
          payload: "{{ {'id': 1, 'src':'shellypro4pm-84cca87e3f84/status', 'method':'Shelly.GetStatus'} | to_json }}"

      # Pro2
      - service: mqtt.publish
        data:
          topic: shellypro2-30c6f784b9b8/rpc
          payload: "{{ {'id': 1, 'src':'shellypro2-30c6f784b9b8/status', 'method':'Shelly.GetStatus'} | to_json }}"

Debug log: hass_log.txt

bieniu commented 2 years ago

It's completny normal behavior of MQTT entities restored from registry after HA start. Correct state will show up after first state update on topics shellypro2-30c6f784b9b8/status/switch:0 and shellypro2-30c6f784b9b8/status/switch:1. The device sends those topics every minute.

davidw3591 commented 2 years ago

Hi, I've been watching the MQTT topics with MQTT.fx and saw that the Pro2 is not sending periodical updates. The Pro4PM is updating every minute.

The Pro2 is only updating when the light is toggled.

Any thoughts on how to turn on periodical updates every minute on the Pro2? Can't find related settings in the Shelly UI.

bieniu commented 2 years ago

Did you check MQTT configuration? Screenshot_20220624-225516

davidw3591 commented 2 years ago

Yes both are enabled. As far as I can tell the configs off both Pro4PM and Pro2 are identical.

bieniu commented 2 years ago

I use Pro 1PM and Plus 1PM at home and both send state every minute. Tomorrow I will check Pro 2.

bieniu commented 2 years ago

Yes, you're right, Pro 2 and Pro 1 don't send switch state every minute. I think only devices with power meterig do this. There is nothing I can do about it, we need to ask Shelly support to change this behavior in the next firmware version.

davidw3591 commented 2 years ago

Thanks for checking. I've posted this to the Shelly FB support group.

davidw3591 commented 2 years ago

I've put togheter a small script that runs on the Pro2 that updates the status of the switch topics every minute. Works fine for now, hopefully Shelly will make some adjustments to the firmware in the future.

// Based on: https://github.com/ALLTERCO/shelly-script-examples/blob/main/mqtt-announce-control.js
// The Shelly Gen2 non-PM devices don't send periodic updates to the switch topics.
// Causing an unknown state in HomeAssistant after a reboot until the output is operated.
// This scripts updates to the switch topics every minute as a workaround.

let CONFIG = {
  device_id: "",
  device_mac: "",
  device_model: "",
  fw_ver: "",
  topic_prefix: "",
  wifi_ip: "",
};

// Read MQTT topic prefix
Shelly.call("MQTT.GetConfig", null, function (config) {
  CONFIG.topic_prefix = config.topic_prefix;
});

// Announce function to send MQTT update for the switches
function updateSwitchTopics() {
 // Switch ID 0
  Shelly.call("Switch.GetStatus", { id: 0 }, function (status) {
    MQTT.publish(CONFIG.topic_prefix + "/status/switch:0", JSON.stringify(status));
    //print(CONFIG.topic_prefix + "/status/switch:0", JSON.stringify(status));
  });

  // Switch ID 1
  Shelly.call("Switch.GetStatus", { id: 1 }, function (status) {
    MQTT.publish(CONFIG.topic_prefix + "/status/switch:1", JSON.stringify(status));
    //print(CONFIG.topic_prefix + "/status/switch:1", JSON.stringify(status));
  });  
}

// Publih every minute (60000ms)
let updateTimer = Timer.set(
   60000,
   true,
   updateSwitchTopics
);