<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.xsl" type="text/xsl"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>eInk Display for Octopus&#8217;s Agile Energy Tariff &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Thu, 05 Dec 2024 08:17:56 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>eInk Display for Octopus&#8217;s Agile Energy Tariff &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[eInk Display for Octopus's Agile Energy Tariff]]></title>
		<link>https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/</link>
					<comments>https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/#comments</comments>
				<dc:creator><![CDATA[@edent]]></dc:creator>
		<pubDate>Tue, 05 Dec 2023 12:34:22 +0000</pubDate>
				<category><![CDATA[/etc/]]></category>
		<category><![CDATA[battery]]></category>
		<category><![CDATA[eink]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[solar]]></category>
		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=48583</guid>

					<description><![CDATA[I&#039;m a little bit obsessed with building eInk displays. They&#039;re pretty cheap second hand. They&#039;re low energy, passive displays, with good-enough performance for occasional updates.  Here&#039;s a new one which shows me what the current cost of my electricity is:    Background  After installing solar panels, a smart electricity meter, and a solar battery - the next obvious step was a smart energy…]]></description>
										<content:encoded><![CDATA[<p>I'm a little bit obsessed with <a href="https://shkspr.mobi/blog/2020/02/turn-an-old-ereader-into-an-information-screen-nook-str/">building eInk displays</a>. They're pretty cheap second hand. They're low energy, passive displays, with good-enough performance for occasional updates.  Here's a new one which shows me what the current cost of my electricity is:</p>

<img src="https://shkspr.mobi/blog/wp-content/uploads/2023/12/Graph.jpg" alt="An eInk screen with a line graph on it. The graph shows the current price of power. The eInk is mounted in a wooden frame." width="1024" height="768" class="aligncenter size-full wp-image-48820">

<h2 id="background"><a href="https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/#background">Background</a></h2>

<p>After installing <a href="https://shkspr.mobi/blog/2020/03/relaunching-edent_solar-part-1-installation/">solar panels</a>, a <a href="https://shkspr.mobi/blog/2020/04/how-to-take-solar-export-readings-on-the-honeywell-as302p-smart-meter/">smart electricity meter</a>, and a <a href="https://shkspr.mobi/blog/2023/08/review-moixa-4-8kwh-solar-battery/">solar battery</a> - the next obvious step was a smart energy tariff.</p>

<p>Octopus (<a href="https://share.octopus.energy/metal-dove-988">join and we both get £50</a>) have an "Agile" tariff. Unlike a normal tariff - with a set price for electricity - this tariff fluctuates every 30 minutes.  Prices depend on wholesale costs which means they can go negative. That's right, you can get paid to soak up excess power.</p>

<p>Of course, they can also spike considerably. Unlike <a href="https://edition.cnn.com/2021/02/23/us/texas-outages-electric-bills-griddy/index.html">the failed Texas experiment</a>, here the maximum price is capped at £1/kWh.</p>

<p>Every day at about 1600, the next day's prices are published on Octopus's website. And they're <a href="https://developer.octopus.energy/rest/">also made available via a simple REST API</a>.</p>

<p>So, it's relatively simple to generate a line graph and display it on the eInk screen.</p>

<h2 id="code"><a href="https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/#code">Code</a></h2>

<p>(You can treat this code as MIT Licenced if that makes you happy.)</p>

<p>Calling the API for the half-houly prices is:</p>

<pre><code class="language-php">$url = "https://{$API_KEY}:@api.octopus.energy/v1/products/" .
       "AGILE-FLEX-22-11-25/electricity-tariffs/E-1R-AGILE-FLEX-22-11-25-C/standard-unit-rates/";
</code></pre>

<p>Your API_KEY is unique - and you'll need to check which tariff you're on.</p>

<p>The data is retrieved as JSON and converted:</p>

<pre><code class="language-php">$content = file_get_contents($url);
$data = json_decode($content);
</code></pre>

<p>The JSON is full of entries like this:</p>

<pre><code class="language-JSON">"results": [
{
  "value_exc_vat": 13.6,
  "value_inc_vat": 14.28,
  "valid_from": "2023-11-01T22:30:00Z",
  "valid_to": "2023-11-01T23:00:00Z",
  "payment_method": null
},
{
  "value_exc_vat": 18.4,
  "value_inc_vat": 19.32,
  "valid_from": "2023-11-01T22:00:00Z",
  "valid_to": "2023-11-01T22:30:00Z",
  "payment_method": null
},
</code></pre>

<p>They're newest first, so need to be reversed:</p>

<pre><code class="language-php">$tariffs = array_reverse( $data-&gt;results );
</code></pre>

<p>Then it's a case of looping through them and grabbing <em>today's</em> data:</p>

<pre><code class="language-php">$userTimeZone = new DateTimeZone('Europe/London');
$now = new DateTime('now', $userTimeZone);

$nowPosition = 0;
$datay = array();
$datax = array();

foreach ( $tariffs as $tariff ) {
    $dateStringFrom = $tariff-&gt;valid_from;
    $dateStringTo   = $tariff-&gt;valid_to;
    $dateTimeFrom = new DateTime($dateStringFrom, new DateTimeZone('UTC'));
    $dateTimeTo   = new DateTime($dateStringTo,   new DateTimeZone('UTC'));

    if ($now &gt;= $dateTimeFrom &amp;&amp; $now &lt;= $dateTimeTo) {
        $costNow = $roundedInteger = (int)round( $tariff-&gt;value_inc_vat );
        $hour   = intval( $dateTimeFrom-&gt;format('G') ); //  No leading 0
        $minute = intval( $dateTimeFrom-&gt;format('i') );
        $offset = ($minute == 0) ? 0 : (($minute == 30) ? 1 : null);
        $nowPosition = (2 * $hour) + $offset + 0.5;
        $until = $dateTimeTo-&gt;format('H:i');
    }

    if ($dateTimeFrom-&gt;format('Y-m-d') == $now-&gt;format('Y-m-d')) {

        $datax[] = $dateTimeFrom-&gt;format("H:i");
        $cost = $roundedInteger = (int)round( $tariff-&gt;value_inc_vat );
        $datay[] = $cost;
    }   
}
</code></pre>

<p>Drawing the graph uses the venerable <a href="https://jpgraph.net/">JPGraph</a>:</p>

<pre><code class="language-php">$path = 'jpgraph/';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');
require_once ('jpgraph/jpgraph_plotline.php');

// Size of graph
$width  = 600;
$height = 600;

// Setup the graph
$graph = new Graph($width,$height);
$graph-&gt;SetScale("intlin");
$graph-&gt;SetMargin(35,0,45,20); // L R T B

$graph-&gt;SetUserFont('dejavu/DejaVuSansMono.ttf');
$graph-&gt;title-&gt;SetFont(FF_USERFONT,FS_NORMAL,25);

$graph-&gt;SetBox(false);

$graph-&gt;title-&gt;Set( $now-&gt;format('l') . "'s Electricity Prices\n" . $costNow . "p / kWh until {$until}" );
$graph-&gt;title-&gt;SetColor('#000');

$graph-&gt;ygrid-&gt;Show(true);
$graph-&gt;xgrid-&gt;Show(true);

$graph-&gt;xaxis-&gt;SetTickLabels( $datax );

$graph-&gt;xaxis-&gt;SetColor('#000');
$graph-&gt;yaxis-&gt;SetColor('#000');

$graph-&gt;xaxis-&gt;SetFont(FF_USERFONT, FS_NORMAL, 10);
$graph-&gt;yaxis-&gt;SetFont(FF_USERFONT, FS_NORMAL, 14); 

// Just let the maximum be autoscaled
$graph-&gt;yaxis-&gt;scale-&gt;SetAutoMin(0); 

// Only show up until 23:00
$graph-&gt;xaxis-&gt;scale-&gt;SetAutoMax(46);

$graph-&gt;xaxis-&gt;SetTextLabelInterval(2); 
$graph-&gt;SetTickDensity(TICKD_DENSE, TICKD_DENSE);  

// Create the line plot
$p1 = new LinePlot($datay);
$graph-&gt;Add($p1);
$p1-&gt;SetStepStyle();
$p1-&gt;SetColor('#000');

//                 Direction, position,     colour@alpha, width
$l1 = new PlotLine(VERTICAL,  $nowPosition, 'black@.8',   13);
// Add vertical highlight line to the plot
$graph-&gt;AddLine($l1);

// Output line
$graph-&gt;Stroke();
</code></pre>

<h2 id="next-steps"><a href="https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/#next-steps">Next steps</a></h2>

<p>I dunno? Add some details about carbon emissions? Battery stats? Let me know what you think in the comments.</p>
<img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=48583&HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager">]]></content:encoded>
					
					<wfw:commentRss>https://shkspr.mobi/blog/2023/12/eink-display-for-octopuss-agile-energy-tariff/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
	</channel>
</rss>
