Skip to main content

python-pylontech

Python lib to talk to pylontech lithium batteries (US2000, US3000, ...) using RS485

What is this lib ?

This lib is meant to talk to Pylontech batteries using RS485. Sadly the protocol over RS485 is not some fancy thing like MODBUS but their own crappy protocol.

How to use this lib ?

First of all, you need a USB to RS485 converter. They are many available online for some bucks.

Then, you simply need to import the lib and start asking values:

>>> import pylontech
>>> p = pylontech.Pylontech()
>>> print(p.get_values())
Container:
    NumberOfModules = 3
    Module = ListContainer:
        Container:
            NumberOfCells = 15
            CellVoltages = ListContainer:
                3.306
                3.307
                3.305
                3.305
                3.306
                3.305
                3.304
                3.305
                3.306
                3.306
                3.307
                3.307
                3.308
                3.307
                3.306
            NumberOfTemperatures = 5
            AverageBMSTemperature = 29.81
            GroupedCellsTemperatures = ListContainer:
                29.61
                29.61
                29.61
                29.61
            Current = -3.5
            Voltage = 49.59
            Power = -173.565
            RemainingCapacity = 39.5
            TotalCapacity = 50.0
            CycleNumber = 5
    -->8-- SNIP -->8--
    TotalPower = -525.8022
    StateOfCharge = 0.79

>>> print(p.get_system_parameters())
Container: 
    CellHighVoltageLimit = 3.7
    CellLowVoltageLimit = 3.05
    CellUnderVoltageLimit = 2.9
    ChargeHighTemperatureLimit = 33.41
    ChargeLowTemperatureLimit = 26.21
    ChargeCurrentLimit = 10.2
    ModuleHighVoltageLimit = 54.0
    ModuleLowVoltageLimit = 46.0
    ModuleUnderVoltageLimit = 44.5
    DischargeHighTemperatureLimit = 33.41
    DischargeLowTemperatureLimit = 26.21
    DischargeCurrentLimit = -10.0

Dependencies

python-pylontech needs python 3.5 or greater (but please, use at least 3.7 or more if possible to be future-proof).

This lib depends on pyserial and the awesome construct lib.

How to run demos

TCP demo:

uv run python ./demos/test-tcp.py 192.168.1.7 10

Serial:

socat  -v pty,link=/tmp/serial,waitslave tcp:192.168.1.7:23,forever
# in another terminal
uv run python ./demos/test-serial.py /tmp/serial 1 

How to run mongodb collector

uv run poller 192.168.1.7 --mongo-url mongodb://mongodb.local:27017 --interval 1000 --interval 5

Hardware wiring

The pylontech modules talk using the RS485 line protocol.

Pylontech side

The first DIP switch on the pylontech indicates the line speed. It must be off (0, down position) so that the speed is set to 115200 Bd.

The RS485 port is exposed on the pins 7 & 8 on the RJ45 connector names RS485.

Client side

USB to RS485

Any RS485 to USB converter should would. You just have to wire the two pins above to the A and B ports (swap them around if it doesn't work). of your converter.

I personally use cheap chinese "RS485 to USB" converters worth a couple of bucks each.

TCP/IP

If you are using a Ethernet to RS485 bridge, connect as for USB and run the following command (Linux only) for creating a virtual serial port. Then run the python script with adapted serial parameter in the constructor.

socat pty,link=$HOME/bat2,waitslave tcp:<IP_ADDRESS>:<PORT>

This is tested with an USR-N540

Known bugs

Mixing between US2000 and US3000

If you are using US2000 and US3000 batteries, then the main battery must be a US2000. Please see bug https://github.com/Frankkkkk/python-pylontech/issues/2#issuecomment-915966564 for more information

FAQ

Using Pylontech LV Hub with multible battery banks

If the LV hub is used the address of the RS485 devices is depending on the battery bank. To read values the specific device address is needed. To scan for devices on a bank you can use the scan_for_batteries function. The max range is 0 to 255.

Metadata

Release files for python-pylontech-ext 0.4.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-pylontech-ext 0.4.5
File Size Uploaded
python_pylontech_ext-0.4.5.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-pylontech-ext 0.4.5
File Interpreter ABI Platform
python_pylontech_ext-0.4.5-py3-none-any.whl Python 3 none any Details

Total release size: 21.3 kB

Release files / python_pylontech_ext-0.4.5.tar.gz

Download URL python_pylontech_ext-0.4.5.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a07de032571c066d4e405e656ca1c534feaa8b210bbe08b878590beb037f0b4c
BLAKE2b-256 checksum
How to use checksums
66b3635456f7bbf967f07140fa9f1f5d0d63296d928dcad788afbc1d7247e1da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.9

Release files / python_pylontech_ext-0.4.5-py3-none-any.whl

Download URL python_pylontech_ext-0.4.5-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a95f420d7d10b2251fb16b12a70be6c6d417fcb42a0e0d3bd0382846915536cd
BLAKE2b-256 checksum
How to use checksums
a054a9c7ddf5a920b840d3bd095d323d79e626910eb292665c1c621036688670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.9

Release history Release notifications | RSS feed

This release

0.4.5 This release

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.1

2 release files

0.4.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page