Shelly MQTT (looking for templates)

Hello,
I would like to integrate some Shelly devices using MQTT.
Do you know if there are any templates available for Home Assistant?

I have searched both the forum and GitHub, but I have found very little, and I am not sure if the configurations are complete (do the exact thing that the official integration do)

I have a few Shelly 2.5 EM, a Shelly EM, and one Shelly 4PM Pro.

It would be great if this post will become a little repository of all templates to easily integrate these devices with MQTT

I don’t know of any, but creating template sensors for the stuff Shellys report should be easy enough - they use MQTT topics ‘shellies’ and ‘shelly’ if I recall; Shelly’s developer documentation online is pretty good.
There’s a page in the HA docs on MQTT sensors; I can share some examples of MQTT sensors for other things if those would help. To command a Shelly from HA, the MQTT switch should work. Anything more complex, there’s the mqtt.publish service.
Out of interest, why do you want to use MQTT rather than the HA Shelly integration?

Unsure what is wrong with the shelly native integration?

Yes if you can provide some example you used it would be nice

@nickrout
Frankly speaking I don’t know why people have to reply to questions if they don’t have an answer…

It would be like if in a car forum and an user asks for some information and an user reply: “I don’t know why people don’t uses bikes…”

Anyway… :roll_eyes:
I am having issue with the integration, every time the wifi goes down I need to reload the integration and I am tired to do that. That is why, and also because I already mqtt running and to me it is a more solid protocol.

Use the shellies discovery script

1 Like

Here are a few… you would replace the state_topic with the Shelly’s published MQTT topic and adapt the value_template to pull out the JSON attribute that’s relevant for you. You can do calculations and logic within the template if need be, they’re pretty powerful (if a bit strange!)

  sensor:
#simple value sensor
    - name: "HA CPU0"
      unique_id: "ha_cpu0temp"
      state_topic: "systems/ha"
      suggested_display_precision: 0
      unit_of_measurement: "C"
      value_template: "{{ value_json.cpu0_temp | float }}"
#device_class and state_class sometimes needed - e.g. Shelly power reporting
    - name: "Solar total DC Power"
      unique_id: "solar_dc_power"
      state_topic: "solardata"
      suggested_display_precision: 0
      unit_of_measurement: "W"
      value_template: "{{ value_json.powerDC | int }}"
      device_class: "power"
      state_class: "measurement"    
#this next one's a binary sensor like a switch state
  binary_sensor:
    - name: "ha sda SMART test"
      unique_id: "ha_sda_smart"
      state_topic: "systems/ha"
      value_template: "{{ value_json.sda_smart_test }}"
      payload_off: "Okay"
      payload_on: "Fail"

Looking at my MQTT traffic, I see each Shelly reporting on a topic like ‘shellypro4pm-789ac897de’; the topic is unique to each device. The payload when they report something is a JSON block, so the template will work to pull out the data:


{
  "src": "shellypro4pm-30c6f784f0a0",
  "dst": "shellypro4pm-30c6f784f0a0/events",
  "method": "NotifyStatus",
  "params": {
    "ts": 1686659100.46,
    "switch:3": {
      "id": 3,
      "aenergy": {
        "by_minute": [
          472.369,
          388.83,
          0
        ],
        "minute_ts": 1686659099,
        "total": 0.861
      }
    }
  }
}

Hope those help you on your way. Looks like @francisp has a possible lower-effort solution; that would be worth a try first, as it’ll save you a ton of effort typing and debugging sensor definitions!