# \[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:** 2

<div class="post-metadata">

**Author:** ![Stikstof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/stikstof/32/76870_2.png) [@Stikstof](https://community.homey.app/u/Stikstof)\
**Post date:** [November 19, 2023, 10:45am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/22 "2023-11-19T10:45:51Z")

</div>

Cool! 😉

---

<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:** [November 19, 2023, 3:16pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/23 "2023-11-19T15:16:40Z")

</div>

Nice, can you share your code?

---

<div class="post-metadata">

**Author:** ![CaptainVoni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/captainvoni/32/3549_2.png) [@CaptainVoni](https://community.homey.app/u/CaptainVoni)\
**Post date:** [November 20, 2023, 7:43am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/24 "2023-11-20T07:43:20Z")

</div>

Which code? The code for the chart is pretty much the same Stikstof shared. The code for the battery charging strategy is a big mess under constant development. 😁

---

<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:** [November 20, 2023, 10:15am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/25 "2023-11-20T10:15:19Z")

</div>

Well yes, I mean the code for the battery charging strategy.  
I have (optimised) working code (homeyscript) for all known prices for coming hours. I would like to know how you add the “charging strategy” knowledge in the graph.  
Maybe you can tell in words how you accomplished that?

---

<div class="post-metadata">

**Author:** ![CaptainVoni](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/captainvoni/32/3549_2.png) [@CaptainVoni](https://community.homey.app/u/CaptainVoni)\
**Post date:** [November 20, 2023, 10:55am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/26 "2023-11-20T10:55:42Z")

</div>

The code for the visualisation is pretty straight forward. The major part is that:

```auto
	let values=[],labels=[],backgrounds=[],datalabels=[];
	lastSimulatedSOC=-1;
	for (i=0;i<simulatedHours.length;i++)
	{
		hour=simulatedHours[i];
		values.push(hour.price);
		labels.push(((currentHour+i)%24).toFixed(0).padStart(2,"0"));
		
		simulatedSOC=Math.round(100.0*(MIN_SOC+hour.simulatedBatteryAtEnd/batteryNetCapacity*(1-MIN_SOC)));
		if (simulatedSOC!=lastSimulatedSOC) datalabels.push(simulatedSOC.toFixed(0)+"%");
		else datalabels.push([]);
		lastSimulatedSOC=simulatedSOC;
		
		let color;
		if (hour.batteryMode.prioritiseBattery) color="rgb(255,0,0)";
		else if (!hour.batteryMode.allowDischarging) color="rgb(255,255,0)";
		else if (hour.pvYield>=hour.consumption) color="rgb(192,255,64)";
		else color="rgb(0,255,0)";		
		backgrounds.push(color);
	}

```

The optimiser just returns for every hour if either (i) battery discharging should be prevented, (ii) battery charging should be prioritised, i.e. no self consumption until battery is full, or (iii) no optimisation necessary, just use prices to decide what to do. Those three I use to choose a color, in addition to the estimated PV yield being higher than the estimated consumption (which I haven’t seen for quite some time 😁)

I also add the estimated battery level as data label, but only if it has changed compared to the last hour.

The four arrays values, labels, backgrounds and datalabels then just go straight into the “settings” for the chart.

```auto
	const settings=
	{ 
		type: "bar",
		data: 
		{
			labels,
			datasets: [
				{
					label: "Optimierung",
					backgroundColor: backgrounds,
					data: values
				}
			]
		},
		options: 
		{
			responsive: true,
			legend: { position: "none" },
			layout: 
			{
				padding: 
				{
					top: 35,
					bottom: 0,
					left: 5,
					right: 5
				}
			},
			rectangleRadius: 6,
			plugins: 
			{
				datalabels: 
				{
					anchor: 'end',
					align: 'start',
					offset: -10,
					padding: 0,
					backgroundColor: 'white',
					color: 'black',
					font: { size: 7 }
				}
			},
			datalabels
		}
	}

```

---

<div class="post-metadata">

**Author:** ![Anders\_E](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/anders_e/32/13512_2.png) [@Anders\_E](https://community.homey.app/u/Anders_E)\
**Post date:** [November 28, 2023, 8:41pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/27 "2023-11-28T20:41:39Z")

</div>

Looks really good and might try it out.

I was looking for a function: Getting a push notification around 21.00 with the cheapest two coherent hours during the night and what the prices would be. This way I would easily know when to start e.g. dishwasher. Seems it would be possible to reuse some of the functions above.

Anyone have suggestions for it? 🙂

---

<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:** [November 28, 2023, 11:24pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/28 "2023-11-28T23:24:15Z")

</div>

Not sure if you need it to be in Hscript, but you could use a flow like this for inspiration

 ![Screenshot 2023-09-21 at 17-18-30 1.HomeyPro Peter](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/a/e/ae393b49f1e469de9e5b4c8983297de54f60db34.png)

You can import it as well:

> [@The Flow Exchange(r) - Exchange Your Flows with Others!](https://community.homey.app/t/the-flow-exchange-r-exchange-your-flows-with-others/68981/81):
>
> Ask @ home users one by one to start the washing machine at cheap energy rates A washing/drying machine is something you can’t exactly automate, while the laundry has to be loaded or moved for instance. Also it’s energy-hungry machines, which should run preferably at low energy rates. This flow reacts to low energy prices; it sends a push confirmation to a user → and when there’s no response, or it was declined → send a push confirmation to the next user to start the washing machine →…

---

<div class="post-metadata">

**Author:** ![Anders\_E](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/anders_e/32/13512_2.png) [@Anders\_E](https://community.homey.app/u/Anders_E)\
**Post date:** [November 29, 2023, 7:35am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/29 "2023-11-29T07:35:08Z")

</div>

Thank you Peter. That looks quite interesting. I’ll take a deeper look at that one.

---

<div class="post-metadata">

**Author:** ![hanneskl](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hanneskl/32/80114_2.png) [@hanneskl](https://community.homey.app/u/hanneskl)\
**Post date:** [January 23, 2024, 8:17pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/30 "2024-01-23T20:17:32Z")

</div>

Hi Gruiter,  
How can I activate this image graph?  
I only see this:

 ![Screenshot 2024-01-23 at 21.17.05](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/0/70779f7fe3af7e5bb54d4da532136fcac8c6933b.jpeg)

---

<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:** [January 23, 2024, 8:32pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/31 "2024-01-23T20:32:06Z")

</div>

> [@hanneskl](#):
>
> How can I activate this image graph?

It is automatically generated when you have prices. But you dont have prices, so probably the source data is down. If in a few days you still dont see anything send an app diagnostics report.

---

<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:** [February 5, 2024, 6:33pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/33 "2024-02-05T18:33:46Z")

</div>

Hi @Stikstof and @CaptainVoni,

I followed the directive from scratch and have a flow for the purpose of test. If you see the image, the error I got is similar for both scripts, so the original but also the @CaptainVoni script.

EDIT, just got the default script working, so the @CaptainVoni script still returns the error.

The error occurs when running the script manually with the test data (yellow). Today the flow did not respond to the data received by PBTH.

Any any what’s wrong? Suppose its a simple solution, but I just don’t know.

Thanks!

 ![Screenshot 2024-02-05 at 19.28.52](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/b/db98039c8c99b3959b3346fcf7a5c7f25e10eb7d.jpeg)

---

<div class="post-metadata">

**Author:** ![Stikstof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/stikstof/32/76870_2.png) [@Stikstof](https://community.homey.app/u/Stikstof)\
**Post date:** [February 5, 2024, 7:21pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/34 "2024-02-05T19:21:50Z")

</div>

Hi Phil, maybe you can share your code?

---

<div class="post-metadata">

**Author:** ![Stikstof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/stikstof/32/76870_2.png) [@Stikstof](https://community.homey.app/u/Stikstof)\
**Post date:** [February 5, 2024, 7:27pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/35 "2024-02-05T19:27:17Z")

</div>

The error you are seeing seems helpful.

Probably you are using the `await` within a function. That is only allowed in an async function.

e.g.

```auto
async function() {
  await tag('ChartUrl', fullUrl);
}

```

---

<div class="post-metadata">

**Author:** ![Stikstof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/stikstof/32/76870_2.png) [@Stikstof](https://community.homey.app/u/Stikstof)\
**Post date:** [February 5, 2024, 7:48pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/37 "2024-02-05T19:48:54Z")

</div>

It seems you are missing a closing bracket `}` at the end.

Try this:

```auto
// Check if args are set
if (!args[0]) return false;

function buildChartUrl(simulatedHours)
{
	// Set base url
	const chartWidth = 1200;
	const chartHeight = 600;
	const baseUrl = `https://image-charts.com/chart.js/2.8.0?bkg=white&height=${chartHeight}&width=${chartWidth}&c=`;

	let values=[],labels=[],backgrounds=[],datalabels=[];
	lastSimulatedSOC=-1;
	for (i=0;i<simulatedHours.length;i++)
	{
		hour=simulatedHours[i];
		values.push(hour.price);
		labels.push(((currentHour+i)%24).toFixed(0).padStart(2,"0"));
		
		simulatedSOC=Math.round(100.0*hour.simulatedBatteryAtEnd/batteryCapacity);
		if (simulatedSOC!=lastSimulatedSOC) datalabels.push(simulatedSOC.toFixed(0)+"%");
		else datalabels.push([]);
		lastSimulatedSOC=simulatedSOC;
		
		let color;
		if (hour.chargingMode==CHARGING_GRID) color="rgb(255,0,0)";
		else if (hour.chargingMode==CHARGING_PRIORITISE) color="rgb(255,160,0)";
		else if (hour.dischargingMode==DISCHARGING_OFF) color="rgb(255,255,0)";
		else if (hour.pvYield>=hour.consumption) color="rgb(192,255,64)";
		else color="rgb(0,255,0)";		
		backgrounds.push(color);
	}

	// Build configuration for the chart
	const settings=
	{ 
		type: "bar",
		data: 
		{
			labels,
			datasets: [
				{
					label: "Optimierung",
					backgroundColor: backgrounds,
					data: values
				}
			]
		},
		options: 
		{
			responsive: true,
			legend: { position: "none" },
			layout: 
			{
				padding: 
				{
					top: 35,
					bottom: 0,
					left: 5,
					right: 5
				}
			},
			rectangleRadius: 6,
			plugins: 
			{
				datalabels: 
				{
					anchor: 'end',
					align: 'start',
					offset: -10,
					padding: 0,
					backgroundColor: 'white',
					color: 'black',
					font: { size: 7 }
				}
			},
			datalabels
		}
	}
}

// Build the final url and save as tag
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:** [February 5, 2024, 8:04pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/38 "2024-02-05T20:04:13Z")

</div>

it now says : error, settings not defined (copy paste your script).

In the script line 36 settings is grayed out.

EDIT; taking a closer look it says;  
‘settings’ is declared but its value is never read.(6133)

Seems the extra bracket at the end is causing this trouble, but I don’t know. Everything further than old school VB is like chinese for me, but learning here.

---

<div class="post-metadata">

**Author:** ![Stikstof](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/stikstof/32/76870_2.png) [@Stikstof](https://community.homey.app/u/Stikstof)\
**Post date:** [February 5, 2024, 8:36pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/39 "2024-02-05T20:36:43Z")

</div>

I’m sorry. I’ll send you a personal message, because think I am missing some context.

---

<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:** [February 5, 2024, 8:53pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/40 "2024-02-05T20:53:15Z")

</div>

Hi,

I tried to copy paste the script info but it does not function. Is this the full script?

Just not possible for me to implement, for that I regret the scripting part since your output is very nice!

P

---

<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 3, 2024, 12:34pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/41 "2024-04-03T12:34:03Z")

</div>

[Torch1969](https://community.homey.app/u/Torch1969)

would you be so kind to share the code to adjust the defaults like you did?

---

<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 6, 2024, 11:51am UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/42 "2024-04-06T11:51:20Z")

</div>

Yes, somewhere in the coming days. I need to use my computer to be able to copy the code 😏

---

<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 6, 2024, 2:31pm UTC](https://community.homey.app/t/inspiration-daily-energy-prices-chart-using-power-by-the-hour/80821/43 "2024-04-06T14:31:16Z")

</div>

Perfect! Take it easy, wait for you.

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

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