Shellies Discovery Script

ok, I am gonna update the test device and check it out :slight_smile:

Hi all!
Yesterday I got 2 “Shelly 1” and 2 “Shelly i3” to integrate them to hass…
I started with shelly 1 without success :frowning: even doing what is detailed in the github page.

My versions are:
Home Assistant 0.114.3
Shellies Discovery: 0.27.6
Shelly 1 version: 20200812-090904/v1.8.0@8acf41b0

My home assistant code:

# configuration.yaml file
python_script:

# automations.yaml file
- id: shellies_announce
  alias: 'Shellies Announce'
  trigger:
    - platform: homeassistant
      event: start
    - platform: time_pattern
      hours: "/1"
  action:
    service: mqtt.publish
    data:
      topic: shellies/command
      payload: announce

- id: shellies_discovery
  alias: 'Shellies Discovery'
  mode: queued
  max: 999
  trigger:
    platform: mqtt
    topic: shellies/announce
  action:
    service: python_script.shellies_discovery
    data_template:
      discovery_prefix: 'hass'
      qos: 0
      shelly1-XXXXXXXXXXXX:
        relay-0: "light"
        force_update_sensors: true

When the python script is executed, I get this error:

Log Details (ERROR)
Logger: homeassistant.components.python_script.shellies_discovery.py
Source: components/python_script/__init__.py:205
Integration: Python Scripts (documentation, issues)
First occurred: 10:26:40 AM (6 occurrences)
Last logged: 11:46:16 AM

Error executing script: 'NoneType' object is not callable
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 205, in execute
    exec(compiled.code, restricted_globals)
  File "shellies_discovery.py", line 341, in <module>
TypeError: 'NoneType' object is not callable

Do you have any idea how to fix it?

Thanks!

Show me debug log for python_script component.

Your Shellies Discovery automation is invalid!
Where are these lines?

id: '{{ trigger.payload_json.id }}'
mac: '{{ trigger.payload_json.mac }}'
fw_ver: '{{ trigger.payload_json.fw_ver }}'

You are right,

now, with this changes I get this error:

Log Details (ERROR)
Logger: homeassistant.components.automation.shellies_discovery
Source: helpers/service.py:135
Integration: Automation (documentation, issues)
First occurred: 12:28:09 PM (2 occurrences)
Last logged: 12:28:09 PM

Shellies Discovery: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
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: '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 <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: '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

Did you read the documentation? You can’t run the script manually. You can only run announce automation or restart HA.

I have restarted HA twice and no error appears anymore, thanks :slight_smile:

now, I am not sure what to do. do you have any example that which are the next configuration to see the device in HA?

Sorry for my silly questions

If script works devices should be in Configuration -> Integrations -> MQTT.

Now it works! many thanks for your help and work!

Hello Bieniu,
I’ve upgraded my Shelly 1 (3 pieces) to 1.8.0 and installed new version of your script.
I’ve got two issues:

  1. After restart new sensors (of RSSI, Uptime, and SSID) appeared and showed the values correctly, but… now (after few hours) they are unavailable. It has repeated at least twice.
  2. Also in one case, the Uptime sensor for one of the Shellies was showing something about over 1000 days.

These issues must be obvious bugs either in software or in my config. Can you help or recommend something?

Pictures attached:


You have to update your announce automation. Look at the documentation.

What is the state of the sensor?

Hallo Bieniu,

since upgrade my HA to 114.3 and your integration, I have found the following message in the log. Is that disturbing, or what can I do?

Thanks and regards
Frank

Protokolldetails ( ERROR )

Logger: homeassistant.util.logging
Source: util/logging.py:108
First occurred: 15:39:16 (79 occurrences)
Last logged: 16:18:12

Exception in message_received when handling msg on ‘shellies/shellygas-50029178B649/sensor/concentration’: ‘0’ Traceback (most recent call last): File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 35, in wrapper msg_callback(msg) File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 169, in message_received payload = template.async_render_with_possible_json_value( File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 285, in async_render_with_possible_json_value return self._compiled.render(variables).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 TypeError: ‘<=’ not supported between instances of ‘int’ and ‘str’

Please show me full error traceback.

Could you check if the value of the topic shellies/shellygas-50029178B649/sensor/concentration is an integer or a string? You can use MQTT-Explorer for this.

Indeed…
I corrected config and after restart it’s better now - all uptimes are in correct formats - they were presented as in days (as “0d”) previously now its like “3 hours”
I always read “release notes” and I noticed that there was “breaking change” announced, but maybe you could also indicate in “release note” when config change is required (just a recommendation ;))

Regarding strange uptime reading (over “1000 days”) the sensor was obviously in “available” state but now I think this might be related to the incorrect format of uptime sensor which was rendered when config was not updated.

Anyway thanks for quick recommendation, I’ll of course let you know if anything strage happens.

That was my plan but I forgot about it.

That is from the log file direct.

2020-08-24 16:38:42 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on ‘shellies/shellygas-50029178B649/sensor/concentration’: ‘0’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 35, in wrapper
msg_callback(msg)
File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 169, in message_received
payload = template.async_render_with_possible_json_value(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 285, in async_render_with_possible_json_value
return self._compiled.render(variables).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
TypeError: ‘<=’ not supported between instances of ‘int’ and ‘str’

2020-08-24 16:39:12 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on ‘shellies/shellygas-50029178B649/sensor/concentration’: ‘0’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 35, in wrapper
msg_callback(msg)
File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 169, in message_received
payload = template.async_render_with_possible_json_value(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 285, in async_render_with_possible_json_value
return self._compiled.render(variables).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
TypeError: ‘<=’ not supported between instances of ‘int’ and ‘str’

2020-08-24 16:39:42 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on ‘shellies/shellygas-50029178B649/sensor/concentration’: ‘0’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 35, in wrapper
msg_callback(msg)
File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 169, in message_received
payload = template.async_render_with_possible_json_value(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 285, in async_render_with_possible_json_value
return self._compiled.render(variables).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
TypeError: ‘<=’ not supported between instances of ‘int’ and ‘str’

I hope this can help! :slight_smile:

Please check this version https://github.com/bieniu/ha-shellies-discovery/releases/tag/0.27.9b1