How to build a light indicator of electricity price

How to create a smart light indicating the current electricity spot price? Here is a quick tutorial.

Jarmo Ollikainen

06.03.2023

Jarmo Ollikainen

Pre-Sales Architect at Frends

There was an article in Ilta-Sanomat (the second-largest newspaper in Finland) about a man who had made a smart light indicating the current electricity spot price with a color. I liked the idea and decided to build a similar automation and indicator light with what I already have in use: the Philips Hue light system and Frends iPaaS (integration platform as a service). Not many of us have access to a full-featured low-code integration platform like Frends, but the automation is surprisingly easy to set up using many other automation systems.

Let's start with the automation process picture to get an idea of the required automation steps:

Hue lights blog

Picture of the automation process taken directly from Frends iPaaS UI.

The colored boxes highlight the three steps needed:

  1. Get the current electricity spot price.
  2. Calculate the hue color value representing the current electricity spot price.
  3. Command the price indicator light to use the calculated hue color value.

Get the current electricity spot price

As I live in Finland, I want to use electricity spot price data for Finland, produced by Nordpool, the Pan-European power exchange. Unfortunately, Nordpool does not offer a free-of-charge API (Application Programming Interface) for getting electricity price information. However, the Entsoe Transparency Platform provides a free-to-use API for the Nordpool prices. In this example, I opted to use the api.spot-hinta.fi REST API, which offers free and easy-to-use REST API to get the current Nordpool electricity spot price.

In Frends, APIs are consumed by using a RestRequest Frends Task. A Frends Task is a functional component or building block used to build the automation process, like using Lego bricks. Each Task performs a particular function and is configured with Input parameters and Options to achieve the wanted result.

In this case, we need to make a very simple GET API request to API-endpoint https://api.spot-hinta.fi/JustNow to get the current electricity spot price. For this, you just configure the Method parameter to have value GET and the API-endpoint URL to the Url parameter.

URL parameter

When the process is executing, this is how the result looks in the Frends process execution details view:

Get current electricity

In the Body-section, you can see the current electricity spot price without tax (PriceNoTax) and with tax (PriceWithTax). By default, Frends shows the data returned by the API in a formatted mode. Here is an example of how the API returns the data in raw JSON format:

JSON

Next, in my example process, I store the PriceWithTax to a so-called Frends variable, which I also promote to the Frends log to allow me to easily see from the Frends process log how the price has changed over time. This is a completely optional step.

Current price to log

The picture above shows an example of Frends' low-code reference in the Expression field. The #result[Get Current Electricity Spot Price] refers to the Get Current Electricity Spot Price task and .Body.PriceWithTax refers to the PriceWithTax field value returned in the Body of the Task's result.

Calculate the hue color value representing the current electricity spot price

I decided to use green to represent cheap electricity prices starting from 0, blue for mid-price, and red for high electricity prices. Also, I wanted the price indicator color not to be just one of these three colors but for the color to dynamically shift across a color gradient of green-blue-red. The Hue light bulb color is set by a hue color value, which can be calculated to reflect actual changes in the electricity price.

Jarmo's example

Jarmo's result

In Frends, there are many ways to do this kind of calculation. For clarity, I chose to use the Code block, where you can use standard C#, for example, to do more or less complex calculations. The color value calculation is relatively simple. In a nutshell, I first calculate how much the current price is of the normal maximum spot price. Next, I calculate the equivalent hue color value between GREEN (price 0, hue value 21 845) and RED (maximum normal price, currently 0,70 €/kWh, hue value 65 535).

Calculate Hue value

I will not explain the calculation in detail, but please ask if the logic seems unclear from the code and the comments.

A few words about the #-prefixed Frends low-code variable references used in the code: in the Frends statements and expressions, we can use a mixture of standard C# and Frends low-code references to Frends variables like #var.CurrentPrice and #env.PhilipsHue.MaxSpotComparisonPrice. As you may recall, earlier in the process, the current price from the first API call's result was stored to the #var.CurrentPrice variable. Instead of using a static numeric value in the code for the maximum normal spot price, I use Frends environment variable #env.PhilipsHue.MaxSpotComparisonPrice. Environment variables are just parameters you can easily manage in Frends. It is much easier and safer to change the variable's value than to change the code whenever you want to change the value. As you see from the screenshot below, environment variables are a great way to store, for example, addresses and credentials. Also, if you have the same process running in different environments, you can have different environment variable values for each environment.

Hue environment

Note: the full HueBridgeIP is not shown in the picture above for security reasons.

Command the Philips Hue light bulb to use the calculated hue color value

Philips Hue Light bulbs can be controlled with API calls when you also have the Philips Hue Bridge device. You need to follow these instructions to find the bridge's IP address and to set up an API user: https://developers.meethue.com/develop/get-started-2/

Now, assuming that the Philips Hue API setup is done and as we have already calculated the hue color value to represent the current electricity spot price, the rest is easy.

Once again, we use the RestRequest Frends Task to do the required REST API call to Philips Hue API.

Update Price Indicator

  • Method: When changing a Hue light bulb's state (such as color), the Hue Lights API requires to use the PUT method.

  • Url: We need to have the Hue bridge's IP address in the URL. Instead of putting the IP address as a static value, we use the #env.PhilipsHue.HueBridgeIP to make it easy to change the IP address if necessary. I'm planning later to implement a dynamic detection of the Hue bridge IP address, but for now, the IP is stored in an environment variable. Also, the UserId needs to be part of the Url and is stored in an environment variable in this example.

  • Message: In the request message, we send a simple JSON message. The most important field, in this case, is the hue for which we give the hue value calculated in the previous step (#var.HueValue). The complete documentation of the Lights API and different fields: Lights API. You need to register in order to be able to view the documentation.

When the process is run, voilà, the Hue light bulb changes color representing the current electricity spot price. For example, when writing this article, the electricity spot price was 0.2043 €/kWh (20,43 cents/kWh), and the corresponding calculated hue color value was 34596, which the Philips Hue light bulb interprets as this green-blueish color:

Beta version of my price indicator light (a glass vase, paper towel to soften the light with a Philips Hue smart light bulb inside).

Afterword

I hope this article shows how easy automation can be when there are APIs and an easy-to-use automation system available. Surely, some APIs are not this easy to use, and many APIs require more complex authentication, but the principles are still the same.

share

related articles