Skip to content

Automating your system

From posts by bigbrovar, Niyi Solar, Net NL, AshipaEk0, LifeofDan-EL and Sanusi in the DIY Solar Nigeria group (April 2024 to March 2026). The code is written for this wiki, modelled on what those members run.

What should I automate, and how do I actually set it up?

You want Use Code needed
Alerts, a daily summary and a forecast Epilykos None
A load on a fixed timetable The smart plug’s own app None
A load that switches by battery level, load or forecast Home Assistant The recipes below

For anything beyond a timer you need three things in Home Assistant:

  1. Your inverter’s readings, at least battery percentage and load power. How you get them depends on the inverter. See How to monitor your inverter and battery.
  2. A switch for the load: a smart plug or relay that Home Assistant can control.
  3. The automation, pasted in from below.

Start here if you have not automated anything. From its documentation, Epilykos will:

  • Alert you when a reading crosses a line you set, or when the grid goes off. Alerts go by push notification, Telegram, Discord, ntfy or a webhook.
  • Send a daily summary each evening.
  • Answer on Telegram. Its bot replies to /status and /today.
  • Show tomorrow’s solar forecast, from a free service or from Solcast.
  • Change inverter settings from its Controls page on supported equipment. Each change is checked against the allowed range and read back from the device to confirm it.

What its documentation does not describe is a way to set rules such as “switch this plug on when the battery reaches 95%”. Epilykos reads from Home Assistant, MQTT and Tuya devices, so the two run side by side: Epilykos for the dashboard and alerts, Home Assistant for the rules.

In Home Assistant go to Settings, then Automations and scenes, then Create automation. Open the menu at the top right, choose Edit in YAML, and paste the recipe in place of what is there.

Then change the names to match your own devices. In these recipes:

Name in the recipe Stands for
sensor.battery_soc Your battery percentage
sensor.load_power The house load in watts
switch.freezer_plug, switch.water_heater The smart plug or relay on that load
switch.grid_charging Whatever turns your inverter’s grid charging on and off

The recipes use the wording of Home Assistant 2024.10 and later.

Recipe 1: run a freezer only in solar hours

Section titled “Recipe 1: run a freezer only in solar hours”

bigbrovar’s advice for a small battery is a freezer or fridge that runs from about 8 am to 4 pm. Most smart plug apps can do this by themselves. In Home Assistant:

alias: Freezer on solar hours
description: On at 8 am, off at 4 pm
mode: single
triggers:
- trigger: time
at: "08:00:00"
id: "on"
- trigger: time
at: "16:00:00"
id: "off"
actions:
- action: "switch.turn_{{ trigger.id }}"
target:
entity_id: switch.freezer_plug

Recipe 2: run a water heater on surplus solar

Section titled “Recipe 2: run a water heater on surplus solar”

This is Niyi Solar’s rule set. The heater comes on when the battery is nearly full, goes off when the battery falls, never runs when the house is heavily loaded, and runs for at least 45 minutes once started.

alias: Water heater on surplus solar
description: Heat water only when the battery is nearly full
mode: single
triggers:
- trigger: time_pattern
minutes: "/5"
id: check
- trigger: numeric_state
entity_id: sensor.load_power
above: 7000
id: overload
actions:
- choose:
# The house load is too high: switch off at once
- conditions:
- condition: trigger
id: overload
sequence:
- action: switch.turn_off
target:
entity_id: switch.water_heater
# Battery nearly full and the load is modest: switch on
- conditions:
- condition: numeric_state
entity_id: sensor.battery_soc
above: 95
- condition: numeric_state
entity_id: sensor.load_power
below: 6500
- condition: state
entity_id: switch.water_heater
state: "off"
sequence:
- action: switch.turn_on
target:
entity_id: switch.water_heater
# Battery has fallen and the heater has had its 45 minutes: switch off
- conditions:
- condition: numeric_state
entity_id: sensor.battery_soc
below: 92
- condition: state
entity_id: switch.water_heater
state: "on"
for: "00:45:00"
sequence:
- action: switch.turn_off
target:
entity_id: switch.water_heater

Change the figures to suit your system. Three ideas in it are worth keeping whatever you change:

  • A gap between on and off (95% and 92%), so the heater does not switch every few seconds.
  • A check on total load, so the automation never overloads the inverter.
  • A minimum run time, so passing clouds do not cause constant switching.

Recipe 3: shed loads when the battery is low

Section titled “Recipe 3: shed loads when the battery is low”

The same idea in reverse. When the battery falls to a level you choose, switch off what you can live without, and bring it back once the battery recovers.

alias: Shed loads on low battery
description: Protect the battery overnight
mode: single
triggers:
- trigger: numeric_state
entity_id: sensor.battery_soc
below: 30
id: shed
- trigger: numeric_state
entity_id: sensor.battery_soc
above: 50
id: restore
actions:
- choose:
- conditions:
- condition: trigger
id: shed
sequence:
- action: switch.turn_off
target:
entity_id:
- switch.freezer_plug
- switch.water_heater
- conditions:
- condition: trigger
id: restore
sequence:
- action: switch.turn_on
target:
entity_id: switch.freezer_plug

Only the freezer is restored. The water heater is left to Recipe 2.

Recipe 4: charge from the grid before a poor day

Section titled “Recipe 4: charge from the grid before a poor day”

bigbrovar’s own system reads a solar forecast, predicts the next day’s yield and decides whether to allow charging from the grid. A simple version of that:

  1. Add a forecast to Home Assistant. The built-in Forecast.Solar integration is free. You give it your location and your array’s size, tilt and direction, and it provides a sensor for tomorrow’s expected production in kWh.
  2. Decide how many kWh counts as a poor day for you. It is roughly what your house uses in a day.
  3. Paste this in, with your own figure in place of 8.
alias: Charge from grid before a poor day
description: Allow grid charging overnight when tomorrow looks poor
mode: single
triggers:
- trigger: time
at: "21:00:00"
id: evening
- trigger: time
at: "06:00:00"
id: morning
actions:
- choose:
- conditions:
- condition: trigger
id: evening
- condition: numeric_state
entity_id: sensor.energy_production_tomorrow
below: 8
sequence:
- action: switch.turn_on
target:
entity_id: switch.grid_charging
- conditions:
- condition: trigger
id: morning
sequence:
- action: switch.turn_off
target:
entity_id: switch.grid_charging

How you switch grid charging depends on your inverter. On some it is a setting the monitoring integration exposes. On others the simplest way is a contactor on the inverter’s grid input, operated by a smart relay.

AshipaEk0 tracks how long the grid was on each day. This one is a sensor, not an automation. It goes in your configuration.yaml file, and needs something that tells Home Assistant whether the grid is present.

sensor:
- platform: history_stats
name: Grid on today
entity_id: binary_sensor.grid_present
state: "on"
type: time
start: "{{ today_at('00:00') }}"
end: "{{ now() }}"

The result is the number of hours of supply so far today. Home Assistant’s history statistics page shows how to get weekly and monthly figures the same way.

LifeofDan-EL keeps a similar record of each day’s highest solar output and highest load, with the time each happened. Figures like these tell you whether the system is sized right.

  • Slow the charge as the battery fills. bigbrovar’s system reduces the charge current from 160 A down to 30 A at 98%, on a 560 Ah bank, so the balancer has time to work. That needs a system where the battery, inverter and charge controllers all report to one controller. He runs his automations in Node-RED on a Victron system. See Top balancing lithium cells.
  • No electronics at all. One member’s water pumps are started and stopped by float switches in the overhead tanks.

For writing your own, see Home Assistant’s automation documentation.

Switching a water heater. For a member planning to use the Shelly Plus 1PM, which switches a load and measures its power, bigbrovar’s advice was to buy the fourth generation, which is more stable and has an overheat alarm. That alarm matters on a high current heating load such as a water heater.

Zigbee devices. Net NL’s primer:

  • You need a coordinator, which is the device people loosely call a hub. A coordinator on the network cable gives you more freedom over where to place it than a USB one.
  • For software there are two common choices. He prefers Zigbee2MQTT, which he finds has the larger community, and its website lists the devices it supports.
  • Zigbee is a mesh. Mains-powered devices act as routers and extend the network. Battery devices do not, but their batteries last a year or more.

Controlling an air conditioner. An infrared blaster lets an automation operate an air conditioner as its remote would. Net NL’s experience across five of them:

  • Choose one that works over your own network. His Broadlink units are set up once in their app and then run locally.
  • A very cheap Wi-Fi unit he owns works only through its maker’s cloud. With no internet, it does nothing.
  • Zigbee blasters exist in mains-powered and battery-powered forms. The battery ones are easier to place.
  • Check the switch’s rating against the load. A small smart plug is not built to switch a water heater or a pump. Use a device rated for the current, or have the smart device operate a contactor that does the switching.
  • Prefer devices that work without the internet. An automation that protects your battery should not stop because the connection is down.
  • Test with the load unplugged. Watch an automation switch an empty plug for a day before you trust it with a heater.

Articles are licensed under CC BY-NC-SA 4.0: share and adapt with credit, not for commercial use. Licence details.