Shelly Dimmer with MQTT

Hi,

I just got my Shelly Dimmers installed and I want to integrate them with HA using MQTT.
I can’t for the life of me figure out how to change the brightness via mqtt.
I’ve managed to turn it on and off but that’s it.

It is the template things that I don’t quite understand.
This is the config I have so far:

  - platform: mqtt
    name: Shelly Dimmer
    state_topic: "shellies/shellydimmer-XXXXXX/light/0"
    command_topic: "shellies/shellydimmer-XXXXXX/light/0/command"
    brightness_state_topic: "shellies/shellydimmer-XXXXXX/light/0/status"
    brightness_command_topic: "shellies/shellydimmer-XXXXXX/light/0/status"
    state_value_template: "{{ value_json.state }}"
    brightness_value_template: "{{ value_json.brightness }}"
    brightness_scale: 100
    payload_on: "on"
    payload_off: "off"

I can turn on/off the lights in HA but when I change the brightness it just sends the value direclty to shellies/shellydimmer-XXXXXX/light/0/status and not in the JSON format.
The value in the topic is this:

{"ison":true,"mode":"white","brightness":100}

I only need to change the value for brightness but obviously have no idea how to do that.
I understand I need to use the template settings but even after reading through the template docs and Shelly Dimmer documentaion multiple times I still can’t figure it out.

Is there anyone that has this working and knows how to configure this correctly?

Thanks,
-Patric

3 Likes

You can try this :

1 Like

There you go:

platform: mqtt
  schema: template
  name:  Shelly Dimmer
  state_topic: "shellies/shellydimmer-XXXXXX/light/0/status"
  state_template: '{% if value_json.ison %} on {% else %} off {% endif %}'
  command_topic: "shellies/shellydimmer-XXXXXX/light/0/set"
  command_on_template: '{"turn": "on"{% if brightness is defined %}, "brightness": {{(brightness | float * 0.3882 + 1) | round(0)}}{% endif %}}'
  command_off_template: '{"turn": "off"}'
  brightness_template: '{{ (value_json.brightness | float * 2.55) | round(0) }}'

EDIT: Including a small hack, as setting brightness=0 seems to have no effect in current Shelly Dimmer firmware

11 Likes

Thank you very much! It works perfecly :smiley:

1 Like

https://shelly-api-docs.shelly.cloud/#dimmer-sl-overview

If I may ask, why are you using MQTT? The normal Shellys work well with ESPHome and are easily added. Is this different for the dimmers or do you have other reasons?

I’ve never used ESPHome. I like to do everything the “hard way” so I have more control and understanding of what is going on. I use MQTT for all my devices.

1 Like

Hello! I am trying to use the switch entity template above, but my HA keeps telling me:

Invalid config for [switch.mqtt]: [schema] is an invalid option for [switch.mqtt]. Check: switch.mqtt->schema. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/mqtt/

And if i comment this line out, I get the same message on all the template value rows. What am I doing wrong? Shouldnt this work under the “switches” part in the configuration? I have also tried the code under the lights part of the configuration.

Edit: This was the solution. Code should be under lights instead of switch! Sorry for the confusion!

Kind regards J

Ah ok, that makes sense (in some way :smiley: :smiley: :smiley: ). I’d really suggest you take a closer look to ESPHome, especially together with Shellys.

In ESPHome you configure your firmware for the Shelly and flash it afterwards. It is simple and you can control everything, as you are the “maker” of the firmware. Eg. on a Sonoff S20 you can even control the color of the LED ring around the button.

It’s just a suggestion, but you should really try it. :slight_smile:

Hi @ioprev,

I am using your configuration for my Shelly dimmers, however I’m fairly new to mqtt and HA (so please be gentle here :slight_smile:)

The dimmer works well with one exception.

When changing the brightness of the dimmer using a momentary button, the dimmer does not send a payload message back to the MQTT server for several seconds.

It appear that the dimmer sends a default keep-alive message every 30 seconds, and it is in this message that the current brightness is reported.

I would have thought that after changing the brightness on the dimmer, that it would send back a message to the MQTT server immediately.

Is this something that I should take up with Shelly support, or is there anything that you can spot in this config that could assist?

Thanks,
Alister

Hi Alister,

Sorry I don’t have a button on my Shelly Dimmer so at this time I didn’t had the chance to test what happens :slight_smile:

With this configuration, whenever we get a brightness value published in state topic, we update the value on HA respectively.
Looking into their documentation for the MQTT interface, I don’t see any other way they can publish the changes done by button:

Shelly Dimmer/SL publishes on the following topics:

shellies/shellydimmer-<deviceid>/light/0 reports on/оff status
shellies/shellydimmer-<deviceid>/light/0/status reports complete status in JSON
shellies/shellydimmer-<deviceid>/light/0/power reports instantaneous power consumption
shellies/shellydimmer-<deviceid>/temperature reports device temperature in Celsius
shellies/shellydimmer-<deviceid>/temperature_f reports device temperature in Fahrenheit
shellies/shellydimmer-<deviceid>/overtemperature reports overtemperature status
shellies/shellydimmer-<deviceid>/overload reports overload status
shellies/shellydimmer-<deviceid>/loaderror reports true when attached load cannot be analyzed

So they either just omitted this in current firmware, or there is something else, not mentioned in the guide. From our side, we can take a look on a dump of the MQTT communication from Dimmer in case there is something hidden in there :slight_smile:

But best approach I think is for Shelly team to implement this, as all their other devices have this feature.

Thanks for your quick response.
I did have a look at all the communication on Mqtt from the device, however no brightness values seem to be communicated (other than during keep alive status messages every 30 secs).

I will contact Shelly and see what they have to say.

Thanks for your help!

Thanks for code @ioprev it really did help me alot!
Actually it was my first mqtt setup ever.

So, by the help with the code, I now can controll my shelly dimmer i HA - on/off/dimmer.
But I struggle with the wall switch setup.
I would like to be able to dim my light with the wall switch, overriding the mqtt.

But I am not able to dim through my wall switch at all. Even without mqtt connection or HA connection for that matter.

Any ideas?
Not possible?

I have an fibaro dimmer 2, where i was able to do so.
But would really like to do so with the shelly dimmer since its half the price.

Can you turn on/off with the wall switch? And do you use one or two switches?
If you can turn on/off with the switch you should be able to dim as well as long as you have a momentary switch (that bounce back when you push it).
This works even if you use MQTT or not.
So if it doesn’t work I would check the settings on the Dimmer itself or check the wiring.

What’s the meaning of the formula and if statement in the suggested answer?

Thanks; i haven’t tested yet, but think you can save yourself all the math formulas by setting brightness_scale: 100

[EDIT] got a dimmer and tested… unfortunately due to the topics and control, schema: template is required, which doesn’t appear to support brightness_scale which I think is a mistake, but ah well, works with the math templates :smile: thanks!

The shelly dimmer is not easily flashable. The breakout header isnt as its documented and doesn’t have a pin to ground. The easiest way to use the dimmer is to just use the built in mqtt.

1 Like

Almost a necro at 29days, but I was so excited about how easy it was to setup my shelly dimmers using the mqtt discovery script that I had to post about it somewhere. The stock shelly firmware is very impressive, with an obvious nod to HA all local usage in the way the hotpoint, cloud, and mqtt settings are done. Bottom line, you don’t ever have to connect a shelly outside your lan to get it working well with HA via mqtt, and the python script (2nd post in this thread) makes adding them to HA super easy.

Why not esphome/tasmota/etc custom firmware? I use esphome on a ton of devices and I also love it; my esphome arsenal consists of DIY esp12 modules (only built in libs needed), sonoff basic r2’s, sonoff s31’s, shelly 1pm’s, and shelly 2.5’s. They work great and are easy to manage/maintain with esphome. So of course, when I was adding dimmers I was looking for devices with esphome-ability. Unfortunately, this research came up short of a solution (for now). Why?

Dimmers are much more complicated than the others. All of the wifi dimmers I’ve seen so far use a UART from an esp8266 chip to talk to a separate dimmer uC. So to create custom firmware that works with this dimmer iC, we have to do some more significant reverse engineering work to sniff out those serial communications and map them (to re-implement in our custom firmware). To make matters worse, those UART pins are not isolated from mains voltages. So a lot of willing hackers with o-scopes can’t help provide data because they lack some of the required equipment to sniff those lines safely (isolation transformers, HV probes, etc).

Rest assured there are efforts being made to bring dimmers into the esphome/tasmota fold, but it’s a longer windier road than usual. It is much harder than probing traces on a disconnected pcb (that’s all that had to be done to get esphome on the ‘simpler devices’, like a sonoff basic, etc). Soon enough, some proud EE soldier will walk in with the needed capture files, and the devs can fold that into firmware from there.

Until then, the stock shelly firmware and mqtt discovery is IMHO a very acceptable solution. In fact it’s so good I’m almost tempted to say, “if it aint’ broke don’t fix it”. I won’t, because I love following RE success stories, and I want to pat the proud soldiers on their backs. :wink:

Thanks to ioprev and all contributin to.

I strugle with the schema: template, config file test shows:

Invalid config for [switch.mqtt]: [schema] is an invalid option for [switch.mqtt]. Check: switch.mqtt->schema. (See ?, line ?).

As a newbie is there a good traingin / tutorial for the Home Assistant yaml ?
Was not even able to link to a kmx.yaml, looking for examples.

https://github.com/GetUpdate/Home/blob/master/configuration.yaml

I ran into the same error message. It turn out the template is meant to be added to the light group. Not to sensors or switch groups. :slight_smile:

1 Like