Add Shelly to Home Assistant using MQTT

In this guide I will show you how to integrate Shelly devices to homeassistant using MQTT. When reading this guide I assume you already have MQTT installed and configured, if not follow this guide.

A great tool to configure your devices is the file editor plugin, this makes it easier to edit your yaml files without the use of samba.

Shelly 1 / Shelly Dimmer 2

First we edit our Shelly devices, because we need to specify that MQTT will be used. for this you don’t need to include your Shelly inside of the Shelly app.

Now go to your device ip within a browser and you will see the following (for the shelly one that is at least)

Select internet & security

When you have selected internet and security you will see the following

Select Advanced – developer settings

When you opened ADVANCED – DEVELOPER SETTINGS and pressed: “Enable action execution via MQTT” it will show additional fields.

The mandatory ones are:

  • Username
  • Password
  • Server

When you set these, press “Save”

After you’ve pressed “Save” You’ll need to go to settings, and note the “Device ID” within “Device info”

Device ID will be used later

The home assistant side

So, we enabled MQTT for our Shelly, but we still need to integrate that to our Home assistant.

To make this happen you eed to create a yaml file. I like to split my config and make it easier to find stuff later.

inside configuration.yaml add:

light: !include_dir_merge_list packs/lights

Where “packs/lights” is a folder where all files will be treated as “light”
Next, I create a file called “mqttlights.yaml” inside “packs/lights”

The config will look something like:

- platform: mqtt
  name: "Any name you like"
  state_topic: "shellies/shelly1-<device ID>/relay/0"
  command_topic: "shellies/shelly1-<device ID>/relay/0/command"
  payload_on: "on"
  payload_off: "off"
  retain: false

For the dimmer2 which needs some additional parameters it will look something like:

- platform: mqtt  
  schema: template
  name:  "Any name you like"
  state_topic: "shellies/shellydimmer2-<device ID>/light/0/status"
  state_template: '{% if value_json.ison %} on {% else %} off {% endif %}'
  command_topic: "shellies/shellydimmer2-<device ID>/light/0/set"
  command_on_template: '{"turn": "on"{% if brightness is defined %}, "brightness": {{(brightness | float * 0.3882 + 1) | round(0)}}{% endif %}}'
  command_off_template: '{"turn": "off"}'
  brightness_template: '{{ (value_json.brightness | float * 2.55) | round(0) }}'

Save the file and restart home assistant. Next you’ll need to add a lovelace card.

Top right corner within your dashboard press the 3 dots and press: “edit dashboard”, then press the Plus icon bottom right and select light or entity.

Shelly 2.5

I use Shelly 2.5 for roller shutters, the setup is the same a for the Shelly 1 but you need to change the mode to roller shutter. To do so:

Change settings to Roller Shutter

The config for this looks like:

- platform: mqtt
  name: "Your name"
  command_topic: "shellies/shellyswitch25-<device ID/roller/0/command"
  position_topic: "shellies/shellyswitch25-<device ID/roller/0/pos"
  set_position_topic: "shellies/shellyswitch25-<device ID>/roller/0/command/pos"
  retain: false
  optimistic: false
  payload_open: "open"
  payload_close: "close"
  payload_stop: "stop"
  position_open: 100
  position_closed: 0
  qos: 0

Don’t forget to add: “cover:” in “configuration.yaml” like we did for the lights

You then add the lights or roller shutter and you’re done.

Dennis

Related Post

Leave A Comment

Name

Website

Comment