Shelly Gen 2 (plus and pro) using MQTT

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

All manual config was dropped on my side in favour of the shelly mqtt discovery you can find in HACS

This was driving me nuts, but mayne this helps.
This is my config:

mqtt:
  switch: 
    - name: "MQTT Shelly Plug Dig Uno House WLED Switch"
      unique_id: "mqtt_shelly_plug_dig_uno_house_wled_switch"
      #state_topic: "shellies/shellyplugus-083af201867c/events/rpc"
      state_topic: "shellies/shellyplugus-083af201867c/status/switch:0"
      state_on: true
      state_off: false
      #value_template: "{{ value_json['params']['switch:0']['output'] }}"
      value_template: "{{ value_json['output'] }}"
      command_topic: "shellies/shellyplugus-083af201867c/rpc"
      payload_on: '{"method":"Switch.Set", "params":{"id":0,"on":true}}'
      payload_off: '{"method":"Switch.Set", "params":{"id":0,"on":false}}'
      qos: 1
      # optimistic: false
      # retain: false

I do have “Generic status update over MQTT” enabled on the Shelly. This configuration should eliminate the log warnings. But re the switch being “unknown,” this is how I was able to reproduce the issue I think you’re describing. When you create the yaml, reload the MQTT entries (Developer Tools, YAML, click on Manually configured MQTT entries). when you do this, you’ll note that the switch goes “unknown.” But after about a minute later, it seems that the shelly pushes out an MQTT status message, and HASS does pick up the true state of the device. Try it.

1 Like

I think I figured it out - the configuration yaml is correct there are two other things to be mindful of.

First, make sure on you Shelly configuration under MQTT
The checkbox of Generic status update over MQTT is ticked, otherwise, the status update of the switch is not showing up in the state topic.

Second, what happend to me multiple times is, if you store the config sometimes the password disappears. If that is the case you need to re-paste the password again. This is an annoying bug in the Shelly web UI. You can verify if it works of the small status icon on the top says “connected”.

Hello. I have the shelly plug us with the bluetooth. Does anyone knows the url that outputs raw json? My other version of shelly products are something like this

192.168.1.75/status

But this gen2 plug isn’t the same.

192.168.1.76/status = Not Found

Update: I have found the answer. The correct url is

http://192.168.1.76/rpc/shelly.getstatus

sensors:

sensor:
  - platform: rest
    name: shelly Plug Status
    json_attributes_path: "$.switch:0"
    json_attributes:
     - apower
     - voltage
     - current
     - aenergy
     - temperature
    resource: http://192.168.1.76/rpc/shelly.getstatus
    value_template: "{{ value_json.sys.uptime }}"
    scan_interval: 300

  - platform: template
    sensors:
      shelly_plug_apower:
        value_template: "{{ state_attr('sensor.shelly_plug_status', 'apower') }}"
        unit_of_measurement: "kWh"        
      shelly_plug_voltage:      
        value_template: "{{ state_attr('sensor.shelly_plug_status', 'voltage') }}"
        unit_of_measurement: "V"
      shelly_plug_current:      
        value_template: "{{ state_attr('sensor.shelly_plug_status', 'current') }}"
        unit_of_measurement: "A"
      shelly_plug_tenergy:      
        value_template: "{{ state_attr('sensor.shelly_plug_status', 'aenergy').total }}"
        unit_of_measurement: "kWh"
      shelly_plug_temperature:      
        value_template: "{{ state_attr('sensor.shelly_plug_status', 'temperature').tC }}"
        unit_of_measurement: "C"


Hello. Does your aeneragy: total seems accurate. Mine is seems too high.