Home Assistant Automations

I use Home Assistant to control various smart devices around our home, including our Tesla Powerwall.

We are charged wholesale power prices by our electricity retailer Amber Electric, which means that sometimes the price of our power is really low, and sometimes it is really high. I can therefore use my battery to charge from the grid when the price is low, and sell back to the grid with the price is high.

Amber has a product called “Smart Shift” which can control when my battery buys and sells. It works reasonably well, but sometimes I prefer to control the battery myself, and have set up some automations to do this.

The end result is a simple panel when lets me set the Buy and Sell price, and how I’d like the battery to operate:

  • How low will I let it discharge?
  • How high will I let it charge?
  • How full must the battery be before I sell power?
  • How low should the battery charge drop before I stop selling power?

These things are defined in Home Assistant as Helpers. They’re basically variables that you can set.

I have three scripts which control the battery. They define standard actions which I’d like to run at different times:

“Self Consumption” tells the battery to supply power to the house. When I don’t want to buy or sell or use grid power.

“Fast Charge” tells the battery to charge up from the grid – usually when the power price is cheap.

“Autonomous” tells the battery to look at the “Utility Rate Plan” in the Tesla settings, and decide whether or not to sell power.

The prices are artificial. I enter them via the Powerwall App. Basically they tell the Powerwall that power prices are expensive between 6:00am and 8:30am, and between 4:00pm and 9:00pm. Therefore if the battery ever switches to “Autonomous Mode” at these times, it will try to sell power. At other times, it will do nothing.

Based on this information I have set up four more scripts:

“Battery Mode Buy” does this:

  • Change the Battery Mode helper to display “Buy”.
  • Send me a message via MQTT
  • Change the battery reserve to its maximum (usually 100%)
  • Run the “Fast Charge” script.

“Battery Mode Consume” does this:

  • Change the Battery Mode helper to display “Consume”.
  • Send me a message via MQTT
  • Change the battery reserve to its minimum (usually 15%)
  • Run the “Self Consumption” script.

“Battery Mode Hold” does this:

  • Change the Battery Mode helper to display “Hold”.
  • Send me a message via MQTT
  • Change the battery reserve to its current charge level. This forces the battery to stay at this level, neither importing nor exporting.
  • Run the “Self Consumption” script.

“Battery Mode Sell” does this:

  • Change the Battery Mode helper to display “Sell”.
  • Send me a message via MQTT
  • Run the “Autonomous” script.

Now for the automations:

The “Charge Battery” automation is triggered by one of two events:

  1. If the general power price (from the Amber API) is less than my specified “Buy Price”, or
  2. The general power price (from the Amber API) is negative (some days they pay us to consume power)

There is also a precondition, which must be true: The “Demand Tariff” must not be active. We get charged additional fees for any consumption during the demand period, so I try to avoid consumption at that time.

“Demand Tariff Active” is a binary sensor helper:

The “Consume Battery” Automation is triggered by one of three events:

  1. The Power Price exceeds the “Buy Price”
  2. The “Demand Tariff” changes from OFF to ON
  3. The Power Price exceeds the “Max Hold Price”

“Max Hold Price” is a binary sensor variable I set up in “sensor.yaml”:

The “Dispatch Begin” (Start Selling) automation is triggered whenever the Feed In Tariff is higher than my minimum sell price.

It will only run if the battery level is above the minimum “sell” charge level, which I have currently set to 60%.

The “Dispatch End” (Stop Selling) automation is triggered by one of two events:

  1. The “Feed In Price” falls below my “Sell Price”, or
  2. The Battery Charge Level falls below my minimum charge percentage (currently 40%).

The “Preserve Battery” Automation is triggered whenever the power price falls below the maximum hold price.

The power price must be above my “Buy” price (otherwise I’d be buying, not preserving), and the Demand Tariff must be off.

All of these automations rely on the “Teslemetry” integration which gives me greater control over my battery than the standard Tesla integration. It’s a paid service, but it’s cheap, and it always works.

If you need me to clarify anything, please leave a comment.

3 Replies to “Home Assistant Automations”

  1. Hi, thanks for publishing this. This is probably the most detailed post I have seen by someone automating beyond what Amber Smartshift offers. I have 3xPW2’s, 3xSolarEdge Inverters for ~17 kw Solar & a Tesla EV as well and have recently re-joined Amber.

    I was curious to hear if you have been able to observe the results/track better ROI through this? Does having these automations in any way interfere with Amber’s Smartshift? I.e can they co-exist together or do you have to disable it entirely? I was specifically interested in using their Solar Export Curtailment feature (esp in Summer) as I no longer have the ability to do this myself on the SolarEdge inverters as they don’t offer a free/local API.

    I’m planning to develop a similar automation as well, but use additional logic to also ‘smart charge’ my EV when Feed-In Tariff is low/negative. Also, given my large PW2 capacity and limited peak household consumption, I want to take advantage of price spikes and force feed back a specific amount (eg 13.5 kwh) into grid. Looks like the ony way to manually force feed PW2 back to the grid is by setting up the tariff’s in the Tesla app?

  2. Hi

    Thanks for reading my article.

    Congratulations on having such an impressive setup.

    The automations do what they are designed to do, but the downside was that the system required daily attention in order to work out what my “buy” and “sell” positions were for the day. If I forgot to do this, it was easy to end up paying too much or having an empty battery.

    In answer to your question, you need to disable Smartshift if you enable HA Automations. Otherwise the two things compete with each other, and you have no certainty about what will happen.

    I toyed with the NetZero app for a while, and found it useful because it was able to upload the actual Amber prices into the Tesla Utility Rate plan. That’s really handy. My utility rate plan used to be manually set, which would make it impossible to dispatch to the grid outside of the times I had earmarked in my artificial prices. That app used to be free, but I think you have to pay for it now. Incidentally, NetZero played nicely with Smartshift, so it was possible to have both services enabled simultaneously.

    With regards to curtailment, we don’t have that either, but I work around that by having a programmable switch on the hot water system. If the FIT ever goes negative, I make sure the hot water switch is on. If the FIT goes really negative, I manually intervene and take the house off-grid. It’s not ideal, but I don’t know of an easier solution.

    I wrote some code to work out optimal buy and sell prices, but on cloudy days I ended up with buy prices higher than sell prices, which says to me that I got it wrong 🙂

    I’m an I.T. professional by trade, and this is the first time in my life that errors in my code have an immediate and tangible cost to my hip pocket, which is quite alarming.

    In the end, I disabled the automations and let Amber Smartshift do its thing. I decided their logic was better than mine, and I didn’t want to have to worry about it every day.

    So far it has been good. Our electricity bill has been in credit for the last 9 months, and the only days where we end up with mediocre results are on cold cloudy winter days – which are rare in Brisbane.

    Sorry I couldn’t be more help. I’m not sure if I’ve answered all your questions – if not, please let me know and I’ll do what I can to help.

  3. All good…thanks Neil for your response. It is still very helpul. I work in tech too, but well past my coding days. I just like playing round with tech and could’nt agree with you more – these experiments can come back and hurt your back pocket 🙂 if you are not careful…

    I was using Netzero as well but mainly for simple automations to allow Grid Charging of my PW2’s on a set schedule (Free 3hr electricity plans). I was aware they had Amber integration but I did’nt use it to it’s full potential esp after they went to a paid model. I have the Amber integration setup in HA so in theory I have access to the live prices & should be able to replicate what was possible in Netzero. I’m in Melbourne and thought this is right season to experiment as our household consumption is low during spring before peaking in Summer. The 3rd PW2 was just added last week, so in a way I want to use its capacity exclusively for grid exports to maximise my earnings/ROI and retain the other 2 for self consumption during Spikes/Peak Periods.

    It’s a bummer I need to turn off Smartshift entirely as I can’t seem to figure out how to setup curtailment without it as I no longer have API access to my inverters (I think it expired after 2 years). I might have to find other ways of doing this…

Leave a Reply

Your email address will not be published. Required fields are marked *