Home Assistant Community

Shellies Discovery Script

mqtt
Tags: #<Tag:0x00007fcf4b11d420>
#1

This script adds MQTT discovery support for Shellies. It works with Shelly1, Shelly1PM, Shelly2, Shelly2.5, Shelly4Pro, Shelly Plug, Shelly Plug S, Shelly H&T, Shelly Smoke, Shelly Sense, Shelly RGBW2 (color and white mode) and without configuration it adds entities to Home Assistant.

Download: https://github.com/bieniu/home-assistant-config/blob/master/python_scripts/shellies_discovery.py

How to install python_script: https://www.home-assistant.io/components/python_script/

This is screenshots with Home Assistant Integrations:

Arguments:

  • discovery_prefix - discovery prefix in HA, default ‘homeassistant’, optional
  • id - Shelly ID (required)
  • mac - Shelly MAC address (required)
  • fw_ver - Shelly firmware version (optional)
  • temp_unit - C for Celsius, F for Farenhait, default C (optional)
  • list of shelies id relays and components for them

Default configuration:
Automations:

- id: shellies_announce
  alias: 'Shellies Announce'
  trigger:
    - platform: homeassistant
      event: start
  action:
    service: mqtt.publish
    data:
      topic: shellies/command
      payload: announce

- 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 }}'

If you want the relay to be a other component than the switch in the Home
Assistant, you have to add a description of the relay and its function to the
script configuration.
For example:

- 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 }}'
      qos: 2
      shellyswitch25-001122-relay-0: 'light'
      shellyswitch25-001122-relay-1: 'fan'
      shellyswitch-334455: 'cover'
      shellyrgbw2-AABB22: 'white'
      shellyrgbw2-CC2211: 'rgbw'

qos - maximum QoS level of the topics, this is optional argument, default is 0 (integer)

shellyswitch25-001122-relay-0: 'light' - means that relay 0 of shellyswitch25-001122 will use light component in Home Assistant. You can use switch, light or fan.

shellyswitch25-001122-relay-1: 'fan' - means that relay 0 of shellyswitch25-001122 will use fan component in Home Assistant. You can use switch, light or fan.

shellyswitch-334455: 'cover' - means that Shelly2 works in roller mode and use cover component in Home Assistant.

shellyrgbw2-AABB22: 'white' - means that Shelly RGBW2 works in white-mode

shellyrgbw2-CC2211: 'rgbw' - means that Shelly RGBW2 works in color-mode (default)

If you already have Shellies configuration before using the script, you should delete it. After installing the script and adding automations, run Shellies Announce automation or restart Home Assistant.

If you change the component type for the relay you have to remove the old entities in Settings -> Entity Registry and restart the Home Assistant.

Script supports custom_updater component. Add this to your configuration and
stay up-to-date.

custom_updater:
  track:
    - python_scripts
  python_script_urls:
    - https://raw.githubusercontent.com/bieniu/home-assistant-config/master/python_scripts/python_scripts.json
9 Likes
Shelly H&T with MQTT
Shelly factory firmware configuration with Home Assistant
Solved: Mqtt turns me mad
Shelly 1 will topic and message
Shelly 2 as roller shutter with percentage
#2

New version: 0.0.2

Changelog:

  • add support for Shelly Plug and Shelly4Pro
#3

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!

#4

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?

#5

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

#6

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

#7

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.

#8

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
1 Like
#9

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
#10

@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
#11

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

#12

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?

#13

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)

#14

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.

#15

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.

#16

New version: 0.1.1

Changelog:

  • fix energy sensor for Shelly Plug
#17

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’

#18

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
#19

Thanks!

Confirmed via NodeRed, currently a 3 length string
image

#20

New version: 0.2.0

Changelog:

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