Automation with time calculation

Hi all,

This was a question, but became my own rubber duck - then I thought I’d actually finish the post to help anyone else (or more likely future me) do something similar.

Of course there might still be a better way for me to do this - so suggestions welcome.

Cheers,

With a recent change in electricity export tariff I am now looking to fully cycle my home battery each day.

So I’d like to have HA automatically start a “force discharge” on the battery in the run up to the start of the cheap overnight import period.

I have put this concept together as a long list of conditions all ORed together:

  • If it’s after 22:10 AND the battery has more than x
  • if it’s after 22:30 AND the battery has more than (x minus a bit)
  • etc…

It’s a crude way of asking if I’ve got “enough energy to discharge at full power for the rest of the day”, and has validated my “force discharge” and “stop discharge” actions.

I would like to make this significantly more elegant, by taking the time left in the day (seconds or minutes), scaling that by the discharge power, and comparing that value with the reported state of charge.
That would be a more complex automation, but one with just one test, which would be valid for any time of day - and therefore easier to adjust (and read).

Having tried to clarify what I was doing here - my inner rubber duck spoke up…

I’ve ended up by creating a helper template which takes the start of the next off peak time, subtracts “now” in seconds to get the time remaining, then divides by 3600 to get that time in hours, and finally multiplies by the measured output of my inverter in force export mode.

{{ ((states('sensor.octopus_intelligent_next_offpeak_start')|as_datetime|as_local - now()).seconds / 3600 * 3.15) }}

I’m then going to use an “every five minutes” automation to check whether I have more energy in the battery than I need to force discharge - and if I do… then start to force discharge.
I have some extra work to do in order to make sure than an early “octopus intelligent” slot doesn’t cause me to dump the battery early in the day resulting in me running out before the end of the day when I didn’t need to.