Shelly Gen2. Plus MQTT configuration

Erverthing started after I have bought a brand new Shelly Plus RGBW PM for my frontdoor lights.
Very fast I figured out that the current Shelly-Binding is not properly working with newer Shelly devices. But than I remembered that in the beginning we all connected Shelly devices via MQTT. So I decided to give it a try as I really urgently need this functionality in OpenHAB. Again very fast I figured out that the MQTT communication with newer Shelly devices also changed. I guess it is a more modern way to use the MQTT Qeue and I started searching for a solution. I finally made it running but was a little frustrated as I had to collect the information over many Support threads. I found a lot of “almost knowing and copying from other threads solutions” but I never had the feeling that the author really did understand what he/she had to configre or why you had to configure it like that. So at the end I thought my self that a more complete Tutorial could be a good thing to all the people who need currently a solution.

This tutorial will start after you have setted up and configured a mqtt bridge in OpenHAB
I will start with the very specific configuration of my Shelly Plus RGBW PM and will at the end be more generic. That’s at least the plan.

  1. Configure Your Shelly Device
  • enable MQTT
  • use your own MQTT prefix or use the default one you only have to know it!!

2. Add a generic MQTT thing to OpenHAB


  • Choose your bridge
  • Create the thing

3. Configure your MQTT thing

  • Open the MQTT thing and open the code page

The header should look similar to the one here (some other ids)

UID: mqtt:topic:4ce2582d:shellyplusrgbw
label: Shelly Plus RGBW
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:4ce2582d

The channels need configuration. Please keep in mind, that #shellymqtttopic needs to be replaced with whatever you have choosen in your Shelly MQTT configuration above as MQTT prefix

channels:
  - id: LEDSwitch
    channelTypeUID: mqtt:switch
    label: LED Schalter
    configuration:
      commandTopic: #shellymqtttopic/rpc
      formatBeforePublish: '{"id":1,"src":"#shellyresponse","method":"Light.Set","params":{"id":1,"on":%s}}'
      stateTopic: #shellymqtttopic/events/rpc
      transformationPattern:
        - JSONPATH:$.params.light:1.output
      off: "false"
      on: "true"

Find explainations to the MQTT settings here OpenHAB Generic MQTT Thing

Let’ explain the configuration: of the channels.

configuration value description
commandTopic: #shellymqtttopic/rpc This is the MQTT queue where the request is sended if you want to change any item off the thing
formatBeforePublish: ‘{“id”:1,“src”:“shellyresponse”,“method”:“Light.Set”,“params”:{“id”:1,“on”:%s}}’ This is single quoted as the mqtt answer needs double quotes
stateTopic: #shellymqtttopic/events/rpc This is the MQTT queue where you get the information of the thing from
transformationPattern: - JSONPATH:$.params.light:1.output The answer of the shelly thing is a json and to separate values from the anwer json you have JSONPATH to be installed
off: “false” as this is a switch there is on and off which is true and false on the shelly
on: “false”

Let’s understand the formatBeforePublish: and JSONPATH:

  • formatBeforePublish:
    • id : you can choose a numeric value here. It is only for identification
    • src : this is the MQTT prefix (src/rpc) where you will get the answer back when you send a message to your shelly device. That’s why you can choose your own name. For us this only has a function if you want to debug why the shelly is not doing what you have told it. This is in our case normaly not used
    • method : This parameter took me the most time to understand. I found a lot of examples where this value was set to “Switch.Set” and I never got any reaction on the Shelly. The trick is, this value depends on the device and furthermore on the devicemode!! This is decribed on the Shelly documentation (Shelly Light) but not very clearly to me.
    • params : This is the json you will send to your shelly device. Which parameter you can set depends again on the device or the devicemode. There is simple trick to get easily what your shelly is supporting. It also helps you to figure out which method you have to use. In your browser type in ip-address_of_your_shelly_device/rpc/Light.GetStatus?id=0. If it is a switch use Switch.GetStatus?id=0, an RGBW uses RGBW.GetStatus?id=0 etc. If you use the wrong method you will get as answer
      ShellyNoHandler.
      If you do it right you get the status of your device
      ShellyAnswer
  • JSONPATH:
    • As an answer you get a json String {"src":"shellyplusrgbwpm-08a6f7732ae8","dst":"shellyplusrgbwhaustuer/events","method":"NotifyStatus","params":{"ts":1739093012.22,"light:0":{"id":0,"brightness":69,"output":true,"source":"WS_in"}}}.
      We are only interested on the part “param”. If you want retrieve the output value with JSONPATH we have to start with $.params an. Under params you find the next node light:1 which we add with a dot $.params.light:1. output is then the next node under light:1 which we also add via dot for the final result $.params.light:1.output
    "params": {
        "light:0": {
            "brightness": 3,
            "id": 0,
            "output": true,
            "source": "WS_in"
        },
        "ts": 1739092905.97
    }

3.1 Configure the brightness channel

  - id: LEDBrightness
    channelTypeUID: mqtt:dimmer
    label: LEDs Helligkeit
    configuration:
      min: 0
      formatBeforePublish: '{"id":1,"src":"shellyresponse","method":"Light.Set","params":{"id":1,"brightness":%s}}'
      max: 100
      commandTopic: #shellymqtttopic/rpc
      step: 1
      stateTopic: #shellymqtttopic/events/rpc
      transformationPattern:
        - JSONPATH:$.params.light:1.brightness

5. Additional Remark
I do have a Shelly Plus RGBW PM and this one can either be driven in RGBW mode or 4LED switch mode. The method: you need in the output channel is different depending on the mode:

mode method link
RGBW RGBW.Set RGBW
4 LED switch Light.Set Light

Please if you think, that this tutorial is still a bad description for the configuration write your questions in the comments below so that we can improve this tutorial.

1 Like

Thank you this is helpful. Since the OH Shelly AddOn is running a DoS attack on all my Shelly devices I will need to switch to MQTT.

Do you have a thing file? (I dont like editing YAML in general and had my problems with coping channels in the frontend - even inside the YAML)
Did you try out the MQTT Control channels? MQTT | Shelly Technical Documentation
It should work for “switches” but I was wondering if somebody has an example for something else like a dimmer.

The hardcoded JSON strings leads to “quoting-hell” and feels a bit awkward (but works).

The reference you used in your post is a different way to use MQTT as I have it decribed. I’m using RPC request vi MQTT (That is why you need the settings on the Shelly as I described on my first picture in the tutorial.
Unfortunately I don’t use a thing file anymore, so I can not provide you a thing file from my OpenHAB, but will try create one for you very soon.
In case of a dimmer, in the RPC variant you need as method: a Light.
Here I tried to configure a shelly Light device as thing in a thing file. I can not test it, as I mentioned above I’m not using thing files anymore.

Bridge mqtt:broker:shellyBroker [ host="yourMQTThost", secure=false, username="yourMQTTuser", password="yourMQTTuserpassword" ]
{
    Thing mqtt:topic:kitchenlight "kitchenlight"
    {
        Channels:
            Type dimmer : light_dimmer  [ stateTopic = "#shellymqtttopic/events/rpc", transformationPattern="JSONPATH:$.params.light:1.brightness", commandTopic="#shellymqtttopic/rpc", formatBeforePublish='{"id":1,"src":"shellyresponse","method":"Light.Set","params":{"id":1,"brightness":%s}}', min="0", max="100", step="1"  ]
    }
}

Thank you so much! Its always nice to have an example. My question was more like “if you already have a thing file at hand”. But this helps.

I was looking into another shelly MQTT Thread - using the shelly mqtt command topic. They “mapped” the “on” value into an “on” command

I think I will use the RPC via MQTT.