[Inspiration] Daily energy prices chart (using Power by the Hour)

While not directly related to the app, but I was wondering what are some good energy meters to connect to my digital meter via the P1 port (Belgium)? On the app page there are some products linked, but perhaps there are better products out there?

I know for sure that the YouLess LS120 works in Belgium.

1 Like

Check the plugwise …

2 Likes

Is there a big difference with the HomeWizard P1 meter, because that one is way cheaper :thinking:

I bought the HW indeed while it is one of the cheapest, and it didn’t let me down, I don’t miss any value or such.
It also uses a local connection, no clouds.

It should be available for your BE meter, otherwise you can help HW make it compatible :

1 Like

Hi @Phil_Bro (and other interested users),

Here is the homeyscript code I use to generate the chart:

// Check if args are set
if (!args[0]) return false;
//let args = ["Vandaag"];
let a = args[0].split(',')
let periode = a[0].trim();

const threshold = 0.1;
let hour=0;
let prices_JSON_String;
let variable;


switch(periode) {
  case "Vandaag":
//Get prices of today (stored in DAP_Vandaag variabele by flow)
//To determine id of variable, run Homey.logic.getVariables() in web api playgrond 
      variable = await Homey.logic.getVariable({id: '<your id here>'});
      prices_JSON_String = variable.value.trim();
    break;
  case "Morgen":
//Get prices of tomorrow (stored in DAP_Morgen variabele by flow)
//To determine id of variable, run Homey.logic.getVariables() in web api playgrond 
      variable = await Homey.logic.getVariable({id: '<your id here>'});
      if (variable == "Onbekend"){return false};
      prices_JSON_String = variable.value.trim();
    break;
  case "Komend":
//Current hour, as starting point
      hour = parseInt(new Date().toLocaleString('nl-NL', {hour: '2-digit',   hour12: false, timeZone: 'Europe/Amsterdam' }));
//Get prices of coming hours (stored in DAP_KomendeUren variabele by flow)
//To determine id of variable, run Homey.logic.getVariables() in web api playgrond 
      variable = await Homey.logic.getVariable({id: '<your id here>'});
      prices_JSON_String = variable.value.trim();
    break;
  default:
    return false
}

// Convert input data to prices, labels and values
const prices = JSON.parse(prices_JSON_String.replaceAll("'", "\""));

const labels = Object.keys(prices).map((label) => {
  var num = parseInt(label)+hour;
  num = ((num < 24) ? num : num-24)
  label = num.toString();
  return label.padStart(2, "0");
});
const values = Object.values(prices);

// Determine average prices of known prices.
// Variant 1, mathematic average of all prices
//const averagePrice = (values.reduce((prev, cur) => prev + cur)/values.length);
// Variant 2, value between max and min price
const averagePrice = (Math.max(...values)+Math.min(...values))/2;
//const averagePrice = (maxPrice+minPrice)/2;
log(averagePrice)

// Map color of each bar based on value.
// You can play with the colors and values, if you like.
const backgrounds = values.map((value) => {
  if (value <= 0) {
    return 'rgb(189, 44, 188)'; // Purple (free energy)
  }

  if (value <= averagePrice*(1-threshold)) {
    return 'rgb(0, 170, 101)'; // Green (relatively cheap)
  }

  if (value <= averagePrice*(1+threshold)) {
     return 'rgb(53, 86, 81)'; // Dark green (normal price)
  }

  return 'rgb(237, 95, 23)'; // Orange (high price)
});

// Add a data label to the cheapest and most expensive hour
const cheapestIndex = values.indexOf(Math.min(...values));
const expensiveIndex = values.indexOf(Math.max(...values));

const datalabels = values.map((value, index) => {
  if (index === cheapestIndex || index === expensiveIndex) {
    return [`€${value.toFixed(2)}`];
  }
  return [];
});

// Build configuration for the chart
const settings = { 
	type: "bar",
	data: {
		labels,
		datasets: [
			{
				label: "Prijzen",
				backgroundColor: backgrounds,
				data: values
			}
		]
	},
	options: {
		responsive: true,
		legend: {
			position: "none"
		},
  title: {
      "display": true,
      "text": "Zonneplan Energie prijzen "+periode+" (Gem.: "+averagePrice.toFixed(2) +")"
      },
		layout: {
			padding: {
        top: 35,
        bottom: 0,
        left: 5,
        right: 5
      }
		},
		rectangleRadius: 6,
    plugins: {
      datalabels: {
        anchor: 'end',
        align: 'start',
        offset: -40,
        padding: 5,
        backgroundColor: backgrounds,
        color: 'white',
        borderWidth: 2,
        borderColor: 'white',
        borderRadius: 100,
        font: {
         size: 14
        }
	    }
    },
    datalabels
  }
}

// Build the final url and save as tag
// Set base url
const chartWidth = 540;
const chartHeight = 360;
const baseUrl = `https://image-charts.com/chart.js/2.8.0?bkg=white&height=${chartHeight}&width=${chartWidth}&c=`;

const settingsUrl = encodeURIComponent(JSON.stringify(settings))
const fullUrl = baseUrl + settingsUrl;
await tag('ChartUrl', fullUrl);

log(fullUrl);
return true;
1 Like

Many thanks !!!

Sorry to ask but, any idea where I find the ID as in:

Homey.logic.getVariable({id: '<your id here>'});

Sure, look in the script one row higher, it’s explained in the comment line. :wink:

Excuse is on it’s place here. Will go for new glasses and find myself the ID.

Thanks

1 Like

Not able to understand web api playground. Is the ID used here the same as to be found in the homey app, [instellingen/algemeen] Homey-ID ?

Thanks once again

The purpose of this is to get the id of the variable in which you put the JSON with the prices.
When you go to web api playground (Homey Developer Tools), remove the example code in the rectangle box and put the command
Homey.logic.getVariables() in that box and push the RUN button. In the screen all the homey variables that you have defined will be shown. Search for the one with the prices JSON en and copy the id of that variable and paste it in the script.

If you don’t want to use variables for this, parse the prices JSON directly to the script as the example in the opening post.

@Torch1969 @Phil_Bro
There’s a solution for the variable actions;
You can get a var, and then set that var by using these functions in your script:

1 Like

I think I understand the API playground and the vars. Using about 30 vars for different purposes.
Wha I didn’t understand is that I have to create the vars used in the script to work at all, thought it was pulled from the app data ‘power by the hour’.

Will continue checking this, lack of time now. Thank you !!!

1 Like