Shelly Gen 2 (plus and pro) using MQTT

Hello,

i’m trying to configure the MQTT light for my shelly 1 pm PLUS.

This new models don’t use topics for everything, instead they have a single topic with a json payload…

this is what i have done so far but i am not satisfied…

could you please help me out ?

my biggest problem is that i can’t get the proper state out of the shelly… the state is listed under the " json_attributes_topic: shellies/wardrobe-lamp/status/switch:0 " specifically as {{ value_json.output }} returning true|false …

i can make something show up with “optimistic” but that’s not correct… my issue is that payload_on and payload_off are sent to a different topic, and the result/status is received on another one (as json) so “state_value_template” seems not to work properly as it should match payload_on/payload_off

This is my current light set up. My second step of course, is to take the other sensor data from the attributes and create sensors for energy, temperature, voltage and power

- platform: mqtt
  name: wardrobe-test
  payload_on: '{"id":1, "src": "homeassistant/shelly/wardrobe-lamp", "method": "Switch.Set", "params":{"id":0,"on":true}}'
  payload_off: '{"id":1, "src": "homeassistant/shelly/wardrobe-lamp", "method": "Switch.Set", "params":{"id":0,"on":false}}'
  qos: 1
  state_topic: shellies/wardrobe-lamp/status/switch:0
  state_value_template: "{{ value_json.output }}"
  optimistic: true
  command_topic: shellies/wardrobe-lamp/rpc
  json_attributes_topic: shellies/wardrobe-lamp/status/switch:0

this is how the status topic json looks like, and how the attributes are showed in HA

{
	"id": 0,
	"source": "MQTT",
	"output": true,
	"apower": 58.085,
	"voltage": 240.700,
	"aenergy": {
		"total": 5.688,
		"by_minute": [48.402, 0.000, 0.000],
		"minute_ts": 1634547641
	},
	"temperature": {
		"tC": 46.7,
		"tF": 116.1
	}
}

image

I’m running into the same problem - did you work out a configuration that is working?

Yes, you can find my gist and my git repository which includes an example of the Shelly config I use :

2 Likes

You’re an absolute gentleman!

I was just a tiny bit off with my conf, this has no doubt saved me quite some headscratching.

How do you configure the prefix in the mqtt settings on the device?
And how do you retrieve the availability status of the device in home assistant?

Check my repository, everything should be there. Including screenshot of the Shelly configuration itself

I did and have used your config but the availability didnt work atleast for me so i added the code below at the end of your config for the switch and that made it work for me

  availability:
      - topic: shellies/shellyplus1-XXXX/online
        payload_available: 'true' 
        payload_not_available: 'false' 

you might actually be right, availability isn’t there in the templates i uploaded…

on the git repository though, you definitely have the screenshots where you see how to configure the shelly and the mqtt prefix

Yes i saw that, and that was the same way i configured mine to, was curious if you hade configured them some other way.

Do you know how i could set the online message to be retained?
The problem j have right now when using the availability option is when i restart my home assistant the shelly goes unavailable until i restart my shelly.

Can I ask you why you would use a MQTT broker instead of the official integration (which I believe uses CoAP/CoIOT)?
I’m asking because I am about to install many Shelly 1 Plus myself and looking for the best solutions.

Also asked here

personally for a few reasons.

  • the official integration never really worked for me
  • i have everything running on mqtt and will continue to run without HA
  • the less integration i run, the less stuff is likely to break
  • i can run Highly Available MQTT broker, i can’t run highly available HA yet
  • it’s easier for me to setup firewall rules
  • also in HA i have 1 integration to configure (just mqtt with autodiscovery) that shows me all the devices instead of 1 integration per device
1 Like

That’s an interesting take on it, I should probably consider it as well. What I like most is the high availability for the broker.
On the other hand I know that my automations will reside in HA, so even if the broker is still functioning in the case HA goes down, it won’t be able to do much.

One main reason to use MQTT instead of the official integration is that for Next Gen it does not support password protection. So you leave you device insecure for every one in same network or whole internet if you have also enabled cloud or device has access to internet.

Does anyone mind posting the working mqtt light text, as the GitHub link above isn’t working. Thanks!

You Can use a light platform switch.

I removed the above link because it’s no longer needed. Shelly mqtt autodiscovery for gen 2 is available on HACS

I’ve got the Shelly 1 Plus PM set up as switch, but want to set it up as a light -

switch:
  - platform: mqtt
    name: "Porch light"
    state_topic: shellies/porch_light/status/switch:0
    value_template: "{{ value_json.output }}"
    state_on: true
    state_off: false
    command_topic: shellies/porch_light/rpc
    payload_on: '{"id":1, "src": "homeassistant/shellies/shellyplus1pm-XXXX", "method": "Switch.Set", "params":{"id":0,"on":true}}'
    payload_off: '{"id":1, "src": "homeassistant/shellies/shellyplus1pm-XXXX", "method": "Switch.Set", "params":{"id":0,"on":false}}'
    retain: false

However the MQTT light template doesn’t allow state_on and state_off - I know can also set up automatically, but have quite a few other Shelly devices set up manually, so would like to use the same approach if possible? Thanks!

I just did this

I’m also at a loss for how to integrate my Shelly Pro 1 relays. I can control the relay but I can’t see if it online or offline or the sate of the relay.

switch:
  - name: "Shelly Pro1"
    unique_id: "shellypro1-30c6f7849e14"
    command_topic: "shellypro1-30c6f7849e14/rpc"
    payload_on: '{"id": 1, "src":"homeassistant", "method": "Switch.Set", "params": {"id": 0, "on": true}}'
    payload_off: '{"id": 1, "src":"homeassistant", "method": "Switch.Set", "params": {"id": 0, "on": false}}'
    state_topic: shellypro1-30c6f7849e14/events/rpc
    value_template: "{{ value_json.output }}"
    state_on: true
    state_off: false  
    availability:
      - topic: shellypro1-30c6f7849e14/online
        payload_available: 'true' 
        payload_not_available: 'false' 

The repository decribed above seems to be offline.

For future reference, I have a working setup now as below. There are also other sensor values you could extract, it would work in a similair way.

mqtt:
   switch:
      - name: "Garage: Heat pump switch"
        state_topic: "garage-heat-pump-switch-pm/status/switch:0"
        value_template: "{{ value_json.output }}"
        state_on: true
        state_off: false
        command_topic: "garage-heat-pump-switch-pm/rpc"
        payload_on: '{"id":1, "src": "homeassistant", "method": "Switch.Set", "params":{"id":0,"on":true}}'
        payload_off: '{"id":1, "src": "homeassistant", "method": "Switch.Set", "params":{"id":0,"on":false}}'
        optimistic: false
        qos: 1
        retain: false
    sensor:
      - name: "Garage: Heat pump switch temperature"
        unique_id: 4ca71dd5-645d-48e5-a387-a655cc7dd42e
        state_topic: "garage-heat-pump-switch-pm/status/switch:0"
        value_template: "{{ value_json.temperature.tC }}"
        unit_of_measurement: "°C"
        device_class: temperature
    
      - name: "Garage: Heat pump switch current power"
        unique_id: 44f6d6de-be45-4697-8ff9-882fae91c6a2
        state_topic: "garage-heat-pump-switch-pm/status/switch:0"
        value_template: "{{ value_json.apower }}"
        unit_of_measurement: "W"
        device_class: power
    
      - name: "Garage: Heat pump switch total power"
        unique_id: 44f6d6de-be45-4697-8ff9-882fae91c6a1
        state_topic: "garage-heat-pump-switch-pm/status/switch:0"
        value_template: "{{ value_json.aenergy.total }}"
        unit_of_measurement: "W"
        device_class: power