Because computers are, well, computers, and at some point everything will need rebuilding from scratch after a restore-from-backup fails, I thought I should maybe put some of my home automation stuff on ye olde blog. The irony of documenting things on a self-hosted blog is not lost on me, btw.

Anyhoo, I recently moved onto a new tariff from Octopus, called Octopus Flux, which has a spell of cheap power overnight, and expensive power at peak times (4pm to 7pm). As I have a storage battery, I’d quite like to ensure that I have enough power stored up to get me through the peak time, so I don’t pay a third extra for those hours. Also, because any electricity fed back into the grid is paid at a lower rate than I have to buy it at, so I don’t want to fully charge the battery overnight, or anything that the solar generates will be fed back into the grid.

After running myself round in knots about how to achieve this, I ended up with the following:

When the off-peak period starts, work out how much charge I’m going to need to cover

  • from the end of cheap power until my solar starts generating
  • the three hours of expensive peak time
  • as much of peak-time to cheap-time as possible
  • as much solar-shortfall as possible during the day, in case of poor weather.

once that is sorted, fashion an http request against the API on my battery to say “charge to X”. This buys any cheap electricity that I need.

When the off-peak price comes to an end, I then send another request to the battery with a new desired charge-level. This allows the battery to discharge down to (for example) 20%, which means I draw energy from the battery instead of from the grid.

Once peak time arrives, a further call is made to the battery API to set another, even lower desired charge state (for example 5%), so that the battery can discharge during what I have termed “pricey electric time”.

In Node-RED that looks something like this. The node-red GUI representation of my battery charging ensmartification

After a week or two of experimenting, I figured out that the easiest/best way was to use fixed values for the two backup level reductions at 5am and 4pm, and only do calculations at 2am when the off-peak price kicks in, so I’m not buying in electricity which will be provided by my PV system later in the day.

I pull in a couple of entities from me home assistant install, namely the estimated output from my solar panels, and the current actual charge level of the storage battery, then do a little jiggery-pokery, and create the http request message which gets passed along to the “Set Min Backup” http request node.

That jiggery-pokery looks a little like this:

var mincharge = "30" //percent storage needed to get to sunrise + cover peak hours
var solarpercent = msg.forecast*10 //convert solar forecast to percentage of battery capacity
var topup = 100 - solarpercent + mincharge // work out what level to set the minimum charge to

// if topup target less than minimum charge level, 
// then topup target = minimum charge level
if (topup <= mincharge){
  topup = mincharge

msg.topuplevel = topup //add field for telegram message

msg.payload = "EM_USOC=" +parseInt(topup)
msg.headers = {}
msg.headers['Auth-Token'] = 'hah-lolno-i-am-redacting-this-obviously'
msg.headers["Content-Type"] = "application/x-www-form-urlencoded"
return msg;

You’ll notice that I’ve used topup as a variable name, which doesn’t really make sense, because that’s not what that value refers to. Hurray for technical debt, and iterating on code without changing variable names 😬

The flow splits up after the calculations are done, one part calling the battery API via the http request node, then outputting to the Node-RED debug nodes, and the other part taking the msg.topuplevel field, formatting it into a sensible message for me, then passing it off to a sub-flow which hooks into my home assistant telegram bot. If you get the reference in the sub-flow name, you’re either a film buff, or old 😉

Anyway, if you’ve read all of that, ten internet points to you. If you think I’m a clown who could do it much better, or if you’d like me to post the raw Node-RED flow so you can play along at home, then hit me up on the fediverse via the mastodon link on the left.