Shellies Discovery Script

Tags: #<Tag:0x00007f3f2e95f898>

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

This is screenshots with Home Assistant Integrations page:

Supported devices:

  • Shelly1
  • Shelly1PM
  • Shelly2 (relays and roller mode)
  • Shelly2.5 (relays and roller mode)
  • Shelly4Pro
  • Shelly Plug
  • Shelly Plug S
  • Shelly RGBW2 (color and white mode)
  • Shelly H&T
  • Shelly Smoke
  • Shelly Sense
  • ShellyEM

Installation

You can install this script via HACS or just download shellies_discovery.py file and save it in /config/python_scripts folder.

Repository: https://github.com/bieniu/ha-shellies-discovery

Minimal configuration:

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

Custom configuration example

automation:
  - 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 }}'
        discovery_prefix: 'hass'
        temp_unit: 'F'
        qos: 2
        shelly1-001122-relay-0: 'light'
        shellyswitch-9900AA-relay-0: 'light'
        shellyswitch-9900AA-relay-1: 'fan'
        shellyswitch25-334411-relay-1: 'light'
        shellyswitch-334455: 'cover'
        shellyrgbw2-AABB22: 'white'
        shellyrgbw2-CC2211: 'rgbw'

Script arguments

key optional type default description
discovery_prefix True string homeassistant MQTT discovery prefix
temp_unit True string C temperature unit, C for Celsius, F for Farenhait
qos True integer 0 MQTT QoS, you can use 0, 1 or 2
relay_id/shelly_id True string switch HA component to use with relay_id, for example: shelly1-001122-relay-0: 'light' means that relay 0 of shelly1-001122 will use light component in HA. You can use switch, light or fan. For Shelly2 and Shelly2.5 you can use shellyswitch-334455: 'cover' for roller mode. For ShellyRGBW2 you can use shellyrgbw2-AABB22: 'white' for wite mode.

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 twice.

11 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
1 Like

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