Shellies Discovery Gen2 Script

Please open an issue here Sign in to GitHub · GitHub
with the debug log for python_script.

New issue opened, thanks!

1 Like

This is normal as the HA restores the switch state from the registry. After the first update of the relay state from the device, the normal switch will show up.

First of thank you for sharing your script with the community, the gen1 script has saved me lots of time and I very much appreciate your hard work.

I have a few Shelly 1PM+ installed in the network and I am trying to get those to work to no avail. Here is my config:

alias: Shellies Announce Gen2
trigger:

  • platform: homeassistant
    event: start
    action:
  • service: mqtt.publish
    data:
    topic: homeassistant/shellyplus1pm-fgfloodlights/rpc
    payload: ‘{{ device_info_payload }}’
  • service: mqtt.publish
    data:
    topic: homeassistant/shellyplus1pm-kitchendownlight/rpc
    payload: ‘{{ device_info_payload }}’
  • service: mqtt.publish
    data:
    topic: homeassistant/shellyplus1pm-masterlight/rpc
    payload: ‘{{ device_info_payload }}’
    variables:
    device_info_payload: >-
    {{ {‘id’: 1, ‘src’:‘shellies_discovery’, ‘method’:‘Shelly.GetConfig’} |
    to_json }}

alias: Shellies Discovery Gen2
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

alias: Shellies Status Gen2
trigger:

  • platform: time_pattern
    minutes: /15
  • platform: homeassistant
    event: start
    action:
  • service: mqtt.publish
    data:
    topic: homeassistant/shellyplus1pm-fgfloodlights/rpc
    payload: >-
    {{ {‘id’: 1, ‘src’:‘homeassistant/shellyplus1pm-fgfloodlights/status’,
    ‘method’:‘Shelly.GetStatus’} | to_json }}
  • service: mqtt.publish
    data:
    topic: homeassistant/shellyplus1pm-kitchendownlight/rpc
    payload: >-
    {{ {‘id’: 1,
    ‘src’:‘homeassistant/shellyplus1pm-kitchendownlight/status’,
    ‘method’:‘Shelly.GetStatus’} | to_json }}
  • service: mqtt.publish
    data:
    topic: homeassistant/shellyplus1pm-masterlight/rpc
    payload: >-
    {{ {‘id’: 1, ‘src’:‘homeassistant/shellyplus1pm-masterlight/status’,
    ‘method’:‘Shelly.GetStatus’} | to_json }}

I can also see the topics in mqtt-explorer:

but I am not seeing anything in my home assistant mqtt integration and im not sure why!

And where is the python_script debug log?

Please format the configuration correctly.

by python_script debug log I guess that is in the home assistant log? I found this in my log when I set the debug on the config.yaml
2022-05-05 07:29:07 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Shellies Discovery Gen2: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘payload_json’

2022-05-05 07:29:07 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery_gen2] Error while executing automation automation.shellies_discovery_gen2: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘payload_json’

Oh!! I just read the “you can’t manually run” comment…

I still dont have the devices in my MQTT integration though. I wonder whether I am just misunderstanding the way the script works and the way I should pass the topics. All my shellies are publishing to shellies/name/etc but I have set the topics in my scripts to homeassistant/name/etc thinking that these are publish commands?
I think that the script first has to read from the shellies topics and then publish to the homeassistant ones? I’m not sure… confused by MQTT tbh; probably not the right place to be posting this so sorry.

Help me help you, what can I say without the log?

Thank you I appreciate that. Here is a link to a copy of my log file, I assume that’s where the python script debug gets logged?

There is no information about Gen2 script in this log.

I have updated my automations and script to the latest version and had another go at this but its not working for me. I know im doing something wrong with my configuration but I’m unclear what.

All my shellies are configured like this:

and I have left the discovery_prefix as “homeassistant” with the following code:

  variables:
    device_info_payload: '{{ {''id'': 1, ''src'':''shellies_discovery'', ''method'':''Shelly.GetConfig''}
      | to_json }}'
    device_ids:
    - shellies/shellyplus1pm-kitchendownlight

Is this not correct? What am I missing?

I tried to change the discovery_prefix to ‘shellies’ as well tna.

Hi, should the Gen 2 shellies appear on the Integrations page after they have been discovered? The minimal setup is working correctly for in that my Gen 2 shellies are being discovered as per the events written to the “shellies_discovery” topic however unlike the Gen 1 shellies that appeared under the MQTT integration, them ones only show up under the Shelly HTTP integration. Do I need to disable something?

Additionally, what is the homeassistant prefix for? It seems to be where commands are sent to? Is the assumption that the shellies are prefixed as homeassistant/ for this MQTT topic?

Gen2 devices will appear in MQTT device list, you can ignore core configuration entries for these devices.

Additionally, what is the homeassistant prefix for?

All good. I now have my 7 Gen2 relays configured and coming up consistently. The part I was struggling with was two fold. I had changed the topic used by the some of the shellies but they where still being discovered, even after being deleted from HA. This made it hard to reason out what worked and didn’t work. I had to purge the shelly_discovery topic and delete the integrations to get back to a consistent configuration. The second issue was the naming of the channels. I had included spaces in the shelly channel names, which wasn’t an issue for the HTTP integration but did seem to be an issue for the Shelly one.

Everything is working fine now but I wouldn’t have been able to figure out what was happening without MQTT explorer.

Channel names with spaces should not be a problem. What is your issue?

Resolved by removing white space but essentially the sensors would present unknown as the value.

Hey! What is the minumum required home assistant version for gen2 script?

I am on 2021.11.5, after adding automations into yaml, I can see only shellies_discovery_gen2 in UI, shellies_announce_gen2 is not there, like it has something not supported by my outdated version.

Min HA version: 2022.11.0

1 Like

First of all, I have to thank for a good function.

The idea that I might switch to MQTT or anyway I’ll do it with special devices. Is that it should be easy to control devices that can be connected to different networks but connect to the same MQTT broker-(bridge).

Tested this with the “standard” settings on shellyplusplugs and it works fine. The problem seems to be if I want a special mqtt prefix. “cloud/shellyplusplugs-xxxxxxxxx” instead of the default “shellyplusplugs-xxxxxxxxx”.

Should this work? The reason is that I need to have an MQTT bridge and then configure that cloud/#.

Sorry for the long message but hope someone understands what I’m looking for.