Shellies Discovery Script

FYI, with a Shelly 2.5, when I flip the switch, it takes about 300-400 msec before it registers in Home Assistant (and triggers automations) when using the Shelly integration with CoAP. With MQTT, flipping the switch is registered virtually instantaneously.

My guess would be that’s an issue with CoAP and you can’t do anything about it, I just bring it up because that might be a motivation to get MQTT support integrated into the Shelly integration.

My auto discovered Shelly 2.5 is showing as QOS 0 (under the device > MQTT INFO). Is there a way to change this so it uses QOS 2? I had Max QoS set to 2 in the Shelly firmware before auto discovering them.

Also AWESOME script by the way, it looks like a lot of work if I would have to set these up by hand. Thanks!

You can change QoS in the script configuration GitHub - bieniu/ha-shellies-discovery: Script that adds MQTT discovery support for Shellies devices</t

In my experience, granted this was several months ago, shelly integration with coap was even slower than you (almost the order of seconds to be updated). MQTT with this script is instantaneous and works perfect.

Hello all,

I have been struggling getting MQTT to work with all the info scattered on the forums.
I have made a manual on how to install and I have tested it and it workx :slight_smile:

Check it out on my new website… Install MQTT in Home Assistant to work with Shelly devices

1 Like

Very useful, nice. Just keep in mind that you’re assuming an HA supervised installation. New users might find your guide and struggle with some steps if they chose another installation method. I think, either make the base installation very clear, or add the alternative steps/skips as necessary.

Hi Pieter,

Thanks for your reply/

This setup was done on de home-assistant.io windows Hyper-V installation.


But still you need to have some experience to go thru the process, you are right about that.
I am creating more tutorials where I will explain the alternative steps.

1 Like

Hi!
I found this thread since I cannot see the binary sensors you describe in my Shelly 1L. Using mqtt-explore, I see the input topic and the state changes when pressing the switch, so the Shelly is wired correctly. All other sensors work fine, I just cannot see the state of the attached switch.
Thanks in advance for any tips!

… and literally a minute later after writing this post, I realized that the input binary sensor is just disabled by default. All working now.

4PM Pro support when?:))

I don’t know. Currently, the firmware doesn’t allow the device to be configured with HA via MQTT.

1 Like

Thank you for this script!

I have a few questions that I was not able to answer looking through this thread or through the code on github.

Here is a simplified version of my automation:

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 }}'
  shellyswitch25-DEADBEEF0123:
    mode: relay
    relay-0: light
    relay-0-name: Room Fan Light
    relay-1: fan
    relay-1-name: Room Fan

What I am trying to do:

I have a motion sensitive switch wired to the input on this shelly. When the btn_mode is set to toggle, the load attached to the shelly turns on/off based on the motion sensor.
Some of the time, I do not want this automatic functionality. I wish to change the mode of the shelly to detached from time to time so the motion sensor does NOT directly trigger the shelly. When btn_modee is detached I want HA to see that the motion sensor has detected motion and then decide to toggle the shelly or not.

I have set up a rest_command to change the btn_mode on the device… so now I am trying to figure out how to get the input/{0,1} topics configured in Home Assistant.

Questions:

  1. I found the shelly API documentation for changing the btn_mode on a relay… but it looks like the API to change btn_type only works via HTTP. I was not able to change the type by sending json or raw messages to shellies/shellyswitch25-DEADBEEF0123/relay/0/command. Is there no way to change a btn_type via MQTT? Likewise, it looks like the shelly device does NOT expose the relay btn_type via MQTT; the only way to ‘learn’ this is to do so via the HTTP api, yes?

  2. Is mode: used anywhere? Was it used at one point in time? I don’t see any references in the python script. do i need the mode: relay line anymore?

  3. How can I disable the short/long press binary inputs. I only care about if the external switch is on/off. I can see this via the shellies/shellyswitch25-DEADBEEF0123/input/{0,1} topic but i don’t understand how the python script is configured to create long/short presses. How do i disable short/long press binary inputs and only get the ‘basic’ inputs?

@Bieniu with the latest firmware update to shelly pro4pm you can get separate topics for each channel if you turn on “Generic status update”.

Might be possible to integrate now easier

Yes

mode for Shelly 2.5 is deprecated.

You can disable these entities and create your own input sensor.

Even a manual configuration:

  - platform: mqtt
    name: "Shelly 4Pro PM Relay 0"
    command_topic: "shellypro4pm-1208d1d8b912/rpc"
    payload_on: '{"id": 1, "src":"homeassistant", "method": "Switch.Set", "params": {"id": 0, "on": true}}'
    payload_off: '{"id": 1, "src":"homeassistant", "method": "Switch.Set", "params": {"id": 0, "on": false}}'
    state_topic: "shellypro4pm-1208d1d8b912/status/switch:0"
    value_template: '{% if value_json.output == true %}ON{% else %}OFF{% endif %}'

doesn’t work.

The script also needs the device to send announce topic.

I have working configuration (without availability) for Shelly Pro 4PM:

switch:
  - platform: mqtt
    name: "Shelly 4Pro PM Relay 0"
    unique_id: "shellypro4pm-f008d1xxxxxx-switch-0"
    command_topic: "shellypro4pm-f008d1xxxxxx/rpc"
    payload_on: '{"id": 1, "src":"homeassistant", "method": "Switch.Set", "params": {"id": 0, "on": true}}'
    payload_off: '{"id": 1, "src":"homeassistant", "method": "Switch.Set", "params": {"id": 0, "on": false}}'
    state_topic: "shellypro4pm-f008d1xxxxxx/status/switch:0"
    value_template: "{{ value_json.output }}"
    state_on: true
    state_off: false
    qos: 1
1 Like

Hi @Bieniu

I updated my shellies_discovery.py today for the first time since Oct 2020, it gave me a lot of errors about having to update my shelly device firmware, Ive updated all of them to the latest version now.
Im getting these errors in my HA logs when it starts, any idea what i need to do ?

Logger: homeassistant.util.logging
Source: util/logging.py:105
First occurred: 12:20:48 PM (90 occurrences)
Last logged: 12:21:15 PM

Exception in async_discover when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'name': 'Shelly Dimmer 2 D8BFC019CB0E Energy 0', 'state_topic': 'shellies/shellydimmer2-D8BFC019CB0E/light/0/energy', 'unit_of_measurement': 'kWh', 'value_template': '{{(value|float/60/1000)|round(2)}}', 'device_class': 'energy', 'availability_topic': 'shellies/shellydimmer2-D8BFC019CB0E/online', 'payload_available': 'true', 'payload_not_available': 'false', 'force_update': 'False', 'unique_id': 'shellydimmer2-d8bfc019cb0e-white-energy-0', 'qos': 0, 'device': {'identifiers': ['d8bfc019cb0e'], 'name': 'Shelly Dimmer 2 D8BFC019CB0E', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'state_class': 'total_increasing', 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 180, in async_discover config = schema(discovery_payload) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value must be one of ['measurement'] for dictionary value @ data['state_class']
Exception in async_discover when dispatching 'mqtt_discovery_new_binary_sensor_mqtt': ({'name': 'Shelly Dimmer 2 D8BFC019EEFE Firmware Update', 'state_topic': 'shellies/shellydimmer2-D8BFC019EEFE/info', 'enabled_by_default': 'True', 'unique_id': 'shellydimmer2-d8bfc019eefe-firmware-update', 'qos': 0, 'device': {'identifiers': ['d8bfc019eefe'], 'name': 'Shelly Dimmer 2 D8BFC019EEFE', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'value_template': "{%if value_json['update'].has_update==true%}ON{%else%}OFF{%endif%}", 'availability_topic': 'shellies/shellydimmer2-D8BFC019EEFE/online', 'payload_available': 'true', 'payload_not_available': 'false', 'device_class': 'update', 'json_attributes_topic': 'shellies/shellydimmer2-D8BFC019EEFE/info', 'json_attributes_template': "{{value_json['update']|tojson}}", 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 180, in async_discover config = schema(discovery_payload) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value must be one of ['battery', 'battery_charging', 'cold', 'connectivity', 'door', 'garage_door', 'gas', 'heat', 'light', 'lock', 'moisture', 'motion', 'moving', 'occupancy', 'opening', 'plug', 'power', 'presence', 'problem', 'safety', 'smoke', 'sound', 'vibration', 'window'] for dictionary value @ data['device_class']
Exception in async_discover when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'name': 'Shelly Dimmer 2 D8BFC019EEFE Energy 0', 'state_topic': 'shellies/shellydimmer2-D8BFC019EEFE/light/0/energy', 'unit_of_measurement': 'kWh', 'value_template': '{{(value|float/60/1000)|round(2)}}', 'device_class': 'energy', 'availability_topic': 'shellies/shellydimmer2-D8BFC019EEFE/online', 'payload_available': 'true', 'payload_not_available': 'false', 'force_update': 'False', 'unique_id': 'shellydimmer2-d8bfc019eefe-white-energy-0', 'qos': 0, 'device': {'identifiers': ['d8bfc019eefe'], 'name': 'Shelly Dimmer 2 D8BFC019EEFE', 'model': 'Shelly Dimmer 2', 'sw_version': '20210909-150154/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'state_class': 'total_increasing', 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 180, in async_discover config = schema(discovery_payload) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value must be one of ['measurement'] for dictionary value @ data['state_class']
Exception in async_discover when dispatching 'mqtt_discovery_new_sensor_mqtt': ({'name': 'Shelly 1PM A4CF12F3D9F5 Energy 0', 'state_topic': 'shellies/shelly1pm-A4CF12F3D9F5/relay/0/energy', 'unit_of_measurement': 'kWh', 'value_template': '{{(value|float/60/1000)|round(2)}}', 'device_class': 'energy', 'availability_topic': 'shellies/shelly1pm-A4CF12F3D9F5/online', 'payload_available': 'true', 'payload_not_available': 'false', 'force_update': 'False', 'unique_id': 'shelly1pm-a4cf12f3d9f5-relay-energy-0', 'qos': 0, 'device': {'identifiers': ['a4cf12f3d9f5'], 'name': 'Shelly 1PM A4CF12F3D9F5', 'model': 'Shelly 1PM', 'sw_version': '20210909-145919/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'state_class': 'total_increasing', 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 180, in async_discover config = schema(discovery_payload) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value must be one of ['measurement'] for dictionary value @ data['state_class']
Exception in async_discover when dispatching 'mqtt_discovery_new_binary_sensor_mqtt': ({'name': 'Shelly 1PM A4CF12F3D9F5 Firmware Update', 'state_topic': 'shellies/shelly1pm-A4CF12F3D9F5/info', 'enabled_by_default': 'True', 'unique_id': 'shelly1pm-a4cf12f3d9f5-firmware-update', 'qos': 0, 'device': {'identifiers': ['a4cf12f3d9f5'], 'name': 'Shelly 1PM A4CF12F3D9F5', 'model': 'Shelly 1PM', 'sw_version': '20210909-145919/v1.11.4-DNSfix-ge6b2f6d', 'manufacturer': 'Allterco Robotics'}, 'value_template': "{%if value_json['update'].has_update==true%}ON{%else%}OFF{%endif%}", 'availability_topic': 'shellies/shelly1pm-A4CF12F3D9F5/online', 'payload_available': 'true', 'payload_not_available': 'false', 'device_class': 'update', 'json_attributes_topic': 'shellies/shelly1pm-A4CF12F3D9F5/info', 'json_attributes_template': "{{value_json['update']|tojson}}", 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 180, in async_discover config = schema(discovery_payload) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.9/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: value must be one of ['battery', 'battery_charging', 'cold', 'connectivity', 'door', 'garage_door', 'gas', 'heat', 'light', 'lock', 'moisture', 'motion', 'moving', 'occupancy', 'opening', 'plug', 'power', 'presence', 'problem', 'safety', 'smoke', 'sound', 'vibration', 'window'] for dictionary value @ data['device_class']

HA 2021.9 is required.

Hi @Bieniu - Thanks for the response, ive just upgraded. Im getting this error now,

Logger: homeassistant.components.python_script.shellies_discovery.py
Source: components/python_script/__init__.py:222
Integration: Python Scripts ([documentation](https://www.home-assistant.io/integrations/python_script), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+python_script%22))
First occurred: 9:31:15 AM (4 occurrences)
Last logged: 11:00:13 AM

Error executing script: mode value None is not valid, check script configuration

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 1638, in <module> ValueError: mode value None is not valid, check script configuration

Did you check the script configuration?

image