Shellies Discovery Script

New version: 0.1.1

Changelog:

  • fix energy sensor for Shelly Plug

Where to send that command?
Via SSH from the Hassio mashine?
Or in the command line for mosquitto?

The energy sensor is added, but I am getting the following error message in the log.
State unknown for the sensor aswell

Exception in message_received when handling msg on ‘shellies/shellyplug-7BD83C/relay/0/energy’: ‘580’
Traceback (most recent call last):
File “/usr/local/lib/python3.7/site-packages/homeassistant/components/mqtt/sensor.py”, line 170, in message_received
payload, self._state)
File “/usr/local/lib/python3.7/site-packages/homeassistant/helpers/template.py”, line 174, in async_render_with_possible_json_value
return self._compiled.render(variables).strip()
File “/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/usr/local/lib/python3.7/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/usr/local/lib/python3.7/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/usr/local/lib/python3.7/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’

You have to use mosquitto_pub command in container or machine where Mosquitto is installed via SSH. You can also send this via Services tab in HA.

image

Your traceback suggest that payload for energy topic is string. Can You confirm?

1 Like

Thanks!

Confirmed via NodeRed, currently a 3 length string
image

New version: 0.2.0

Changelog:

  • once again, fix energy sensor for Shelly Plug
  • add support for Shelly H&T

Solved the energy-meter by changing the value-template to

{{ ( value | float / 10000) | round(2) }}

in the script for the value templates.
Also, I think it should be changed to / 100 instead of / 10000 if we should convert the *10Wh that the shellies report to kWh?

The faulty power meter keep getting re-added however despite the “” retain flag on the same topic

Of course You are right, I think I’m tired today :wink:

New version: 0.2.1

Changelog:

  • fix unit for energy sensor

Can someone help me get the discovery working. I added the python file, wrote python_script as mentioned in configuration.yaml also in automation.yaml added the text written. Restarted home assistant but it’s not showing Shelly devices. Mqtt discovery shows sonoff tasmota devices.

Show your configuration for the script.

the python_script or configuration.yaml?

shellies_discovery.py script configuration.

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