Shellies Discovery Script

Thanks for your fast help, but I think I don’t know exactly how to use the update? Can you help me please?

move in my python_scripts folder and restart HA?

If you are using HACS, this version is available there.

Hi Bieniu, thanks for this script.
I think you have bug in your Minimal configuration sample. The trigger for shellies_discovery should be the mqtt announce topic and not the HA start or timer.

Thanks, Tomer.

Yes, I use HACS, but I can’t see the “new” Version. Is a beta setting necessary?

You have to use Reinstall option and turn on Beta Releases.

the same message. need a restart?

Run annuance automation and then restart HA.

Great! No Error messages :slight_smile: - Thank you for your fast response and help!!! :sunglasses:

1 Like

New version: 0.28.0

Changelog:

  • add support for custom names of the Shelly devices @t3hk0d3 BREAKING CHANGE

Note

The script now supports Shelly devices with non-standard names ( Internet & Security -> Advanced - developer settings -> Custom MQTT prefix in the Shelly WWW panel).
If you want to change the name of the Shelly device, you must first remove the device from Home Assistant ( Configuration -> Integrations -> MQTT -> Device -> Remove ). Otherwise, all device entities will be duplicated.

Automation change required

Please update your shellies_discovery automation!

- id: 'shellies_discovery'
  alias: 'Shellies Discovery'
  mode: queued
  max: 999
  trigger:
    platform: mqtt
    topic: shellies/announce
  action:
    service: python_script.shellies_discovery
    data_template:
      id: '{{ trigger.payload_json.id }}'
      mac: '{{ trigger.payload_json.mac }}'
      fw_ver: '{{ trigger.payload_json.fw_ver }}'
      model: '{{ trigger.payload_json.model }}'

Hi !
The new sensor wifi, rssi and uptime have the possibilty give the same name of:
relay--name,roller--name or light--name ?

tks

No, changing friendly_name for sensors is not supported. But now you can change device name.

Hi,

Having problems with the discovery script.

I can see the server in MQTT Explorer with all devises:

Logginformation (ERROR)
Logger: homeassistant.components.automation.shellies_discovery
Source: helpers/service.py:135
Integration: Automation (documentation, issues)
First occurred: 10:25:44 (2 occurrences)
Last logged: 10:25:44

Shellies Discovery: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'
While executing automation automation.shellies_discovery
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 230, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'payload_json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 132, in async_prepare_call_from_config
    template.render_complex(config[CONF_SERVICE_DATA_TEMPLATE], variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 74, in render_complex
    return {key: render_complex(item, variables) for key, item in value.items()}
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 74, in <dictcomp>
    return {key: render_complex(item, variables) for key, item in value.items()}
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 76, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 232, in async_render
    raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'dict object' has no attribute 'payload_json'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 191, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 352, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 135, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'dict object' has no attribute 'payload_json'

configuration.yaml:

python_script:

mqtt:
  broker: 192.168.1.111
  discovery: true
  discovery_prefix: hass
  username: hass
  password: !secret mqtt_password

logger:
  default: warning
  logs:
    homeassistant.components.python_script: debug
    homeassistant.components.automation: info

automations.yaml:

#Shellies Discovery Script
- id: shellies_announce
  alias: 'Shellies Announce'
  trigger:
    platform: homeassistant
    event: start
  action:
    service: mqtt.publish
    data:
      topic: shellies/command
      payload: announce

- id: 'shellies_discovery'
  alias: 'Shellies Discovery'
  mode: queued
  max: 999
  trigger:
   - platform: homeassistant
     event: start
   - platform: time_pattern
     hours: "/1"
  action:
    service: python_script.shellies_discovery
    data_template:
      id: '{{ trigger.payload_json.id }}'
      mac: '{{ trigger.payload_json.mac }}'
      fw_ver: '{{ trigger.payload_json.fw_ver }}'
      model: '{{ trigger.payload_json.model }}'

Any idé?

Your shellies_discovery automation is invalid. Look here: https://github.com/bieniu/ha-shellies-discovery#minimal-configuration

New version: 0.29.0

Changelog:

  • add support for Shelly Plug US

Hi, I am having an issue discovering my shellies.

The announce automation does works (i see the shellies being announced on mqtt) but they don’t show up in HA.

Here is my config:

mqtt.yaml

discovery: true
#discovery_prefix: homeassistant
broker: bigboy
birth_message:
  topic: 'hass/status'
  payload: 'online'
will_message:
  topic: 'hass/status'
  payload: 'offline'

automations.yaml

- id: shellies_announce
  alias: 'Shellies Announce'
  trigger:
    platform: homeassistant
    event: start
  action:
    service: mqtt.publish
    data:
      topic: shellies/command
      payload: announce

- id: 'shellies_discovery'
  alias: 'Shellies Discovery'
  mode: queued
  max: 999
  trigger:
    - platform: homeassistant
      event: start
    - platform: time_pattern
      hours: "/1"
  action:
    service: python_script.shellies_discovery
    data_template:
      id: '{{ trigger.payload_json.id }}'
      mac: '{{ trigger.payload_json.mac }}'
      fw_ver: '{{ trigger.payload_json.fw_ver }}'
      model: '{{ trigger.payload_json.model }}'

Well that was quite a silly mistake… But i still don’t get them to show up in hass-os.
The logg is clear.

But no entites after 2 reboots:

MQTT

Troubleshooting checklist: https://github.com/bieniu/ha-shellies-discovery#troubleshooting-checklist

Debug log: https://github.com/bieniu/ha-shellies-discovery#how-to-debug

Your shellies_discovery automation is invalid. Look here: https://github.com/bieniu/ha-shellies-discovery#minimal-configuration

You are the second person with the same bug. Strange day :slight_smile:

Thanks for your answer, the mistake was in the first post of this topic which you edited 1h ago.