Shellies Discovery Script

After updating to 0.27.3, I’m having problems with my Shelly Dimmer 2 devices

My version numbers:
Shellies Discovery: 0.27.3
Home Assistant: 0.114.2
Shelly Dimmer2 FW: 20200812-092210/v1.8.0@8acf41b0

The dimmers are reporting their status correctly if controlled using the physical switch or through their http interface, but can’t be controlled in HA. I have reverted back to 0.27.2, and this is working correctly.

I think the issue appears to be single vs double quotes in the MQTT payload.

0.27.2 (Working)

Message 1 received on shellies/shellydimmer2-E09806950DF1/light/0 at 10:54 AM:

on

Message 0 received on shellies/shellydimmer2-E09806950DF1/light/0/set at 10:54 AM:

{
    "turn": "on"
}

0.27.3 (Not working)

Message 0 received on shellies/shellydimmer2-E09806950DF1/light/0/set at 10:59 AM:

{'turn':'on'}

Hello and thank you for this script, and the community for the added knowledge - it made my life a lot easier!
I have a small problem though. I have a Shelly DUO that - while reports the status/states correctly if I set it in the devices webgui - it won’t work the other way around - so if I try to set it from hass. FW is 1.8.0. And the script is the latest. Did anyone experience something similiar?

For the record: I also have a Shelly 1, and that is behaving correctly with the same setup.

Use version 0.27.2 of the script, version 0.27.3 has an issue for lights.

Fixed via https://github.com/bieniu/ha-shellies-discovery/releases/tag/0.27.4

1 Like

Hi, I tried to add https://github.com/bieniu/ha-shellies-discovery as a custom repository to HACS, but HACS throws error that the repository structure for 0.27.4 is not compliant.

This script isn’t a HACS custom repository. Please, please read the documentation.

I can’t even understand how you fixed it so fast, but a HUGE thank You!!!:))

1 Like

Hi , first of all thank you very much for your work.
I am having problem with your script . Running it from the console I receive this error in log:

[homeassistant.components.python_script.shellies_discovery.py] Error executing script: ‘NoneType’ object is not callable

Pyton hello world works, I installed your script through hacs and then added in configuration.yaml

python_script:

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

Thank you
Andrea

You can’t run this script manually. You have to add automations.

thank you for your prompt replay .
This is the log after running the automation and no Shelly discovered

2020-08-19 13:55:31 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Executing Shellies Discovery
2020-08-19 13:55:31 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Shellies Discovery: Running script
2020-08-19 13:55:31 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Shellies Discovery: Executing step call service
2020-08-19 13:55:31 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery] Shellies Discovery: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘trigger’ is undefined
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 "
File “/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py”, line 407, in getattr
value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: ‘trigger’ is undefined
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
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: ‘trigger’ is undefined
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: ‘trigger’ is undefined

You can’t run Shellies Discovery automation manually.

I am so dumb I executed discovery instead of annunce :slight_smile:
If I run announce I receive no error . But no shelly discovered.
I should find the new shelly device in mqtt integration right?

discovery enabled in configuration.yaml
same prefix homeassitant
Shely firmware 1.8
HA 0.114.2
Mqtt configurated in shelly option with right name and user password.
Clean Session Retain enabled
Standard mqtt prefix

In mosquitto log I find the new device connected

1597842326: New client connected from 192.168.0.204 as shelly1-68B057 (p2, c1, k60, u’mqttuser’).
1597842391: New connection from 192.168.0.48 on port 1883.

And I can browse all mqtt data in mqttclient.

What I am missing?

Search for homeassistant.components.python_script.shellies_discovery.py in HA log.

You should see something like this:

2020-08-19 15:35:00 DEBUG (SyncWorker_15) [homeassistant.components.python_script.shellies_discovery.py] dev_id: shellyrgbw2-99233F5AA, mac: 99233f5aa, fw_ver: 20190821-094851/v1.8.0@4148d2b7
2020-08-19 15:35:00 DEBUG (SyncWorker_15) [homeassistant.components.python_script.shellies_discovery.py] Sending to MQTT broker: qwerty/sensor/shellyrgbw2-99233F5AA-rssi/config {"name": "Shelly RGBW2 99233F5AA RSSI", "stat_t": "~info", "exp_aft": 43200, "frc_upd": "False", "uniq_id": "shellyrgbw2-99233f5aa-rssi", "qos": 1, "dev": {"ids": ["99233f5aa"], "name": "Shelly RGBW2 99233F5AA", "mdl": "Shelly RGBW2", "sw": "20190821-094851/v1.8.0@4148d2b7", "mf": "Allterco Robotics"}, "~": "shellies/shellyrgbw2-99233F5AA/", "unit_of_meas": "dB", "dev_cla": "signal_strength", "avty_t": "~online", "pl_avail": "true", "pl_not_avail": "false", "val_tpl": "{{value_json['wifi_sta'].rssi}}"}

thank you very much for the time you are spending trying to help me.
this is what I found on home-assitant.log

2020-08-19 16:00:00 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Shellies Discovery: Running script
2020-08-19 16:00:00 INFO (MainThread) [homeassistant.components.automation.shellies_discovery] Shellies Discovery: Executing step call service
2020-08-19 16:00:00 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery] 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’
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 “”, 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’

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

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 430, in async_trigger
await self.action_script.async_run(variables, trigger_context)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 802, in async_run
await asyncio.shield(run.async_run())
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 563, in async_run
await super().async_run()
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 183, in async_run
await self._async_step(log_exceptions=False)
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’

But what did you do? This error indicates that the automation did not get the correct data from Shelly.

You should start announce automation.

Any progress?

I am still breaking my mind on it.
I redo all the steps from the start.
Deleted Shelly for Hass
Reinstalled the script trough HACS

this is my configuration.yaml

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

then added this on 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 }}'

In the shelly

enabled MQTT with right parameter clean session and retain checked MaxQoS 0
custom Mqtt prefix unflagged ( even if I should be able to change it right? )
flagged device discoverable

restarted HA
then in integration - MQTT mosquito broker - I can’t find any new discovered mqtt device but if I open mosquito log I can read that the shelly is correctly connected.

I manually launched Shellies Announce with the same results

If I run announce automation I can see this message in MQTT explorer

I deleted the HA log and restarted this is the new fresh log

2020-08-20 11:00:00 ERROR (MainThread) [homeassistant.components.automation.shellies_discovery] 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/components/automation/__init__.py", line 430, in async_trigger
    await self.action_script.async_run(variables, trigger_context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 802, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 563, in async_run
    await super().async_run()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 183, in async_run
    await self._async_step(log_exceptions=False)
  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'

                                                                                                                                 

If I search for the shelly in entity I can only find it as device tracker
I really don’t understand what I am missing :frowning:

Your Shelly’s name is shelly1-TEST. Custom names and topics ane not supported.

Your device has unofficial firmware version also.

The shelly is brand new. I connected it right out of the box just to be sure there aren’t other variables.
I revert it back to default shelly name , unflagged Custom MQTT prefix " and still nothing :frowning_face:

the firmware page say

The current Firmware version of your Shelly device is 20191217-140757/1047-long-id-for-shelly-devices@a0661583 New version 20200812-090904/v1.8.0@8acf41b0 is available.

I am waiting to update in case I will have to revert to shelly for has that is still not compatible with 1.8 version .

Thank you for your effort !

Current version of the script works only with Shelly firmware 1.8.0 or never.
You can use version 0.26.3, it should work with old firmware.