Openhab Shelly Pro 4PM MQTT

Hi!
Here is the :grinning: SOLUTION :grinning: for the Shelly Pro 4PM with MQTT:
Hope this helps

Example for the first (0) Relay:

UID: mqtt:topic:7d3c004212:Shelly4_Heizpatrone
label: Shelly4_Heizpatrone
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:7d3c004212
channels:
  - id: Relay0
    channelTypeUID: mqtt:switch
    label: Relay0
    description: ""
    configuration:
      commandTopic: shellies/shellypro4pm-84cca87d86bc/rpc
      formatBeforePublish: '{"src":"MQTT","method":"Switch.Set", "params":{"id":0,"on":%s}}'
      stateTopic: shellies/shellypro4pm-84cca87d86bc/status/switch:0
      transformationPattern: JSONPATH:$.output
      off: "false"
      on: "true"
  - id: Relay0_Volt
    channelTypeUID: mqtt:number
    label: Relay0_Volt
    description: null
    configuration:
      unit: V
      formatBeforePublish: "%.4f"
      stateTopic: shellies/shellypro4pm-84cca87d86bc/status/switch:0
      transformationPattern: JSONPATH:$.voltage
  - id: Relay0_Watt
    channelTypeUID: mqtt:number
    label: Relay0_Watt
    description: ""
    configuration:
      unit: W
      formatBeforePublish: "%.4f"
      stateTopic: shellies/shellypro4pm-84cca87d86bc/status/switch:0
      transformationPattern: JSONPATH:$.apower
  - id: Relay0_Ampere
    channelTypeUID: mqtt:number
    label: Relay0_Ampere
    description: null
    configuration:
      unit: A
      formatBeforePublish: "%.4f"
      stateTopic: shellies/shellypro4pm-84cca87d86bc/status/switch:0
      transformationPattern: JSONPATH:$.current
3 Likes

Hi,

Thank you for sharing. But in my opinion it’s not a solution but a workaround for a (product) binding that seems to be EOL.

The shelly (binding) was the perfect wifi solution without needing any “bridging” components, but now i’ll be needing an extra mqtt service for bridging between openhab and shelly (also without discovery). Yes, I bougt pro products some days ago and was stunned that the pro series was not (yet) supported.

In my opinion using mqtt is 4 steps back in time, especially with a PRO series (needs a lot of manual configuration) which offers direct connections (utp) for more reliability (and now we would shift another service in between).

If it’s so different than the regular shelly productline, then couldn’t there be a shelly PRO binding added? Just a thought.

Hi all,
Thank you for this workaround.
I’ve got the problem that it doesn’t work in rules.
Do you have a solution for this problem?
Best regards
Bany

You’ll have to be a bit more specific.
What “it”? What does or does not happen that you did not expect? What rule?

Hi!
That is not a rule!
This is a code for MQTT Binding.
You must install a MQTT Binding.
Then go to things, and insert this under the Label “Code”.
You have to adjust the link (shellies/shellypro4pm-84cca…/rpc) to your own shelly.

I found the error: spelling mistake
thanks for your help

Hi,
This work fine but as my stateTopic is little different i had to make some changes.
I still get this error on every switch:

Executing the JSONPATH-transformation failed: Invalid path ‘$.params.switch:0.output’ in ‘{“src”:“shellypro4pm-083af27b8cd8”,“dst”:“shelly4pm/events”,“method”:“NotifyStatus”,“params”:{“ts”:1646937780.08,“switch:0”:{“id”:0,“aenergy”:{“by_minute”:[0.000,0.000,0.000],“minute_ts”:1646937779,“total”:0.000}}}}’

I can not find out where the src is defined as my topic is not shellypro4pm but shelly4pm
Any help would be welcome.
My code is changed like this

UID: mqtt:topic:9162e145cb:fad70a20ca
label: Shelly4PM
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:9162e145cb
channels:

  • id: Relay2
    channelTypeUID: mqtt:switch
    label: Relay2
    description: null
    configuration:
    commandTopic: shelly4pm/rpc
    formatBeforePublish: ‘{“src”:“MQTT”,“method”:“Switch.Set”, “params”:{“id”:2,“on”:%s}}’
    stateTopic: shelly4pm/events/rpc
    transformationPattern: JSONPATH:$.params.switch:2.output
    off: “false”
    on: “true”
  • id: Relay2_Volt
    channelTypeUID: mqtt:number
    label: Relay2_Volt
    description: null
    configuration:
    unit: V
    formatBeforePublish: “%.4f”
    stateTopic: shelly4pm/status/switch:2
    transformationPattern: JSONPATH:$.voltage

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
image
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"  [
       stateTopic="shelly2mqtt/shelly4PM_Sauna/status/switch:0", 
       commandTopic="shelly2mqtt/shelly4PM_Sauna/rpc",
       formatBeforePublish="{\"src\":\"MQTT\",\"method\":\"Switch.Set\", \"params\":{\"id\":0,\"on\":%s}}",
       transformationPattern="JSONPATH:$.output",
       on="true", 
       off="false"
       ]

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" {
Channels:
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 shelly.cloud 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.

.things:

Bridge mqtt:broker:mosquitto "_Mosquitto MQTT Broker" [
    host="xx.xx.xx.xx (Enter yout IP here)", 
//    secure=false,
    port=1883,
    clientID="OpenHAB"
//    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) {
    Channels:
// Relais-Status
        Type switch : Switch0 "_Relais 1: Schalter" [
            commandTopic="EG_Flur_Shelly4Pro/events/rpc",
            formatBeforePublish="'{\"src\":\"MQTT\",\"method\":\"Switch.Set\",  \"params\":{\"id\":0,\"on\":%s}}'",
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.output",
            on="true",
            off="false"
        ]

// Apower? Watt lt. Beschreibung
        Type number : Watt0 "_Relais 1: Watt aktuell" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.apower"
        ]


// Voltage
        Type number : Voltage0 "_Relais 1: Volt" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.voltage"
        ]

// current? Ampere
        Type number : Ampere0 "_Relais 1: Ampere" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.current"
        ]

// pf? Power Faktor? Last measured power factor lt. Shelly
        Type number : PF0 "_Relais 1: PowerFaktor??? ist was?" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.pf"
        ]

// aenergy (eigenes Array)
// ... total (kwh Verbrauch in Wh Watt Stunden?)
        Type number : Usage0 "_Relais 1: kwh Total" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.aenergy.total"
        ]


// ... byminute (letzten 3 Minuten?
        Type number : Usage0_Minute0 "_Relais 1: kwh current Minute" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.aenergy.by_minute[0]"
        ]
        Type number : Usage0_Minute1 "_Relais 1: kwh last Minute" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.aenergy.by_minute[1]"
        ]
        Type number : Usage0_Minute2 "_Relais 1: kwh prelast Minute" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.aenergy.by_minute[2]"
        ]

// ... minute_ts?
        Type number : Usage0_Minute0_Timestamp "_Relais 1: kwh Minute Timestamp" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.aenergy.minute_ts"
        ]

// ... Transformed in DateTime
        Type datetime : Usage0_Minute0_DateTime "_Relais 1: kwh Minute DateTime" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JS:MQTT_ts_to_datetime.js"
        ]

// ... temperature (array)
//     ...tC (Celcius)
        Type number : Temperature0C "_Relais 1: Temperatur in °C" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.temperature.tC"
        ]

//     ...tF (Fahrenheit)
        Type number : Temperature0F "_Relais 1: Temperatur in °F" [
            stateTopic="EG_Flur_Shelly4Pro/status/switch:0",
            transformationPattern="JSONPATH:$.temperature.tF"
        ]
}

.items


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)

(function(i){
    var parsed = JSON.parse(i);
    var ts = new Date(parsed.aenergy.minute_ts * 1000);
    return (ts.toISOString());
})(input)```