This script adds MQTT discovery support for Shelly Gen2, Gen3 and Gen4 devices in the Home Assistant.

This script needs Home Assistant python_script component so, if you never used it, I strongly suggest you to follow the official instruction and check that python_script is properly configured and it's working.
For the device to work with the script, it must have MQTT configured and options Enable "MQTT Control", RPC status notifications over MQTT and Generic status update over MQTT enabled.
MQTT integration must be configured in Home Assistant.
You can download shellies_discovery_gen2.py file and save it in <config>/python_scripts folder or install the script via HACS.
You won't find Shellies Discovery Gen2 in the HACS Integrations section, nor add it as a custom repository. You must have a properly configured python_script component to be able to install the script from the HACS Automations section.
Shellies Discovery Gen2 will automatically install/update the script on your Shelly device. Due to python_scripts integration limitations, updating the device script requires the announce automation to run twice.
You need to manually add the shellies_announce_gen2, shellies_discovery_gen2 automations and the shellies_components_gen2 script to your automations.yaml and scripts.yaml files.
[!IMPORTANT] You cannot manually run the Shellies Discovery automation. You can only manually run the Shellies Announce automation to begin the device configuration process.
Battery powered devices like Plus H&T are put to sleep most of the time. For this reason, adding/updating entities configuration for a device should be done as follows:
Shellies Announce Gen2 automationShellies Discovery supports the following virtual Shelly components:
boolean with mode labelboolean with mode togglebuttonenum with mode dropdownenum with mode labelnumber with mode labelnumber with mode slidertext with mode labelTo debug the script add this to your logger configuration:
# configuration.yaml file
logger:
default: error
logs:
homeassistant.components.python_script: debug
homeassistant.components.automation: info
homeassistant.components.mqtt.discovery: info
light or fan to configure light or fan platform)| key | optional | type | default | description |
|---|---|---|---|---|
discovery_prefix |
True | string | homeassistant |
MQTT discovery prefix |
script_prefix |
True | string | MQTT prefix to install the script in the device | |
qos |
True | integer | 0 |
MQTT QoS, you can use 0, 1 or 2 |
# configuration.yaml file
python_script:
# automations.yaml file
- id: shellies_announce_gen2
alias: "Shellies Announce Gen2"
triggers:
- trigger: homeassistant
event: start
variables:
get_config_payload: "{{ {'id': 1, 'src': 'shellies_discovery', 'method': 'Shelly.GetConfig'} | to_json }}"
device_ids: # enter the list of device IDs (MQTT prefixes) here
- shellyplus2pm-485519a1ff8c
- custom-prefix/shelly-kitchen
actions:
- repeat:
for_each: "{{ device_ids }}"
sequence:
- action: mqtt.publish
data:
topic: "{{ repeat.item }}/rpc"
payload: "{{ get_config_payload }}"
- action: script.shellies_components_gen2
data:
device_topic: "{{ repeat.item }}"
- id: shellies_discovery_gen2
alias: "Shellies Discovery Gen2"
mode: queued
max: 50
triggers:
- trigger: mqtt
topic: shellies_discovery/rpc
actions:
- condition: template
value_template: "{{ 'components' not in (trigger.payload_json.result | default({})) }}"
- action: python_script.shellies_discovery_gen2
data:
id: "{{ trigger.payload_json.src }}"
device_config: "{{ trigger.payload_json.result }}"
- condition: template
value_template: "{{ 'mqtt' in trigger.payload_json.result }}"
- action: mqtt.publish
data:
topic: "{{ trigger.payload_json.result.mqtt.topic_prefix }}/command"
payload: "status_update"
# scripts.yaml file
shellies_components_gen2:
alias: Shellies Components Gen2
mode: queued
max: 50
fields:
device_topic:
description: MQTT topic prefix for the device, e.g. shellies/shelly-1-gen4-abc123
required: true
discovery_prefix:
description: MQTT discovery prefix
default: homeassistant
sequence:
- variables:
device_name: "{{ device_topic.split('/') | last }}"
src: shellies_discovery/{{ device_topic.split('/') | last }}
response_topic: shellies_discovery/{{ device_topic.split('/') | last }}/rpc
all_pages: []
offset: 0
total: 1
device_id: "{{ device_topic.split('/') | last }}"
- repeat:
while:
- condition: template
value_template: "{{ offset < total }}"
sequence:
- action: mqtt.publish
data:
topic: "{{ device_topic }}/rpc"
payload: "{{ {'id': 1, 'src': src, 'method': 'Shelly.GetComponents', 'params': {'include': ['config'], 'offset': offset}} | to_json }}"
- wait_for_trigger:
- trigger: mqtt
topic: "{{ response_topic }}"
timeout: "00:00:30"
- if:
- condition: template
value_template: "{{ wait.trigger is none }}"
then:
- stop: Timeout waiting for Shelly.GetComponents from {{ device_topic }}
- variables:
page: "{{ wait.trigger.payload_json.result }}"
device_id: "{{ wait.trigger.payload_json.src }}"
- variables:
all_pages: "{{ all_pages + [page] }}"
offset: "{{ page.offset + (page.components | length) }}"
total: "{{ page.total }}"
- action: python_script.shellies_discovery_gen2
data:
id: "{{ device_id }}"
device_config:
components: "{{ all_pages }}"
discovery_prefix: "{{ discovery_prefix }}"
- variables:
all_components: "{{ all_pages | map(attribute='components') | list | flatten }}"
- variables:
mqtt_comp: "{{ all_components | selectattr('key', 'equalto', 'mqtt') | list | first | default(none) }}"
- condition: template
value_template: "{{ mqtt_comp is not none }}"
- action: mqtt.publish
data:
topic: "{{ mqtt_comp.config.topic_prefix }}/command"
payload: status_update
$ claude mcp add ha-shellies-discovery-gen2 \
-- python -m otcore.mcp_server <graph>