Shellies Discovery Script

New version: 0.3.0

Changelog:

  • no need to add id and component type for the relay in script configuration, for example shellyswitch-5B2604-relay-0: 'switch', all relays are switches by default
1 Like

I’m new to Home Assistant and am trying to get your script to detect a Shelly H&T. I’ve added the python script and automations. I haven’t added a shellyht argument to the script because, as I understand it, that is no longer needed unless I am misreading the note for version 0.3.0. Otherwise, the script is unmodified.

When I execute Shellies Announce, there are entries in the Mosquitto broker and Home-Assistant log that seem to show that things are happening, but nothing shows up in the MQTT section of Integrations, and there are no entries in Entity Registry.

This is what the Mosquitto Log gives me:
554444124: New connection from 192.168.1.56 on port 1883.
1554444124: |-- mosquitto_auth_unpwd_check(mosquitto)
1554444124: |-- ** checking backend http
1554444124: |-- url=http://127.0.0.1:8080/login
1554444124: |-- data=username=mosquitto&password=XXXXX&topic=&acc=-1&clientid=
[INFO] found mosquitto on Home Assistant
1554444127: |-- getuser(mosquitto) AUTHENTICATED=1 by http
1554444127: New client connected from 192.168.1.56 as shellyht-XXXXXX (c1, k60, u’mosquitto’).

This is what the Home-Assistant log gives me. One thing that jumps out at me is the mac address retrieved from “announce” IS NOT the mac on the Shelly H&T. It is off by one digit. The mac from DHCP begins with CC5 but the mac from announce begins with CE5. Otherwise, they are the same. I don’t know how this is possible, but then again, I don’t know how any of this works. Do you see what is going wrong?

2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1835638608] Received {'type': 'call_service', 'domain': 'automation', 'service': 'trigger', 'service_data': {'entity_id': 'automation.shellies_announce'}, 'id': 82}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=automation, service=trigger, service_data=entity_id=automation.shellies_announce>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Shellies Announce, entity_id=automation.shellies_announce>
2019-04-05 00:04:09 INFO (MainThread) [homeassistant.components.automation] Executing Shellies Announce
2019-04-05 00:04:09 INFO (MainThread) [homeassistant.helpers.script] Script Shellies Announce: Running script
2019-04-05 00:04:09 INFO (MainThread) [homeassistant.helpers.script] Script Shellies Announce: Executing step call service
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=mqtt, service=publish, service_data=payload=announce, topic=shellies/command>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on shellies/command: announce
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.shellies_announce, old_state=<state automation.shellies_announce=on; last_triggered=2019-04-05T00:02:04.536685-06:00, id=shellies_announce, friendly_name=Shellies Announce @ 2019-04-04T21:47:59.077741-06:00>, new_state=<state automation.shellies_announce=on; last_triggered=2019-04-05T00:04:09.540555-06:00, id=shellies_announce, friendly_name=Shellies Announce @ 2019-04-04T21:47:59.077741-06:00>>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1835520464] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=automation.shellies_announce, old_state=<state automation.shellies_announce=on; last_triggered=2019-04-05T00:02:04.536685-06:00, id=shellies_announce, friendly_name=Shellies Announce @ 2019-04-04T21:47:59.077741-06:00>, new_state=<state automation.shellies_announce=on; last_triggered=2019-04-05T00:04:09.540555-06:00, id=shellies_announce, friendly_name=Shellies Announce @ 2019-04-04T21:47:59.077741-06:00>>}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1835638608] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=automation.shellies_announce, old_state=<state automation.shellies_announce=on; last_triggered=2019-04-05T00:02:04.536685-06:00, id=shellies_announce, friendly_name=Shellies Announce @ 2019-04-04T21:47:59.077741-06:00>, new_state=<state automation.shellies_announce=on; last_triggered=2019-04-05T00:04:09.540555-06:00, id=shellies_announce, friendly_name=Shellies Announce @ 2019-04-04T21:47:59.077741-06:00>>}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.mqtt] Received message on shellies/announce: b'{"id":"shellyht-XXXXXX","mac":"XXXXXXXXXXXX","ip":"192.168.1.56","new_fw":false, "fw_ver":"20190402-134544/v1.4.9@9be72c7e"}'
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event automation_triggered[L]: name=Shellies Discovery, entity_id=automation.shellies_discovery>
2019-04-05 00:04:09 INFO (MainThread) [homeassistant.components.automation] Executing Shellies Discovery
2019-04-05 00:04:09 INFO (MainThread) [homeassistant.helpers.script] Script Shellies Discovery: Running script
2019-04-05 00:04:09 INFO (MainThread) [homeassistant.helpers.script] Script Shellies Discovery: Executing step call service
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=python_script, service=shellies_discovery, service_data=discovery_prefix=hass, id=shellyht-XXXXXX, mac=XXXXXXXXXXXX, fw_ver=20190402-134544/v1.4.9@9be72c7e, temp_unit=F>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1835638608] Sending {'id': 82, 'type': 'result', 'success': True, 'result': None}
2019-04-05 00:04:09 INFO (SyncWorker_17) [homeassistant.components.python_script] Executing shellies_discovery.py: {'discovery_prefix': 'hass', 'id': 'shellyht-XXXXXX', 'mac': 'XXXXXXXXXXXX', 'fw_ver': '20190402-134544/v1.4.9@9be72c7e', 'temp_unit': 'F'}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=mqtt, service=publish, service_data=topic=hass/sensor/shellyht-XXXXXX-temperature/config, payload={"name":"ShellyH&T XXXXXX Temperature","stat_t":"~sensor/temperature","unit_of_meas":"°F","device_class":"temperature","val_tpl":"{{ value | round(1) }}","avty_t":"~online","pl_avail":"true","pl_not_avail":"false","uniq_id":"shellyht-XXXXXX-temperature","device": {"identifiers": ["XXXXXXXXXXXX"],"name":"ShellyH&T XXXXXX","model":"ShellyH&T","sw_version":"20190402-134544/v1.4.9@9be72c7e","manufacturer":"Shelly"},"~":"shellies/shellyht-XXXXXX/"}, retain=True, qos=0>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on hass/sensor/shellyht-XXXXXX-temperature/config: {"name":"ShellyH&T XXXXXX Temperature","stat_t":"~sensor/temperature","unit_of_meas":"°F","device_class":"temperature","val_tpl":"{{ value | round(1) }}","avty_t":"~online","pl_avail":"true","pl_not_avail":"false","uniq_id":"shellyht-XXXXXX-temperature","device": {"identifiers": ["XXXXXXXXXXXX"],"name":"ShellyH&T XXXXXX","model":"ShellyH&T","sw_version":"20190402-134544/v1.4.9@9be72c7e","manufacturer":"Shelly"},"~":"shellies/shellyht-XXXXXX/"}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=mqtt, service=publish, service_data=topic=hass/sensor/shellyht-XXXXXX-humidity/config, payload={"name":"ShellyH&T XXXXXX Humidity","stat_t":"~sensor/humidity","unit_of_meas":"%","device_class":"humidity","val_tpl":"{{ value | round(1) }}","avty_t":"~online","pl_avail":"true","pl_not_avail":"false","uniq_id":"shellyht-XXXXXX-humidity","device": {"identifiers": ["XXXXXXXXXXXX"],"name":"ShellyH&T XXXXXX","model":"ShellyH&T","sw_version":"20190402-134544/v1.4.9@9be72c7e","manufacturer":"Shelly"},"~":"shellies/shellyht-XXXXXX/"}, retain=True, qos=0>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on hass/sensor/shellyht-XXXXXX-humidity/config: {"name":"ShellyH&T XXXXXX Humidity","stat_t":"~sensor/humidity","unit_of_meas":"%","device_class":"humidity","val_tpl":"{{ value | round(1) }}","avty_t":"~online","pl_avail":"true","pl_not_avail":"false","uniq_id":"shellyht-XXXXXX-humidity","device": {"identifiers": ["XXXXXXXXXXXX"],"name":"ShellyH&T XXXXXX","model":"ShellyH&T","sw_version":"20190402-134544/v1.4.9@9be72c7e","manufacturer":"Shelly"},"~":"shellies/shellyht-XXXXXX/"}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=mqtt, service=publish, service_data=topic=hass/sensor/shellyht-XXXXXX-battery/config, payload={"name":"ShellyH&T XXXXXX Battery","stat_t":"~sensor/battery","unit_of_meas":"%","device_class":"battery","val_tpl":"{{ value | round }}","avty_t":"~online","pl_avail":"true","pl_not_avail":"false","uniq_id":"shellyht-XXXXXX-battery","device": {"identifiers": ["XXXXXXXXXXXX"],"name":"ShellyH&T XXXXXX","model":"ShellyH&T","sw_version":"20190402-134544/v1.4.9@9be72c7e","manufacturer":"Shelly"},"~":"shellies/shellyht-XXXXXX/"}, retain=True, qos=0>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.mqtt] Transmitting message on hass/sensor/shellyht-XXXXXX-battery/config: {"name":"ShellyH&T XXXXXX Battery","stat_t":"~sensor/battery","unit_of_meas":"%","device_class":"battery","val_tpl":"{{ value | round }}","avty_t":"~online","pl_avail":"true","pl_not_avail":"false","uniq_id":"shellyht-XXXXXX-battery","device": {"identifiers": ["XXXXXXXXXXXX"],"name":"ShellyH&T XXXXXX","model":"ShellyH&T","sw_version":"20190402-134544/v1.4.9@9be72c7e","manufacturer":"Shelly"},"~":"shellies/shellyht-XXXXXX/"}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.shellies_discovery, old_state=<state automation.shellies_discovery=on; last_triggered=2019-04-05T00:02:12.031827-06:00, id=shellies_discovery, friendly_name=Shellies Discovery @ 2019-04-04T21:47:59.191797-06:00>, new_state=<state automation.shellies_discovery=on; last_triggered=2019-04-05T00:04:09.802640-06:00, id=shellies_discovery, friendly_name=Shellies Discovery @ 2019-04-04T21:47:59.191797-06:00>>
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1835520464] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=automation.shellies_discovery, old_state=<state automation.shellies_discovery=on; last_triggered=2019-04-05T00:02:12.031827-06:00, id=shellies_discovery, friendly_name=Shellies Discovery @ 2019-04-04T21:47:59.191797-06:00>, new_state=<state automation.shellies_discovery=on; last_triggered=2019-04-05T00:04:09.802640-06:00, id=shellies_discovery, friendly_name=Shellies Discovery @ 2019-04-04T21:47:59.191797-06:00>>}
2019-04-05 00:04:09 DEBUG (MainThread) [homeassistant.components.websocket_api.http.connection.1835638608] Sending {'id': 2, 'type': 'event', 'event': <Event state_changed[L]: entity_id=automation.shellies_discovery, old_state=<state automation.shellies_discovery=on; last_triggered=2019-04-05T00:02:12.031827-06:00, id=shellies_discovery, friendly_name=Shellies Discovery @ 2019-04-04T21:47:59.191797-06:00>, new_state=<state automation.shellies_discovery=on; last_triggered=2019-04-05T00:04:09.802640-06:00, id=shellies_discovery, friendly_name=Shellies Discovery @ 2019-04-04T21:47:59.191797-06:00>>}

Are you sure that your discovery prefix is hass not homeassistant? There is no errors in log. Everything should work.
Every Shelly send topic shellies/announce with it’s MAC address. If this isn’t correct ask Shelly support about this.

New version: 0.4.0

Changelog:

  • support for roller mode for Shelly2
    shellyswitch-5B1200-roller-0: 'cover' in script configuration to use Shelly2 as cover in Home Assistant

New version: 0.4.1

Changelog:

  • fix topics for roller mode

Hi @Bieniu

Reply to your message in Roller shutter thread

I see your Python script, but I don’t understand if this is an automatic discovery script or if it needs some parameters besides the discovery_prefix.

I have several Shelly 2 and some Shelly 1, with same prefix, how should I call the script?

Thanks

If you use Shellies relays as switches in HA and discovery_prefix in your MQTT configuration is homeassistant, you don’t need to add parameters besides id, mac, fw_ver to call the script.
This is example:

service: python_script.shellies_discovery
data_template:
  id: '{{ trigger.payload_json.id }}'
  mac: '{{ trigger.payload_json.mac }}'
  fw_ver: '{{ trigger.payload_json.fw_ver }}'

Ok, done it and this is the result

Why the sensor.shelly_shsw* report that values even if they are not operating? Is the last value recorded, or what?

Doing this discovery it will write those cover, sensor and switch definition somewhere where I can change a friendly_name and add some other stuff?

Should it be the information regardin the sw version as an attribute to the device? I don’t see it in any place.

Thanks

What are sensor.shelly_shsw*? Power sensors show the current power measurement. I’m not sure if Shelly2 sends a power topic with roller mode.

You can edit names and entity_ids in Settings -> Entity Registry.
image

The script supports device registry, go Settings -> Integrations -> MQTT.
image

Yes, power is measured also in roller shutter mode, just seen.

How is defined retain and qos, or it gains from the Shelly definition?

Can you check in what topic Shelly2 sends a power measurement in roller mode?
Script doeasn’t interfere with retain or other MQTT options what Shellies send. The script based on topic shellies/announce creates only the appropriate config topic that adds entities to HA.

The topic for power measurement is shellies/shellyswitch-xxxxxx/relay/power

The topic in script is correct.

Thanks @Bieniu, changing from hass to homeassistant for the discovery prefix fixed the problem. Don’t know how I missed it, but you might change it in your example automation to match the default.

I will check with Shelly support as to why the MAC doesn’t match the reported MAC

Hello @Bieniu and thanks for this beautiful script !

I’m trying to use it with my Shelly HT , and i feel like i’m not far from succeed but I don’t know what i’m missing.

I’m using embeded MQTT on home assistant on docker, i’m receiving well MQTT messages from my shelly , but no entity are created, do you know what I should watch next ?

thanks in advance

Best regards,

Do you use correct automation to run the script? In your topics thera aren’t MAC addresses and firmware versions.

- id: 'shellies_discovery'
  alias: 'Shellies Discovery'
  trigger:
  - platform: mqtt
    topic: shellies/announce
  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 }}'

Hello, thanks for your answer !

I have put this in the automations.yaml file

image

You have to write me full announce topic that your Shelly send to MQTT broker.

Thanks again for your answer, sorry i’m a bit new to MQTT.

From what I understand I need to subscribe with a MQTT client to directly read the shellies topic, right ?

my MQTT server isn’t exposed from outside so I can’t use an MQTT client right now, i’ll try tonight at home.

The only thing I can provide right now is the HA logs, where just before the “Transmitting messages” the MQTT component is logging the received message, which seems not that much verbose :

Regards,

Very strange. This isn’t correct payload for Shelly announce topic. This is example of correct payload:

{"id":"shellyht-XXXXXX","mac":"XXXXXXXXXXXX","ip":"192.168.1.56","new_fw":false, "fw_ver":"20190402-134544/v1.4.9@9be72c7e"}

Without that my script doesn’t work.
Maybe factory reset helps?