Using HomeyScript to calculate Energy Consumption of a device

Hi everyone,

I just wanted to share a script I built to calculate the energy consumption of my dryer based on different energy rates (peak hours are more expensive than night hours). It took my some time to learn HomeyScript and Javascript and moreover to discover which Homey Apps and logic to use. And I did a lot of searching in these communities. Hopefully this is of use to someone else.

Problem statement: I want to calculate the energy consumption of a device based on different energy rates, which are based on the time of day, the day of the week and bank holidays.

Solution (as how I solved it):

  1. Installed a smart power plug for my dryer that (obviously) measures power and energy consumption
  2. Created 2 Homey Logic variables for a low (“Laagtarief”) and high (“Hoogtarief”) energy rate in cents per kWh
  3. Installed the Better Logic App from the Homey App Store and created a couple of variables in this App
  4. Created a HomeyScript to determine if I need to apply the low energy rate or the high one, depending on the time of day, the day of the week and if it is a bank holiday
  5. Created 2 flows to set the energy rate to apply. Once at 7 AM and once at 11 PM. These flows execute my script
  6. Created a flow that triggers when my dryer starts. This flow reads the current energy consumption of the smart power plug (this energy consumption just accumulates)
  7. Created a second flow that triggers when my dryer stops. This flow now calculates a mathjs expression in the Better Logic App to calculate the energy consumption in Euro (consumed kWh * energy rate) of the run and increases the energy consumption for the day (I might have several dryer runs per day). It also uses a mathjs expression in the Better Logic App to accumulate the year total energy consumption for the dryer

As to my script: I store the result of the check and energy rate in a couple of variables in the Better Logic App, using the method “BLApp.apiPut”. And the nice thing of Better Logic is, that you can use these variables in Homey flows. You can even calculate with them (using mathjs expressions).

Nothing brilliant to all of this, but just wanted to share it with the community so others might find use of it. Have fun! And if you have questions, just let me know.

Here is my script:

var Tarief = 0 ;
var Feestdag_check = -1;
var Weekend_check = -1;

/* Definieer de weekdagen, weekenddagen en feestdagen. */

var Weekdagen = [‘Zondag’,‘Maandag’,‘Dinsdag’,‘Woensdag’,‘Donderdag’,‘Vrijdag’,‘Zaterdag’];
var Weekenddagen = [‘Zondag’,‘Zaterdag’];
var Feestdagen = [‘01-01’,‘04-15’,‘04-18’,‘04-27’,‘05-05’,‘05-26’,‘06-06’,‘12-26’];

/* Haal de datum van vandaag op en bepaal de elementen. */

var Datum = new Date();
var Jaar = Datum.getFullYear();
var Maand = Datum.getMonth()+1;
var Dag = Datum.getDate();
var Uur = Datum.getHours()+1; /* 1 uur optellen i.v.m. conversie van GMT naar GMT+1. */
var Minuut = Datum.getMinutes();

/* Bepaal de dag. */

let Vandaag = Weekdagen[Datum.getDay()];

/* Haal de tarieven op vanuit Homey Logicals. */

let Laagtarief = await Homey.logic.getVariable({id: ‘7af81c50-83cd-451a-9cb5-0fc23ad37ee7’});
let Hoogtarief = await Homey.logic.getVariable({id: ‘5cabc141-0d1b-4d45-89a2-46880a1c8c4a’});

/* Maak een DagMaand string in formaat jj-dd en plak er een nul voor als kleiner dan 10 */

var DagMaand = ‘0’;
if( Maand < 10 ) { Maand = ‘0’.concat(Maand.toString()); }
if( Dag < 10 ) { Dag = ‘0’.concat(Dag.toString()); }
DagMaand = Maand.concat(’-’,Dag);

/* Controleer of vandaag een feestdag is en of het weekend is. /
Let op: het eerste veld in de Array levert waarde 0; als het veld niet wordt gevonden, dan is de waarde -1. */

Feestdag_check = Feestdagen.indexOf(DagMaand);
Weekend_check = Weekenddagen.indexOf(Vandaag);

/* Zet waarde gebaseerd op de tijd, feestdag en weekend. */

if (Uur == 7 && Weekend_check == -1 && Feestdag_check == -1) { Tarief = Hoogtarief.value; } else { Tarief = Laagtarief.value; }

/* Tarief formatteren. */

Tarief = Tarief.toFixed(5);

/* Schrijf tarief weg in Better Logic App als een variabele. */

let BLApp = await Homey.apps.getApp({id:‘net.i-dev.betterlogic’});
await BLApp.apiPut(‘Tarief/’+ Tarief);

if (Tarief == Hoogtarief.value) {
await BLApp.apiPut(‘Soort Tarief/Hoog’); }
else {
await BLApp.apiPut(‘Soort Tarief/Laag’);

return Tarief;


Nice work. Just to let you know there is also an app that does all this for you:

Thanks Robin.
I know this App. Using it also, but this does not do the trick per smart socket, right?

Euh, why not? Im using it for several smart sockets myself.

Oké, apparently I then need to have a closer look at the your App :slight_smile:

1 Like