Shelly vs OpenHAB
The Shelly brand is known for its products that primarily communicate over WiFi, including smart plugs, relay switches, blinds control relays, and many other devices. One of the advantages for deployment is the ability to both read and control these devices using the universal MQTT protocol. Across existing add-ons for both OpenHAB and Home Assistant, we will demonstrate how to use Shelly devices without installing any additional extensions.
Hardware
Shelly devices supporting WiFi and MQTT (in this tutorial, Shelly Plus Plug S, Shelly Smoke Plus)
Pi-Home - if you don't have it, see the section How to Do It
Software
OpenHAB
Addon JSONPath Transofrmation (Settings->Bindings->Search JSON)
We will use the already installed Pi-Home by default with an existing MQTT server. First, connect the Shelly device to power, in this case, Shelly Plus Plug S into a socket. You will see a WiFi network named "ShellyPLusPLug.......". Connect to this network and enter the universal address for Shelly in your browser, which is http://192.168.33.1
After loading the window, set your home WiFi in Settings->WiFi and save. The device should connect to your WiFi. You can check IP from your router and set static address for it. Then, in Settings-> MQTT, you can enable the MQTT protocol. The server address is the address of your Raspberry Pi with OpenHAB and MQTT server.
You can also secure access to the Shelly interface by encrypting it in Settings -> Authentication. Then turn off Bluetooth and AP functions so that Shelly doesn't unnecessarily broadcast an unsecured network. After saving, Shelly should start sending some information via MQTT. To capture this information for diagnostic purposes, you can use any MQTT listener or simply SSH into Raspberry Pi and enter the command:
sudo mosquitto_sub -h localhost -t '#' -v -u openhabian -P yourMQTTpassword
Then, you will see all MQTT communication, including something like this:
Caution: Shelly can be Generation 1 or 2. Accordingly, the structure of the MQTT message may vary. Below, we will show a real example for both variants. The image shows Shelly Gen2 MQTT message.
Shelly Generation 1
In OpenHAB, go to Things, add a new one (plus icon), choose MQTT Binding -> Generic MQTT Thing. In the Bridge, select your MQTT Broker, enter some meaningful ID and description, and create the Thing. Then open the "Code" tabs and paste the following code. Change the header if necessary and especially change the ID (xxxxxxxxx in code) to your real Shelly ID. You can find it on the web interface or in the MQTT topic from the previous point.
Shelly Plus Plug S
UID: mqtt:topic:pihome:shelly
label: Shelly socket
thingTypeUID: mqtt:topic
configuration:
payloadNotAvailable: off
payloadAvailable: on
bridgeUID: mqtt:broker:pihome
location: MQTT
channels:
- id: Relay0
channelTypeUID: mqtt:switch
label: Relay0
description: null
configuration:
commandTopic: shellies/shellyplug-s-XXXXXXXXX/relay/0/command
stateTopic: shellies/shellyplug-s-XXXXXXXXXXX/relay/0
off: off
on: on
Save and create a new Channel item Add and Link Item and name it something. You will get a switch-type item that should control the respective socket. Various rules can be applied to this new item in OpenHAB.
Shelly Generation 2
In OpenHAB, go to Things, add a new one (plus icon), choose MQTT Binding -> Generic MQTT Thing. In the Bridge, select your MQTT Broker, enter some meaningful ID and description, and create the Thing. Then open the "Code" tabs and paste the following code. Change the header if necessary and especially change the ID to your real Shelly ID. You can find it on the web interface or in the MQTT topic from the previous point.
Shelly Plus Plug S
UID: mqtt:topic:pihome:shellyplusplugs-xxxxxxxxxx
label: Shelly PLug SW001
thingTypeUID: mqtt:topic
configuration:
payloadNotAvailable: off
payloadAvailable: on
bridgeUID: mqtt:broker:pihome
location: MQTT
channels:
- id: relay0
channelTypeUID: mqtt:switch
label: Power Switch
description: null
configuration:
commandTopic: shellyplusplugs-xxxxxxxxxxxx/rpc
formatBeforePublish: '{"id":0, "src":"MQTT","method":"Switch.Set", "params":{"id":0,"on":%s}}'
stateTopic: shellyplusplugs-xxxxxxxxxxxxxx/status/switch:0
transformationPattern: JSONPATH:$.output
off: "false"
on: "true"
Shelly Smoke Plus
- id: relay1
channelTypeUID: mqtt:switch
label: Fire alarm 1
description: null
configuration:
commandTopic: shellyplussmoke-xxxxxxxxxx/rpc
formatBeforePublish: '{"id":0, "params":{"id":0,"alarm":%s}}'
stateTopic: shellyplussmoke-xxxxxxxxxxxxxx/status/smoke:0
transformationPattern: JSONPATH:$.alarm
off: "false"
on: "true"
Save and create a new Channel item Add and Link Item and name it something. You will get a switch-type item that should control the respective socket. Various rules can be applied to this new item in OpenHAB.
Usage Examples:
- switching indoor sockets (Christmas tree, lights, pumps, etc.)
- controlling heating, boiler based on the photovoltaic power (use a relay on DIN or a robust socket at least 16A)
- upgrading traditional switches to smart ones via Shelly relays
- controlling blinds via Shelly relays
- smoke alarm - notification + turn off electricity in object
Add comment