Shellies Discovery Script

sorry its been so long since i installed this script, forgot how it all fits together. fixed now, thank you for your help as always.

1 Like

Hi @Bieniu

Thanks for an awesome script. I have several shellies and have set an custom mqtt prefix for each, e.g. shelly-garage-light. How come the entity id is not that unique, and hard to search for them. I get e.g. switch.shelly_1_light_relay_0 and another shelly has switch.shelly_1_light_relay_0_2. Is there something I do wrong here? I was expecting to get more unique names for the entity id, more based on the mqtt prefix. (I figure I miss out on some configuration thing)

Hi!

It hit me that the last word was appended only to the entity id, this is because the script cuts out the text behind the last minus (-) sign. I changed the mqtt topic to shelly_garage_light and voiala it works. Don’t know if this is a bug though?

This is a script limitation. Since the default device ID is <model>-<id> and sometimes there is a - character in <model>, the script uses the part of the string after the last - as the ID.

hi @Bieniu - me again. I had to replace a faulty dimmer and move another one, the replacement may have been used in the past, im not 100% sure. I have removed the old devices and rebooted my home assistant a few times but cant seem to find this device in my configuration \ devices. i cant find D8BFC019F22B in my device list, im wondering if its used by an old device, im not sure how to find it though.

I can see these entries in my logs after i restart HA.

2021-09-23 15:00:22 INFO (SyncWorker_6) [homeassistant.components.python_script] Executing shellies_discovery.py: {'id': 'shellydimmer2-D8BFC019F22B', 'mac': 'D8BFC019F22B', 'fw_ver': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'model': 'SHDM-2', 'mode': ''}
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-temperature
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-rssi
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-ssid
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-uptime
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-ip
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shellydimmer2-D8BFC019F22B-input-0 button_release, sending update
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Got update for trigger with hash: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-0 button_release') '{'automation_type': 'trigger', 'topic': 'shellies/shellydimmer2-D8BFC019F22B/input/0', 'payload': '0', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'type': 'button_short_release', 'subtype': 'button_1', 'platform': 'mqtt'}'
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Updating trigger: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-0 button_release')
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shellydimmer2-D8BFC019F22B-input-0 button_long_press, sending update
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shellydimmer2-D8BFC019F22B-input-0 button_short_press, sending update
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shellydimmer2-D8BFC019F22B-input-1 button_release, sending update
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Got update for trigger with hash: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-0 button_long_press') '{'automation_type': 'trigger', 'topic': 'shellies/shellydimmer2-D8BFC019F22B/input_event/0', 'payload': 'L', 'value_template': '{{value_json.event}}', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'type': 'button_long_press', 'subtype': 'button_1', 'platform': 'mqtt'}'
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Updating trigger: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-0 button_long_press')
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Got update for trigger with hash: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-0 button_short_press') '{'automation_type': 'trigger', 'topic': 'shellies/shellydimmer2-D8BFC019F22B/input_event/0', 'payload': 'S', 'value_template': '{{value_json.event}}', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'type': 'button_short_press', 'subtype': 'button_1', 'platform': 'mqtt'}'
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Updating trigger: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-0 button_short_press')
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Got update for trigger with hash: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-1 button_release') '{'automation_type': 'trigger', 'topic': 'shellies/shellydimmer2-D8BFC019F22B/input/1', 'payload': '0', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'type': 'button_short_release', 'subtype': 'button_2', 'platform': 'mqtt'}'
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Updating trigger: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-1 button_release')
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shellydimmer2-D8BFC019F22B-input-1 button_long_press, sending update
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Got update for trigger with hash: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-1 button_long_press') '{'automation_type': 'trigger', 'topic': 'shellies/shellydimmer2-D8BFC019F22B/input_event/1', 'payload': 'L', 'value_template': '{{value_json.event}}', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'type': 'button_long_press', 'subtype': 'button_2', 'platform': 'mqtt'}'
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Updating trigger: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-1 button_long_press')
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: device_automation shellydimmer2-D8BFC019F22B-input-1 button_short_press, sending update
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Got update for trigger with hash: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-1 button_short_press') '{'automation_type': 'trigger', 'topic': 'shellies/shellydimmer2-D8BFC019F22B/input_event/1', 'payload': 'S', 'value_template': '{{value_json.event}}', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'type': 'button_short_press', 'subtype': 'button_2', 'platform': 'mqtt'}'
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.device_trigger] Updating trigger: ('device_automation', 'shellydimmer2-D8BFC019F22B-input-1 button_short_press')
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-overtemperature
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-overload
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-loaderror
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-input-0
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-input-1
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-longpush-0
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-longpush-1
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-shortpush-0
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-shortpush-1
2021-09-23 15:00:25 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: binary_sensor shellydimmer2-D8BFC019F22B-firmware-update
2021-09-23 15:00:26 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: light shellydimmer2-D8BFC019F22B-0, sending update
2021-09-23 15:00:26 INFO (MainThread) [homeassistant.components.mqtt.mixins] Got update for entity with hash: ('light', 'shellydimmer2-D8BFC019F22B-0') '{'schema': 'template', 'name': 'Shelly Dimmer 2 D8BFC019F22B Light 0', 'command_topic': 'shellies/shellydimmer2-D8BFC019F22B/light/0/set', 'state_topic': 'shellies/shellydimmer2-D8BFC019F22B/light/0/status', 'availability_topic': 'shellies/shellydimmer2-D8BFC019F22B/online', 'payload_available': 'true', 'payload_not_available': 'false', 'command_on_template': '{"turn":"on"{%if brightness is defined%},"brightness":{{brightness|float|multiply(0.3922)|round}}{%endif%}{%if transition is defined%},"transition":{{min(transition|multiply(1000),4999)}}{%endif%}}', 'command_off_template': '{"turn":"off"{%if transition is defined%},"transition":{{min(transition|multiply(1000),4999)}}{%endif%}}', 'state_template': '{%if value_json.ison%}on{%else%}off{%endif%}', 'brightness_template': '{{value_json.brightness|float|multiply(2.55)|round}}', 'unique_id': 'shellydimmer2-d8bfc019f22b-light-0', 'qos': '0', 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'platform': 'mqtt'}'
2021-09-23 15:00:26 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-white-power-0
2021-09-23 15:00:26 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor shellydimmer2-D8BFC019F22B-white-energy-0
2021-09-23 15:00:26 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor shellydimmer2-D8BFC019F22B-white-overpower_value-0, sending update
2021-09-23 15:00:26 INFO (MainThread) [homeassistant.components.mqtt.mixins] Got update for entity with hash: ('sensor', 'shellydimmer2-D8BFC019F22B-white-overpower_value-0') '{'name': 'Shelly Dimmer 2 D8BFC019F22B Overpower_Value 0', 'state_topic': 'shellies/shellydimmer2-D8BFC019F22B/light/0/overpower_value', 'unit_of_measurement': 'W', 'value_template': '{{value|float|round(1)}}', 'device_class': 'power', 'availability_topic': 'shellies/shellydimmer2-D8BFC019F22B/online', 'payload_available': 'true', 'payload_not_available': 'false', 'force_update': 'False', 'unique_id': 'shellydimmer2-d8bfc019f22b-white-overpower_value-0', 'qos': 0, 'device': {'identifiers': ['d8bfc019f22b'], 'name': 'Shelly Dimmer 2 D8BFC019F22B', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'platform': 'mqtt'}'

The script has created the device. It should be in Configuration → Integrations → MQTT

Hi there,

I’ve been busy with the good tool for the last few days. I also got it to work but I don’t get an error from the LOG.

My log:

2021-09-27 18:31:44 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/zeroconf/init.py”, line 375, in _process_service_update
async_service_info = AsyncServiceInfo(service_type, name)
File “/usr/local/lib/python3.9/site-packages/zeroconf/_services/info.py”, line 124, in init
raise BadTypeInNameException
zeroconf._exceptions.BadTypeInNameException
2021-09-27 18:35:44 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/zeroconf/init.py”, line 375, in _process_service_update
async_service_info = AsyncServiceInfo(service_type, name)
File “/usr/local/lib/python3.9/site-packages/zeroconf/_services/info.py”, line 124, in init
raise BadTypeInNameException
zeroconf._exceptions.BadTypeInNameException
2021-09-27 18:38:50 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/zeroconf/init.py”, line 375, in _process_service_update
async_service_info = AsyncServiceInfo(service_type, name)
File “/usr/local/lib/python3.9/site-packages/zeroconf/_services/info.py”, line 124, in init
raise BadTypeInNameException
zeroconf._exceptions.BadTypeInNameException

My Config:

  • id: ‘shellies_discovery’
    alias: ‘Shellies Discovery’
    mode: queued
    max: 999
    trigger:
    platform: mqtt
    topic: shellies/announce
    action:
    service: python_script.shellies_discovery
    data:
    id: ‘{{ trigger.payload_json.id }}’
    mac: ‘{{ trigger.payload_json.mac }}’
    fw_ver: ‘{{ trigger.payload_json.fw_ver }}’
    model: ‘{{ trigger.payload_json.model }}’
    mode : " {{ trigger.payload_json.mode | default }} "
    discovery_prefix: ‘homeassistant’
    qos: 2
    shelly1-E8DB84D73E29:
    relay-0: “light”
    ext-temperature-0: true
    ext-temperature-1: true
    ext-temperature-2: true
    force_update_sensors: true
    shelly1-BCDDC277CCA9:
    relay-0: “fan”
    ext-temperature-0: true
    ext-temperature-1: true
    ext-temperature-2: true
    force_update_sensors: true
    shellyrgbw2-80EB0B:
    mode: “rgbw”
    shellyrgbw2-811878:
    mode: “rgbw”
    shellyswitch25-483FDA82854D:
    mode: “roller”
    position_template: “{{ ‘{% if value | float < 30 %}0{% else %}{{ value }}{% endif %}’ }}”
    set_position_template: “{{ ‘{%if position | float < 30 %}0{% else %}{{ position }}{% endif %}’ }}”
    shellyswitch25-483FDA823F96:
    mode: “roller”
    position_template: “{{ ‘{% if value | float < 30 %}0{% else %}{{ value }}{% endif %}’ }}”
    set_position_template: “{{ ‘{%if position | float < 30 %}0{% else %}{{ position }}{% endif %}’ }}”
    shellyswitch25-84CCA8B06435:
    mode: “roller”
    position_template: “{{ ‘{% if value | float < 30 %}0{% else %}{{ value }}{% endif %}’ }}”
    set_position_template: “{{ ‘{%if position | float < 30 %}0{% else %}{{ position }}{% endif %}’ }}”

Where did I go wrong.

Thanks in advance for your trouble

I apologize for my bad english

This exception is related to zeroconf component, not to python_script component.

OK then it is a coincidence that the error occurs at the same time as the setup of mqtt.
Thank you and keep up the good work.

Hello everyone,
I am a bit stuck.
I have multiple Shelly 2.5 and 1PM. All set up as MQTT devices and used to work as is. Now I reinstalled Home Assistant and am now getting the following error message even after multiple reboots:

2021-10-03 22:08:39 ERROR (SyncWorker_8) [homeassistant.components.python_script.shellies_discovery.py] Error executing script: Firmware dated 20210720 is required, please update your device shelly1pm-3C6105E5B788

Traceback (most recent call last):

File “/usr/src/homeassistant/homeassistant/components/python_script/init.py”, line 222, in execute

exec(compiled.code, restricted_globals)

File “shellies_discovery.py”, line 571, in

ValueError: Firmware dated 20210720 is required, please update your device shelly1pm-3C6105E5B788

What am I missing? I copied the minimal automations.yaml entry from github.

WebUI of my Shellies denies any availability of firmware updates.

You have to update your device. Look here http://archive.shelly-tools.de/

1 Like

Thank you, got it to work now after forcing the update.

Would it be possible to have the script get and assign the device name rather than the ID?

I have named each Shelly according to its function. That way it is easy to identify (e.g. “Fridge”). The script uses/assigns the Shelly ID, i.e. 3C3838whatever.

You have to configure custom MQTT prefix:

image

So I cannot directly use the device name like the other Shelly integration does? The device name is already defined and should be available, I think.

Device name and channel names are not available via MQTT.

I got everything up and running, or so I thought.

I can see all my Shellies and their states and data. However, I cannot trigger the switches. I see the state of the switches and I can see if their state changes, but when I try to change it from HA it does nothing.

Is there a problem maybe with current release or does anybody have an idea what could be wrong?

All other MQTT devices work. Just the Shellies are not.

Is it possible to install Shellies Discovery Script completely without HACS when i “download shellies_discovery.py file and save it in /python_scripts folder”? I do not have HACS installed and do not plan to do so.

Thank you. I have managed it without HACS.

Recently my Shelly 2.5 in roller mode are not recognized as rollers any more, i.e. the cover entity is not there. Two things have changed: I upgraded the firmware, and I keep the Shellies Discovery script up to date. Investigating further I found that the “mode” key is not any more supported. So I commented out the mode: "roller" lines in the config, butt that did not help (not even after a reboot of the HASSOS machine). My current assumption is that a change in the Shellies Discovery script is responsible that my roller mode shellies are not recognized as rollers any more. Is that correct? If so, I would be happy for a pointer to what I need to change to get it back on track.