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

**URL:** <https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821>\
**Category:** Flows\
**Tags:** energy, homeyscript, flow, homey-pro\
**Created:** [May 20, 2023, 2:42pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821 "2023-05-20T14:42:26Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![simtom](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/simtom/32/103694_2.png) [@simtom](https://community.homey.app/u/simtom)\
**Post date:** [April 12, 2024, 11:02am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/44 "2024-04-12T11:02:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Gruijter](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gruijter/32/1973_2.png) [@Gruijter](https://community.homey.app/u/Gruijter)\
**Post date:** [April 12, 2024, 11:38am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/45 "2024-04-12T11:38:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phil\_Bro](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/phil_bro/32/78156_2.png) [@Phil\_Bro](https://community.homey.app/u/Phil_Bro)\
**Post date:** [April 12, 2024, 11:58am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/46 "2024-04-12T11:58:44Z")

</div>

Check the plugwise …

> **[Smile P1 (V3) | Plugwise](https://www.plugwise.com/product/smile-p1-v3-2/?lang=en)**

---

<div class="post-metadata">

**Author:** ![simtom](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/simtom/32/103694_2.png) [@simtom](https://community.homey.app/u/simtom)\
**Post date:** [April 12, 2024, 12:19pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/47 "2024-04-12T12:19:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 13, 2024, 12:42am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/48 "2024-04-13T00:42:27Z")

</div>

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.

> ![Screenshot_20240413-024147_Homey](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/b/2b65f35eeb77b2696d85884227ffd456ebb226f8.jpeg)

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

> **[Is my smart meter compatible? | HomeWizard Helpdesk](https://helpdesk.homewizard.com/en/articles/5935311-is-my-smart-meter-compatible)**
>
> Find your smart meter compatibility, version, P1 port location and if it requires a power adapter.

---

<div class="post-metadata">

**Author:** ![Torch1969](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torch1969/32/33305_2.png) [@Torch1969](https://community.homey.app/u/Torch1969)\
**Post date:** [April 17, 2024, 5:36pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/49 "2024-04-17T17:36:22Z")

</div>

Hi @Phil_Bro (and other interested users),

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

```auto
// 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;

```

---

<div class="post-metadata">

**Author:** ![Phil\_Bro](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/phil_bro/32/78156_2.png) [@Phil\_Bro](https://community.homey.app/u/Phil_Bro)\
**Post date:** [April 19, 2024, 8:46am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/50 "2024-04-19T08:46:44Z")

</div>

Many thanks !!!

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

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

```

---

<div class="post-metadata">

**Author:** ![Torch1969](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torch1969/32/33305_2.png) [@Torch1969](https://community.homey.app/u/Torch1969)\
**Post date:** [April 19, 2024, 10:00am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/51 "2024-04-19T10:00:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Phil\_Bro](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/phil_bro/32/78156_2.png) [@Phil\_Bro](https://community.homey.app/u/Phil_Bro)\
**Post date:** [April 19, 2024, 3:58pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/52 "2024-04-19T15:58:15Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![Phil\_Bro](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/phil_bro/32/78156_2.png) [@Phil\_Bro](https://community.homey.app/u/Phil_Bro)\
**Post date:** [April 19, 2024, 6:53pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/53 "2024-04-19T18:53:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Torch1969](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/torch1969/32/33305_2.png) [@Torch1969](https://community.homey.app/u/Torch1969)\
**Post date:** [April 20, 2024, 2:05pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/54 "2024-04-20T14:05:18Z")

</div>

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](https://tools.developer.homey.app/tools/api-playground)), 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.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 20, 2024, 7:46pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/55 "2024-04-20T19:46:22Z")

</div>

@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:

> [@Setting Logic Variable by HomeyScript](https://community.homey.app/t/setting-logic-variable-by-homeyscript/14819/43):
>
> ```auto
> //Name of Logic Variable
> var name = "TVProgramma";
> 
> //Read Logic Var
> var value = await ReadLogicVar(name);
> console.log("The parameter '"+name+"' has a value '"+value+"'");
> 
> //Write Logic Var
> var new_value = "NOS Journaal";
> await WriteLogicVar(name,new_value);
> 
> async function ReadLogicVar(name){
> let Logic = await Homey.logic.getVariables();
> let idArr = Object.entries(Logic);
> let filtered = idArr.filter(([key,value]) => value.name==name);
> let [[,{id:varID}]] = filtered;
> 
> var varArr = await Homey.logic.getVariable({id:varID});
> 
> return varArr.value;
> }
> 
> async function WriteLogicVar(name,value){
> let Logic = await Homey.logic.getVariables();
> let idArr = Object.entries(Logic);
> let filtered = idArr.filter(([key,value]) => value.name==name);
> let [[,{id:varID}]] = filtered;
>   
> var varArr = await Homey.logic.getVariable({id:varID});
> 
> if (typeof(value) == varArr.type){
> await Homey.logic.updateVariable({id:varID,variable:{value:value}})
> //console.log("Value has been updated")
> return true;
> }
> else{
> console.log("New value has the wrong type! Expected type is '"+varArr.type+"', not '"+typeof(value)+"'. Value has NOT been updated!")
> return false;
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Phil\_Bro](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/phil_bro/32/78156_2.png) [@Phil\_Bro](https://community.homey.app/u/Phil_Bro)\
**Post date:** [April 20, 2024, 9:30pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/56 "2024-04-20T21:30:47Z")

</div>

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 !!!

---

<div class="post-metadata">

**Author:** ![MrDutchfighter](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mrdutchfighter/32/4295_2.png) [@MrDutchfighter](https://community.homey.app/u/MrDutchfighter)\
**Post date:** [November 4, 2024, 4:49pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/58 "2024-11-04T16:49:44Z")

</div>

This is really cool! Looking for something like this with the new Homey Dashboard. Any chance this will be embedded in the Power By The Hour app anytime as a dashboard widget?

---

<div class="post-metadata">

**Author:** ![Gruijter](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gruijter/32/1973_2.png) [@Gruijter](https://community.homey.app/u/Gruijter)\
**Post date:** [November 4, 2024, 5:11pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/59 "2024-11-04T17:11:32Z")

</div>

> [@MrDutchfighter](#):
>
> Looking for something like this with the new Homey Dashboard.

The price graph from PBTH is already available for Homey Dashboard:

 ![1000000668](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/6/b/6b25e98ac60c8091d62e0cb7ec6416ba9cfc4421.jpeg)

---

<div class="post-metadata">

**Author:** ![MrDutchfighter](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/mrdutchfighter/32/4295_2.png) [@MrDutchfighter](https://community.homey.app/u/MrDutchfighter)\
**Post date:** [November 4, 2024, 9:38pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/60 "2024-11-04T21:38:56Z")

</div>

That is awesome, thanks! Missed that completely because I didn’t add it as a device, looks awesome 🙂

---

<div class="post-metadata">

**Author:** ![Jeroen\_Leeflang](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jeroen_leeflang/32/12593_2.png) [@Jeroen\_Leeflang](https://community.homey.app/u/Jeroen_Leeflang)\
**Post date:** [December 14, 2024, 7:21am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/61 "2024-12-14T07:21:21Z")

</div>

Are there any ideas to add Power by the hour to Homey Dashboard.  
I want to be able to show the hourly prices graph for the current day to be present on a dashboard tab.

---

<div class="post-metadata">

**Author:** ![Gruijter](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/gruijter/32/1973_2.png) [@Gruijter](https://community.homey.app/u/Gruijter)\
**Post date:** [December 14, 2024, 8:17am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/62 "2024-12-14T08:17:31Z")

</div>

> [@Jeroen\_Leeflang](#):
>
> show the hourly prices graph for the current day to be present on a dashboard tab.

Already possible. Add it as camera image.

---

<div class="post-metadata">

**Author:** ![RuneTun](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/runetun/32/17017_2.png) [@RuneTun](https://community.homey.app/u/RuneTun)\
**Post date:** [December 19, 2024, 7:36pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/63 "2024-12-19T19:36:26Z")

</div>

Hi, Im not a super user but would still like to get the daily el. price graph into homey dash.

Any info would be recieved with thanks

Rune

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [December 19, 2024, 10:21pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/64 "2024-12-19T22:21:55Z")

</div>

Like Robin explained

> [@Gruijter](#):
>
> Add it as camera image

I’ll elaborate a bit.  
The graph is a available as “cam” in the app:

> ![1000039055](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/5/1/513466a989cfadc6dcea915739e286d1b479ff50.jpeg)

In the dashboard, you can add a Camera widget.  
Then select the “cam” from your DAP device;  
I called this DAP device “_Z.P. Energie Uurprijs_”:

> ![1000039051](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/b/dbaffe86a7e1e07791c490cc89d3d1d2222e9321.jpeg)

Dashboard view:

> ![1000039053](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/6/9/6947530d67a1ae301bd2807a12551f95901966e0.jpeg)

[Previous page](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821.md?page=2)

[Next page](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821.md?page=4)
