UPDATE 22-06-2024: I improved the wiring diagram, so that you are able to supply the NodeMCU board with a different voltage than 5V.
I decided to embark on this project because the app provided by my solar inverter was clunky and didn’t offer an easy way to connect to Home Assistant. I wanted a seamless integration that allowed me to have full control over my solar panels, especially to automate shutting them down when energy prices go negative. This guide will walk you through how I achieved this using an ESP8266, MODBUS communication, and Home Assistant, giving you the ability to efficiently manage your solar energy system.
Before we start a quick explanation of all the terms used:
To follow the steps in this blog post, you’ll need the following:
Before proceeding, verify that you can get MODBUS serial to work with your inverter. I used a USB RS485 board for easy debugging with a trial of Modbus Poll. I have wasted a lot of time starting directly on the ESP8266 instead of starting with some isolated testing.
For this solution, I used an ESP8266 with a MAX485. Alternatively, you can use a UART TTL to RS485 board with automatic flow control, although I found the MAX485 more reliable. Setting up flow control on an ESPHome installation is just setting one configuration value.
Follow the schematic for these connections:
Update the connections accordingly if you use an ESP32.
The installation manual of my inverter contained a schematic to connect it through an RJ45 jack. I connected A to A and B to B. In my situation, the wire is very short, so I did not bother connecting ground. In a typical T-568B layout, that means white-orange to A and orange to B.
Ensure your Home Assistant is up and running. If not, set it up first. I suggest avoiding containers as they can be tricky with Home Assistant.
If you do not already have it, add the official ESPHome add-on to your Home Assistant.
Once set up, install ESPHome on your ESP device using the getting started guide.
Once ESPHome is set up, add the configuration.
You’ll need to set up the MODBUS connection first. See the configuration example below with added comments for clarification.
esphome:
name: solarinverter
friendly_name: Solar Inverter
esp8266:
board: esp01_1m
logger:
level: INFO
# On an ESP8266 you will NEED to disable serial logging
baud_rate: 0
# There will be some Home Assistant and Wi-Fi configuration here
# ...
# Setup the UART connection
uart:
id: mod_uart
# On a Lolin board the TX & RX pins are 1 & 3
tx_pin: 1
rx_pin: 3
# Follow your solar inverter's guide for the following settings
# My inverter documentation specifies 9600 8N1 which means '8' data bits, no 'N' parity bit, '1' stop bit
baud_rate: 9600
data_bits: 8
parity: NONE
stop_bits: 1
modbus:
id: modbus_connection
uart_id: mod_uart
# In the diagram we added the flow pins to GPIO 14 or D5
flow_control_pin: 14
modbus_controller:
- id: modbus_device
# The modbus device address to connect to, in the installer settings of my app it shows:
# Modbus device address: 3 (update this accordingly, I recommend figuring this out during debugging)
address: 0x3
modbus_id: modbus_connection
# Interval in seconds with which to poll all sensors
update_interval: 15s
setup_priority: -10
sensor:
# Here we will add the sensors
switch:
# Here we will add the switches
Translate the documentation of your inverter to the following template. A tip is that you can upload screenshots or text of the documentation to ChatGPT which may help you with transforming the documentation to YAML. The full yaml configuration for use with a SolPlanet ASW 5K-LT-G2 Pro inverter is available on my Github.
Also, check out the ESPHome documentation for MODBUS sensors.
sensor:
- platform: modbus_controller
modbus_controller_id: modbus_device
name: "Today of inverter"
# update the following fields to reflect the documentation
register_type: read
address: 1302
value_type: U_DWORD
device_class: energy
unit_of_measurement: "kWh"
accuracy_decimals: 1
# the state class is only useful for day or all-time totals, otherwise remove
state_class: total_increasing
# the amount of intervals this sensor may skip, remove if you want to update every cycle
skip_updates: 50
filters:
# multiply if necessary by following the documentation
- multiply: 0.1
# my sensor sometimes bugs to its max value, I fixed this by clamping to reasonable values
- clamp:
min_value: 0.1
max_value: 7000
ignore_out_of_range: true
# select a nice icon to show in Home Assistant
icon: "mdi:solar-power"
Also, check out the ESPHome documentation for MODBUS switches.
switch:
- platform: modbus_controller
modbus_controller_id: modbus_device
name: "PV Inverter ON/OFF"
# update the following fields to reflect the documentation
register_type: holding
bitmask: 1
address: 200
# select a nice icon to show in Home Assistant
icon: "mdi:toggle-switch"
As of the time of writing, ESPHome also supports other types of sensors:
Full local insight and control of your solar inverter.
Plus, you can now add this to your energy dashboard. I also have a dynamic energy contract for which I have an automation that automatically shuts down my solar panels if the price goes negative.