Skip to main content

Simplified communication with modbus batteries

Project description

HomeBattery

Overview

This project provides a set of Python modules to interact with various inverters and battery management systems. The modules allow for reading and writing parameters such as power setpoints, battery state of charge, and grid voltage.

Features

  • Support for multiple inverter brands:

    • Victron

    • Huawei

    • Deye (via Modbus and Solarman data logger)

    • Sessy

    • SMA

  • Communication via Modbus TCP

  • Reading and writing inverter and battery parameters

  • Asynchronous API support for Sessy battery system

  • Comprehensive register mapping for each inverter

Tested Devices

The following devices have been used to test the implementations in this project:

  • Victron Inverter: Victron MultiPlus-II
  • Huawei SUN2000 Inverter: Huawei SUN2000-6KTL-M1
  • Deye Inverter: Deye 12K-SG04LP3-EU
  • Sessy Battery System: Sessy
  • SMA Inverter: Sunny island 8.0H-13

Installation

For PyPi installation please run:

python3 -m pip install HomeBattery

For local installation please run this command:

python3 -m pip install .

Usage

from HomeBattery import DeyeModbusInverter, DeyeSolarManInverter, SessyInverter, Sun2000Inverter, VictronInverter

sessybattery = SessyInverter(...)

Modules

1. modbus.py

Contains utility classes for interacting with Modbus devices:

  • ModbusDevice: Base class for Modbus communication. This class provides the foundational methods and properties required to communicate with Modbus devices. It handles the setup of communication parameters, sending and receiving Modbus requests, and processing responses.

  • Register: Data class that encapsulates the properties and behaviors of a Modbus register, providing methods to decode and build payloads based on the register's data type and other attributes.

  • AccessType and DataType: Enums for defining register properties. AccessType specifies the read/write access for a register, while DataType Defines the type of data stored in the register.

2. battery.py

Defines battery management classes:

  • Battery: Base battery class

  • ModbusBattery: Extends Battery to support Modbus communication

3. victron.py

Handles communication with Victron inverters:

  • Inherits from ModbusBattery

  • Supports reading active power, battery SOC, and grid voltage

  • Allows setting power and current setpoints

  • Implements comprehensive register mapping

4. sun2000.py

Handles communication with Sun2000 inverters:

  • Inherits from ModbusBattery

  • Supports reading active power, battery SOC, and grid voltage

  • Allows setting power and current setpoints

  • Implements comprehensive register mapping

5. deye_modbus.py

Handles communication with Deye inverters via Modbus:

  • Inherits from ModbusBattery

  • Supports reading active power, battery SOC, and grid voltage

  • Allows setting power and current setpoints

  • Implements comprehensive register mapping

6. deye_solarman.py

Handles communication with Deye inverters via Solarman data logger:

  • Inherits from Battery

  • Uses pysolarmanv5 for communication

  • Similar functionality to deye_modbus.py but using the Solarman API

7. sessy.py

Handles communication with Sessy Battery System:

  • Uses aiohttp for asynchronous API communication with the Sessy API

  • Supports reading active power, battery SOC, and grid voltage

  • Allows setting power and current setpoints

8. sma.py

Handles communication with SMA inverters:

  • Inherits from ModbusBattery

  • Supports reading active power, battery SOC, and grid voltage

  • Allows setting power and current setpoints

  • Implements comprehensive register mapping

Usage

Example usage for interacting with a Victron inverter:

Warning: Using this library to interact with your inverters and battery systems may result in the loss of settings or configurations. Ensure you have backed up all important settings before proceeding. Use this library at your own risk.

from victron import Victron

# Initialize the Victron inverter
victron_inverter = Victron(ip="192.168.1.100", port=502)

# Read active power
active_power = victron_inverter.get_active_power()
print(f"Active Power: {active_power} W")

# Read battery state of charge (SOC)
battery_soc = victron_inverter.get_battery_soc()
print(f"Battery SOC: {battery_soc} %")

# Read grid voltage
grid_voltage = victron_inverter.get_grid_voltage()
print(f"Grid Voltage: {grid_voltage} V")

# Set power setpoint
victron_inverter.set_power_setpoint(1, 1500)
print("Power setpoint set to 1500 W on phase 1")

Implementing a New Battery

To implement a new battery, follow these steps:

  1. Define the Battery Class: Create a new class that inherits from the Battery or ModbusBattery class, depending on the communication protocol used by the new battery system.

  2. Initialize the Battery: Implement the __init__ method to initialize the battery-specific parameters and call the superclass constructor.

  3. Register Mapping: If using Modbus communication, define the register mapping for the new battery system. You can use an Enum class to map the register addresses with their respective parameters, or you can use a dictionary or any other suitable data structure that fits your needs.

  4. Implement Required Methods: Define methods to read and write battery parameters such as active power, state of charge (SOC), and grid voltage. Use the provided templates as a guide.

Example Battery Implementation

from battery import Battery

class MyNewBattery(Battery):
    def __init__(
        self, 
        capacity: int = None, 
        max_charge_power: int = None, 
        max_discharge_power: int = None, 
        soc: int = None
    ):
        super().__init__(capacity, max_charge_power, max_discharge_power, soc)
        # Initialize battery-specific parameters here

    def get_active_power_total(self):
        # Implement logic to get active power total
        pass

    def get_battery_soc(self):
        # Implement logic to get battery SOC
        pass

    def get_grid_voltage(self):
        # Implement logic to get grid voltage
        pass

    def set_power_setpoint(self, setpoint: int):
        # Implement logic to set power setpoint
        pass

Example Modbus Battery Implementation

from enum import Enum
from battery import ModbusBattery
from modbus import Register, AccessType, DataType

class MyNewModbusBattery(ModbusBattery):
    def __init__(
        self, 
        ip: str, 
        port: int = 502, 
        timeout: int = 3, 
        capacity: int = None, 
        max_charge_power: int = None, 
        max_discharge_power: int = None, 
        soc: int = None
    ):
        super().__init__(ip, port, timeout, capacity, max_charge_power, max_discharge_power, soc)

    def get_active_power(self):
        return self.read_holding_register(self.NewBatteryRegisters.active_power_total.value)

    def get_battery_soc(self):
        return self.read_holding_register(self.NewBatteryRegisters.battery_soc.value)

    def get_grid_voltage(self):
        return self.read_holding_register(self.NewBatteryRegisters.grid_voltage.value)

    def set_power_setpoint(self, setpoint: int):
        self.write_register(self.NewBatteryRegisters.power_setpoint.value, setpoint)

    class NewBatteryRegisters(Enum):
        grid_voltage = Register(30000, "grid_voltage", DataType.UINT16)
        battery_soc = Register(30001, "battery_soc", DataType.UINT16, gain=0.01)
        active_power_total = Register(30002, "active_power_total", DataType.UINT16, gain=10)
        power_setpoint = Register(30003, "power_setpoint", DataType.INT16, access_type=AccessType.READ_WRITE)

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

homebattery-0.0.4.tar.gz (63.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

homebattery-0.0.4-py3-none-any.whl (56.2 kB view details)

Uploaded Python 3

File details

Details for the file homebattery-0.0.4.tar.gz.

File metadata

  • Download URL: homebattery-0.0.4.tar.gz
  • Upload date:
  • Size: 63.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for homebattery-0.0.4.tar.gz
Algorithm Hash digest
SHA256 78809445fd2e5468cb3411be9a05f2fc83591a4f789b691c31113fa4fcc95b57
MD5 6faa0d915cfbdc0064d9c32922e50911
BLAKE2b-256 bc1a37f7ed0f35b159f592dc3750eb58250cc41ecc6ab85d09f23c79f70b71bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for homebattery-0.0.4.tar.gz:

Publisher: workflow.yml on ElaadNL/HomeBattery

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file homebattery-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: homebattery-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 56.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for homebattery-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 82d531052f39f60aa5ec2939e40a5c864128176e7f5446dc95d1914f20d4703d
MD5 da290ca3013197b76ab35fcf2a5164e8
BLAKE2b-256 ad03b7502d097192867dd5fd5b395750f831bd588b6da1d59ebf04251074106c

See more details on using hashes here.

Provenance

The following attestation bundles were made for homebattery-0.0.4-py3-none-any.whl:

Publisher: workflow.yml on ElaadNL/HomeBattery

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page