hero
Home About

Categories

Archive

May 2024

April 2024

March 2024

February 2024

January 2024

December 2023

November 2023

October 2023

September 2023

August 2023

July 2023

June 2023

May 2023

April 2023

March 2023

February 2023

January 2023

December 2022

back to home

Fun with Shelly Plug S - Switching Power on and off

09 February 2023 • 4 mins

Shelly is a Germany and US based company that provides gadgets for home automation. Their products are popular among home automation enthusiasts, because unlike competing products, you can easily access Shelly’s products with standard technologies like REST and MQTT.

Other vendors desperately try to sell their own landscape, app, cloud, etc., and while the Shelly guys have those things too, they don’t build a fence around their cloud or products.

As already mentioned, Shelly is built for private use at home. Therefore, we should carefully check if their products meet the reader’s standards before using them in a professional, industrial environment.

In this article, we will build an app that sends commands to the Shelly Plug S to switch a light on and off. We will use a toggle button on the Peakboard screen so that when the app starts, it should already have the correct state representing whether the light is on or off. We will also show the curent power consumption of the light with a gauge.

Please note: This will be a direct communication between Peakboard and Shelly—no MQTT, no hub, no cloud….

The API of the Shelly Plug S

Please refer to the official documentation for how to use the API of the Plug. Here are the three function we will use.

http://<MyShellyPlugIP>/status

returns a JSON object with lots of information about the plug. For our needs, there are two attributes: meters[0].power is the current power consumption and ison indicates whether the plug is turned on or off. The two functions

http://<MyShellyPlugIP>//relay/0?turn=on
http://<MyShellyPlugIP>//relay/0?turn=off

are used to send commands to turn the power on and off.

Preparing the canvas

We prepare the canvas with a gauge; a big, fat toggle button; and a text box that prints out the formatted power consumption value.

image

Setting the initial state of the toggle button

We start with a simple JSON data source that points to the status API.

image

This data source only runs once to set the state of the toggle button. So we add a simple command to the Refreshed script of this data source, to set the active attribute of the toggle button.

image

Processing the power consumption

The second data source is executed once per second and calls the same API as the last one. However, we use a path to go deeper into the JSON result in order to read the power consumption according to the API documentation.

image

Before we can use the power value, we need to re-format it from a string to a number. A small data flow will do that for us:

image

And now we can bind the text box and gauge to the output of the dataflow:

image

Sending on / off commands

For sending the on / off commands, we prepare two simple JSON sources that point to the two corresponding API calls. As we don’t want to fire them without anyone pressing the toggle button, we set both to not enabled.

image

Finally, the toggle button has two events that are relevant for us. One for checking, and one for unchecking. In both, we just reload the JSON source for switching on / off:

image

The result

Here’s how our final application works in real life. You can see in the video how the power consumption goes up when the light is switched on. Feel free to download it.

Fun with Shelly Plug S - Switching Power on and off

Newsletter

Want to get notified when a new article is published? Sign up below!