Shelly Script - MQTT Discovery

Here is, another take how to integrate Shelly devices with HA (gen2 and newer)
You know me I prefer MQTT way, but I’m getting older and lazier to write and maintain manually configured MQTT entities.
At the same time I wanted to dive into Shelly scripting.

I’ve started with the overhaul of one of the example scripts provided by Shelly, registering BLE devices proxied by AC-powered Shellys using MQTT discovery. I have described it over here.

BTW: did you know you can use buttons available in DW BLE as a regular button?

Then, I came up with the idea of a complementary script, which will register hosting devices to MQTT discovery. And here is. So far, limited to following components:

  • switch (can be reported as a light)
  • cover (incl. position and slat if enabled)
  • pm1
  • wifi
  • em, em1
  • emdata, em1data
  • temperature
  • humidity
  • voltmeter (incl. custom formula and units)
  • input (incl. custom formula and units)

I’ve tested the models I own:

  • Shelly 1 gen3
  • Shelly Mini PM gen3
  • Shelly Plus 1PM (gen2)
  • Shelly Plus 2PM (gen2) - as switch, light, cover incl. position and slat support
  • Shelly Pro EM3 (gen2) - both triphase and monophase profiles
  • Addon Plus - with 4xDS18B20, DHT22, binary, analog and voltimeter inputs

However, it will create sensors out of all the Shelies that utilize the components listed above. Here is a compatibility list: shelly-scripts/scripts/mqtt-discovery-self.md at main · michalk-k/shelly-scripts · GitHub

The biggest challenge was to write it in a way not exceeding resources: memory, MQTT queue, CPU stack, etc. Side effects were really unexpected. I was desperate, close to throwing this project into the bin. Finally, I found a way to generate a relatively high amount of data and publish it, keeping the memory footprint low (about 10kB), and mainly not overloading the MQTT component.

See: GitHub - michalk-k/shelly-scripts
for a more detailed description, screenshots, and scripts, of course.

1 Like

I’ve updated this Shelly script, adding support for Shelly Plus Addon, x-sensors (Shelly analog inputs allow defining custom formulas and units) and following Shelly components:

  • cover (incl. position and slat if enabled)
  • humidity
  • voltmeter (incl. custom formula and units)
  • input, binary and analog (incl. custom formula and units)

Now the script triggers Discovery not only on script start, but also on MQTT connect event as well as some Shelly configuration changes (i.e., change to device or channel name).

With the growing number of supported Shelly components, the script was taking more and more memory. Simply put, it deserved refactoring, which cut another 5kB :slight_smile:

1 Like