Shellies Discovery Gen2 Script

The device name is missing, maybe problem with the firmware. Update firmware and/or add a device name.

Thank you. This worked. I added a custom name to the device and a name for the output.

1 Like

I appreciate your help over in the gen1 script thread, but this is a related gen2 script question so I moved it here. Fo you have any idea why some plus 1 devices would populate the input0 entity and some would not? This is what I’m seeing. 4 of the devices show a disabled input 0, and 2 do not.

Input sensors are only created if the input type is switch.

Thanks… got it. I also have an issue that the “extra” info like last restart, relay temp, wifi strength, etc. isn’t coming across MQTT on the same 2 out of 6 plus1’s. Is there another setting I’m missing for this?


Do you use automations from the last version of the script? Does the js script is correctly installed and running on every device?

I just installed it for the first time so I assume so… I could delete the script from the device and try to reprovision it I suppose?

Using the exact same process, 4 of the plus 1’s worked fine, 2 did not.

If the device was originally provisioned as a button, and then changed to a switch, does the script need to be deleted and reinstalled?

Every configuration changes are populated to the MQTT configuration after HA restart (assuming that shellies_discovery and shellies_announce automation are added correctly).

Let me know if there’s any other info I can capture to help debug. I’m not sure what else to look for. Would you just factory-reset the Plus1’s and try again at this point? Looking for ideas.

Here is the script that is contained in the device (same on 4 working and 2 not-working units):

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);

Here is the header from shellies_discovery_gen2.py:

"""This script adds MQTT discovery support for Shellies Gen2 devices."""
VERSION = "2.20.0"

Here is what is included in my automations.xml.

- 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:
    - garage/shellyplugus-c049ef895404
    - garage/shellyplus1-b8d61a87d2b0
    - garage/shellyplus1-b8d61a8c38d8
    - shop/shellyplus1-b8d61a880604
    - bar/shellyplus1-b8d61a8c3538
    - theater/shellyplus1-b8d61a87d024
    - exercise/shellyplus1-b8d61a8a9420
  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

I just ran an MQTT logger and came to realize that 6 of the 7 devices aren’t actually sending their status on a regular basis. They are very responsive to commands, and the webgui is easily acceptable (so probably not a wifi strength issue?), but are not sending any MQTT messages regularly.

Only 1 device is refreshing the shellyplus1-xxxxxxxxxxxx/status/rpc topic every 30s as expected.

My gen1 devices (dimmer2) are in the same physical location in many cases and appear to be working perfectly.

Any ideas?

Pro/Plus devices don’t send data periodically, they send data when the data changes. For non-PM devices, this means you need a js script that forces the data to be sent periodically. The script is probably disabled or not working on your devices.

Sounds like it. What triggers the script to run? Do you have any suggestions on how to debug this?

I think figured it out. If you go to the webgui scripts tab and manually hit “play”, it starts again. I assume the switch there means “autorun” but it wasn’t obvious at all. We’ll see if it keeps working.

Is there a way to set the “expire after” MQTT sensor property? I see how to do this for a gen1 custom config but not for gen2. I have an H&T sensor that only sends updates every 5 min and shows unavailable inbetween.

No. The script uses the device configuration to properly configure the entity.

Can you help me with why my H&T shows unavailable then despite getting a data packet every five minutes

Sure, open an issue on GH and attach a debug log for python script component.

Hello @Bieniu !

Thank you so much for this hard work.
I try to make GitHub - bieniu/ha-shellies-discovery-gen2: Script that adds MQTT discovery support for Shellies Gen2 devices script to work, but with no success, my shelly pro 1 isn’t detected.

What I’ve done so far :

  • install a mosquitto mqtt broker
  • setup shelly device to connect to that broker. It seems successful, its status is connected. See
  • add mqtt integration in HA and connect it to mosquitto broker. Seems to be fine too, but I’m not sure on how to perfectly chek that.
  • manually install your ha-shellies-discovery-gen2 script. Change configuration.yaml and automation.yaml as described in the doc. These is my automation.yaml file :
- 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
      - shellypro1-30c6f7813e68
  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"
  • enabled debugging log as explained in the doc. These are the logs I’ve got :
2023-10-29 10:24:57.320 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Running automation actions
2023-10-29 10:24:57.321 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeating sequence: Iteration 1 of 1 with item: 'shellypro1-30c6f7813e68'
2023-10-29 10:24:57.321 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Initialized trigger Shellies Announce Gen2
2023-10-29 10:24:57.321 INFO (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Initialized trigger Shellies Discovery Gen2
2023-10-29 10:24:57.322 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Running automation actions
2023-10-29 10:24:57.322 INFO (MainThread) [homeassistant.components.automation.shellies_announce_gen2] Shellies Announce Gen2: Repeat at step 1: Executing step call service

I’m now a bit lost. No device are detect in the HA MQTT integration and I don’t know what could I check to see what’s wrong in my setup. What could I do ?

Thank you in advance for any help !

PS : sorry for posting this message in the wrong topic the first time

This is wrong. You shouldn’t change this if you don’t need a custom MQTT prefix.