Openhab Shelly Pro 4PM MQTT

You have a mistake in your stateTopic.
Look in your shelly configuration on the MQTT Definition.

For example:
My Shelly:
stateTopic: shellies/shellypro4pm-84cca87d86bc/status/switch:0

Your Shelly:
stateTopic: xxx/xxx/status/switch:0
maby can be:
stateTopic: xxx/status/switch:0

Hi, Thank you for help.
My Shelly is configured like this
and my openhab channel like this:

I do not find out where the shellypro4pm-… came from

Ok, another try:
Look at my transformation pattern.
It looks like you have my old version copied.

you mean changing it to

transformationPattern: JSONPATH:$.output
It does not change anything :roll_eyes:

my MQTT Explorer structure is like this:

Your state topic is also wrong:
Look at the whole code

      commandTopic: shelly4pm/rpc
      stateTopic: shelly4pm/status/switch:0
      transformationPattern: JSONPATH:$.output
1 Like

Thank you., it’s working. Great

just FYI: If you still like to add things on file base, the code for an switch looks like this:

     Type switch: Shelly4PM_Sauna_SW0_state    "SW0 state"  [
       formatBeforePublish="{\"src\":\"MQTT\",\"method\":\"Switch.Set\", \"params\":{\"id\":0,\"on\":%s}}",

Hi All.
I also fight with this dreadful new shelly (they broke it, integration wise the old API was so much easier)
I’m still in OH 2.5 (not broken, don’t fix it rule apply) but need to add this switch to this old setup.

What I see is that when defining a thing in a file I can’t extract the state of the switch properly

Thing topic shellypro4pm-sauna "shellypro4pm-sauna" {
Type string : relay_0_state "relay_0_state" [ stateTopic="shellypro4pm-sauna/status/switch:0", transformationPattern="JSONPATH:$.output",on="true",off="false"]		

The result is
2022-04-12 17:10:50.578 [vent.ItemStateChangedEvent] - sauna_relay_0_state changed from true to false

So it looks like transformationPattern="JSONPATH:$.output" is working fine but then the mapping is not applied. That is already a debug code (assign to the string) as I in the end I want to assign that to switch that is not taking true as value.

I’m so desperate that I think about having a this helper item and do that in the rule.
Is there a diffrence how that is handled in OH3.x ?? as it looks like a copy of the @schmieeed solution for a switch, but does not work in OH2.5

Thanks for any input, i’m on the end of my rope here

Maybe you can help:
I have openhab with a lot of stuff, but yes without any MQTT on an ubuntu machine.
I just installed a Shelly4Pro, now saw: Not supported by shelly binding. In openhab allways “configuration pending”, Webinterface works.

I saw the workaround with MQTT, but I’m not familiar with it.

  • You wrote install MQTT binding needed: Only the MQTT Binding in OH 3.3? Or those Actions too?
  • Do I need to install additional stuff like mosquito, or ist the openhab MQTT binding enough talking to shelly directly?
  • All my configurations are in files (.things, *items) until now. Do you have an example including Watt, Voltage, etc., so byond the Switch example in post 15?

For the first two questions:

  • Yes, install the binding. It will come with any relevant actions.
  • Yes, you need to install an MQTT broker outside of openHAB - the binding does not come with one.

I wrote this tutorial which covers both of the above:

As you’re sticking with files, swap step 3 in the above tutorial for step 2.a.i in the tutorial below:

I will give up.

Installes mosquitto, which looks like up and running, logs showing client logs in and out (EG at reboot of device).

I configured bridge and thing both shown online in openhab. But not able to successfully create a Thing / item combination doing anything.

I looked for API-Documentation at too. None of the HTTP API-Endpoints seems to work. So I guess, the firmware 10.3 of shelly is broken or the Shelly just produces gargabe! :frowning:
Any working example would be much appreciated.

Because I got it work now, here my example (maybe it helps others).

  • installed mosquitto on ubuntu out of the box (no additonal configuration yet)
  • installes mqtt binding in openhab (only the binding, no actions)
  • Hint for checking: iOS App MQTTAnalyzer was very helpful for me to control Shelly sends correctly and to find the structure my shelly uses in MQTT. Looks like they changed a lot.


Bridge mqtt:broker:mosquitto "_Mosquitto MQTT Broker" [
    host="xx.xx.xx.xx (Enter yout IP here)", 
//    secure=false,
//    username="user MQTT",
//    password="PASSWORD MQTT"

// Thing für Device-Status fehlt noch (TODO)

Thing mqtt:topic:mosquitto:EG_Shelly4Pro_R1 "_EG-Flur Shelly: Relais 1 - Solar" (mqtt:broker:mosquitto) {
// Relais-Status
        Type switch : Switch0 "_Relais 1: Schalter" [
            formatBeforePublish="'{\"src\":\"MQTT\",\"method\":\"Switch.Set\",  \"params\":{\"id\":0,\"on\":%s}}'",

// Apower? Watt lt. Beschreibung
        Type number : Watt0 "_Relais 1: Watt aktuell" [

// Voltage
        Type number : Voltage0 "_Relais 1: Volt" [

// current? Ampere
        Type number : Ampere0 "_Relais 1: Ampere" [

// pf? Power Faktor? Last measured power factor lt. Shelly
        Type number : PF0 "_Relais 1: PowerFaktor??? ist was?" [

// aenergy (eigenes Array)
// ... total (kwh Verbrauch in Wh Watt Stunden?)
        Type number : Usage0 "_Relais 1: kwh Total" [

// ... byminute (letzten 3 Minuten?
        Type number : Usage0_Minute0 "_Relais 1: kwh current Minute" [
        Type number : Usage0_Minute1 "_Relais 1: kwh last Minute" [
        Type number : Usage0_Minute2 "_Relais 1: kwh prelast Minute" [

// ... minute_ts?
        Type number : Usage0_Minute0_Timestamp "_Relais 1: kwh Minute Timestamp" [

// ... Transformed in DateTime
        Type datetime : Usage0_Minute0_DateTime "_Relais 1: kwh Minute DateTime" [

// ... temperature (array)
//     ...tC (Celcius)
        Type number : Temperature0C "_Relais 1: Temperatur in °C" [

//     ...tF (Fahrenheit)
        Type number : Temperature0F "_Relais 1: Temperatur in °F" [


Switch Shelly_Test_Switch	"_Test Shelly"								(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Switch0" }
Number Shelly_Test_Watt		"_Test Shelly W"							(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Watt0" }
Number Shelly_Test_Volt		"_Test Shelly V"							(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Voltage0" }
Number Shelly_Test_Ampere	"_Test Shelly A"							(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Ampere0" }
Number Shelly_Test_PF		"_Test Shelly Power Faktor?"						(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:PF0" }
Number Shelly_Test_kwh		"_Test Shelly kwh? Wh?"							(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Usage0" }
Number Shelly_Test_kwh_M0	"_Test Shelly kwh? Wh? aktuelle Minute"					(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Usage0_Minute0" }
Number Shelly_Test_kwh_M1	"_Test Shelly kwh? Wh? letzte Minute"					(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Usage0_Minute1" }
Number Shelly_Test_kwh_M2	"_Test Shelly kwh? Wh? vorletzte Minute"				(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Usage0_Minute2" }
Number Shelly_Test_kwh_M0_TS	"_Test Shelly kwh? Wh? Timestamp"					(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Usage0_Minute0_Timestamp" }
DateTime Shelly_Test_kwh_M0_DT	"_Test Shelly kwh? Wh? DateTime"					(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Usage0_Minute0_DateTime" }
Number Shelly_Test_Temperature	"_Test Shelly [%.1f °C]"						(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Temperature0C" }
Number Shelly_Test_TemperatureF	"_Test Shelly [%.1f °F]"						(gAll, gHistoryCh)		{ channel="mqtt:topic:mosquitto:EG_Shelly4Pro_R1:Temperature0F" }

MQTT_ts_to_datetime.js (directory /etc/openhab/transform)

    var parsed = JSON.parse(i);
    var ts = new Date(parsed.aenergy.minute_ts * 1000);
    return (ts.toISOString());


i tried to get mqtt also working. Switching Channels works fine, but Openhab doesn’t recognise the status yet. So when i switch in shelly cloud, openhab doesn’t get the new state.

Here is my mqtt client code:

UID: mqtt:topic:76310a1e3f:b38bdd3f41
label: Bewaesserung
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:76310a1e3f
  - id: BewasserungCourt1
    channelTypeUID: mqtt:switch
    label: BewasserungCourt1
    description: ""
      retained: true
      qos: 2
      formatBeforePublish: '{"id":123,"src":"chan1","method":"Switch.Set",
      commandTopic: shellypro4pm-083af2bf3250/rpc
      stateTopic: shellypro4pm-083af2bf3250/events/rpc
      transformationPattern: REGEX:(.switch:0.)∩JSONPATH:$.params.switch:0.output
      off: "false"
      on: "true"
  - id: BewasserungCourt2
    channelTypeUID: mqtt:switch
    label: BewasserungCourt2
    description: ""
      retained: true
      qos: 2
      formatBeforePublish: '{"id":2,"src":"light","method":"Switch.Set", "params":{"id":1,"on":%s}}'
      commandTopic: shellypro4pm-083af2bf3250/rpc
      stateTopic: shellypro4pm-083af2bf3250/events/rpc
      transformationPattern: REGEX:(.switch:1.)∩JSONPATH:$.params.switch:1.output
      off: "false"
      on: "true"

michael, you made my day…uhhh, how complicate this shelly staff with mqtt, since pro versions…? i’m woundering how to find out the right code and syntax. can you explain, how you found the right path? would be helpfuel to me for future issues…thanks!

I used iOS app MQTT analyzer for this

Hi there,

did anybody already configure the inputs to be read out if configured to detached on shelly channel input/output configuration?

I’ve tried the following which does not seem to work:

  • id: Wohn_Input
    channelTypeUID: mqtt:switch
    label: Wohnzimmer_Input
    description: null
    stateTopic: shellies/shellypro4pm-30C6F783568C/status/input:0
    transformationPattern: JSONPATH:$.state

I’ve just checked again and realised that the other state read outs, like posted as a sample earlier in this this threat do not work either, such as voltage for example. Did anybody test anything else then the switches?

Solved it, sorry. Forgot to install the JSON Transform add on

Hi @htttito .
Just came accros your post.
Are you getting status updates on your channel if the switch gets initiated by any other source than openHAB (e.g. Shelly WebGUI or touchscreen on the device)?

Your stateTopics looks off to me, should rather be something like this to work properly - correct? :

stateTopic: shellypro4pm-84cca87d86bc/events/rpc
transformationPattern: REGEX:(.*\"switch:0\".*\"output\".*)∩JSONPATH:$.params.switch:0.output

I’m using a thing file:

Type switch: relay1   "Relais"   [stateTopic="shellypro4pm-c8f09e87eec8/events/rpc", transformationPattern="REGEX:(.*\"switch:0\".*\"output\".*)∩JSONPATH:$.params.switch:0.output", 
                                  commandTopic="shellypro4pm-c8f09e87eec8/rpc", formatBeforePublish="{\"id\":0, \"src\":\"openHAB\", \"method\":\"Switch.Set\", \"params\":{\"id\":0,\"on\":%s}}", on="true", off="false"]