Shellies Discovery Script

This script adds MQTT discovery support for Shelly devices and without configuration it adds entities to Home Assistant.

Buy Me A Coffee

This is screenshots with Home Assistant Integrations page:

Gen2 devices information

Pro/Plus devies are supported by Shellies Discovery Gen2 script.

Supported devices:

  • Shelly 1 (with external sensors)
  • Shelly 1L
  • Shelly 1PM (with external sensors)
  • Shelly 2 (relays and roller mode)
  • Shelly 2.5 (relays and roller mode)
  • Shelly 3EM
  • Shelly 4Pro
  • Shelly Air
  • Shelly Bulb
  • Shelly Bulb RGBW
  • Shelly Button1
  • Shelly Dimmer
  • Shelly Dimmer 2
  • Shelly Door/Window
  • Shelly Door/Window 2
  • Shelly DUO
  • Shelly EM
  • Shelly Flood
  • Shelly H&T (with or without USB adapter)
  • Shelly i3
  • Shelly Plug
  • Shelly Plug S
  • Shelly Plug US
  • Shelly RGBW2 (color and white mode)
  • Shelly Sense
  • Shelly Smoke
  • Shelly Valve
  • Shelly Vintage
  • Shelly UNI (with external sensors)
  • Shelly Motion

Repository: GitHub - bieniu/ha-shellies-discovery: Script that adds MQTT discovery support for Shellies devices

Documentation: ha-shellies-discovery/README.md at master · bieniu/ha-shellies-discovery · GitHub

39 Likes

New version: 0.0.2

Changelog:

  • add support for Shelly Plug and Shelly4Pro

Hi!

Just tried this script with my shelly-plug.
Both the switch and the power-sensor is added as entities under MQTT integration, but I think the power-sensor is not subscribing to the correct state_topic.

Ever since I included it with the script instead of the manual config, I get state unknown for the sensor.

I am new to Python, so I am not fully following the syntax, but seems that you are making the sensor subscribe to the wrong topic. Correct topic would be:
shellies/shellyplug-7BD83C//relay/0/power.

It is missing the relay-number.
With the current script, the sensor subscribes to:
shellies/shellyplug-7BD83C/relay/power

If i publish to that topic, the sensor updates directly.

As mentioned, I have never worked with python, so not 100% sure on how to get the syntax correct. Could you please update it?

On a related topic, is there any way I can review what settings the entities were created with? I am not finding the config-entries anywhere in the files, so the troubleshooting was a bit more guesswork than would have been nessecary if I could have reviewed the entity topics manually.

Thanks!

Yes you are right. I only have Shelly2 and it send topic without relay number. I will correct the script. Does Shelly Plug send correct data in topic energy?

Hi
Energy is published in “shellies/shellyplug-XXXXXX/relay/0/energy”
with unit [10 * Wh] seems like a very strange energy-unit.
So if you do end up adding it, please convert it to kWh directly, seems confusing to use a non SI unit of 10Wh

For info,
Looking at the API-documentation, it appears that the 4pro is using the same logic with */0/power as the plug

https://shelly-api-docs.shelly.cloud/#4pro-mqtt

Yes, I have read API documentation. Shelly2 for two relays sends power in one topic and doesn’t send energy at all because of issue in firmware. I thought that Shelly Plug and Shelly4Pro have the same issue.

Can you add support for Shelly H/T
image

- platform: mqtt
  name: "HT Dinner  Temp"
  state_topic: "shellies/shellyht-YOUR_ID_1/sensor/temperature"
  unit_of_measurement: '°C'
  device_class: temperature
- platform: mqtt
  name: "HT Dinner  Humidity"
  state_topic: "shellies/shellyht-YOUR_ID_1/sensor/humidity"
  unit_of_measurement: '%'
  device_class: humidity
- platform: mqtt
  name: "HT Dinner  Battery"
  state_topic: "shellies/shellyht-YOUR_ID_1/sensor/battery"
  unit_of_measurement: '%'
  device_class: battery
- platform: mqtt
  name: "HT Dinner  Online"
  state_topic: "shellies/shellyht-YOUR_ID_1/online"

  
- platform: mqtt
  name: "HT Living Temp"
  state_topic: "shellies/shellyht-YOUR_ID_2/sensor/temperature"
  unit_of_measurement: '°C'
  device_class: temperature
- platform: mqtt
  name: "HT Living Humidity"
  state_topic: "shellies/shellyht-YOUR_ID_2/sensor/humidity"
  unit_of_measurement: '%'
  device_class: humidity
- platform: mqtt
  name: "HT Living Battery"
  state_topic: "shellies/shellyht-YOUR_ID_2/sensor/battery"
  unit_of_measurement: '%'
  device_class: battery
- platform: mqtt
  name: "HT Living Online"
  state_topic: "shellies/shellyht-YOUR_ID_2/online"
 
- platform: mqtt
  name: "Shelly Firmware"
  state_topic: "shellies/announce"
  value_template: "{{ value_json.new_fw }}"
  json_attributes:
    - fw_ver  
    - new_fw
    - id
    - mac
    - ip
2 Likes

New version: 0.1.0

Changelog:

  • add developer mode for safe debug
  • fix power sensor topic for Shelly Plug and Shelly4Pro
  • add energy sensor for Shelly Plug and Shelly4Pro
1 Like

@Flanders Please, write whether the sensors are working properly on the new version of the script.
@elRadix Thanks for the configuration. Soon I will add support for ShellyH&T to the script.

1 Like

@elRadix Does ShellyH&T in the announce topic send the unit that is used for the temperature sensor (°C or °F)?

I don’t understand What’s the benefit using this script… Shelly have already mqtt support by default in their fw.
Or with this script we can get more infos that i missed?

You add only the script and two automations to HA configuration, and each Shelly will be added to HA via MQTT Discovery with support for device registry and entity registry.

Screenshot_2019-02-27%20Home%20Assistant
Screenshot_2019-02-27%20Home%20Assistant(1) Screenshot_2019-02-27%20Home%20Assistant(2)

Hi!
Forgot to post feedback after testing yesterday, sorry.

Tested it, and works fine. Power sensor is added with correct topic and is now working.
The old non working power sensor is still there though. not sure if it was re-added, or if I failed to delete it completely.

image

The 2nd and 3rd I re-named and are the working ones, the first one is the sensor with the wrong topic that gets re-added even after deleting it in the entity-registry.

To remove the incorrect power sensor you have to:

  • Send empty message to MQTT broker with topic shellies/shellyplug-7BD83C/relay/power and retain flag. You can use this command mosquitto_pub -h MQTT_BROKER_IP -p MQTT_BROKER_PORT -u MQTT_USER -P MQTT_PASSWORD -r -t 'shellies/shellyplug-7BD83C/relay/power' -m ''
  • Remove sensor entity from entity registry. You can do it manually from file .storage/core.entity_registry (not recomended) or from Entity Registry in frontend (Settings -> Entity Registry).
  • restart Home Assistant

What about energy sensor? Does it work correctly?

EDIT: I found stupid mistake in script with energy sensor.

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