Skip to main content

Get values from modbus energy meter to MQTT / HomeAssistant

Project description

energymeter2mqtt

tests codecov energymeter2mqtt @ PyPi Python Versions License GPL-3.0-or-later

Get values from modbus energy meter to MQTT / HomeAssistant

Energy Meter -> modbus -> RS485-USB-Adapter -> energymeter2mqtt -> MQTT -> Home Assistant

The current focus is on the energy meter "Saia PCD ALD1D5FD" However, the code is kept flexible, so that similar meters can be quickly put into operation.

Quick start

Overview:

  • Clone the sources
  • Create your config: ./cli.py edit-settings
  • Test: ./cli.py print-values
  • Setup and start MQTT publishing: sudo ./cli.py systemd-setup

Note: It's a good idea to use the /dev/serial/by-id/{your-device-id} path as serial port, instead of /dev/ttyUSB1 Call udevadm info -n /dev/ttyUSB* to get information about all USB serial devices and ls -l /dev/serial/by-id/ to see the existing links.

~$ git clone https://github.com/jedie/energymeter2mqtt.git
~$ cd energymeter2mqtt
~/energymeter2mqtt$ ./dev-cli.py --help
usage: ./cli.py [-h]
                {debug-settings,edit-settings,print-definitions,print-registers,print-values,probe-usb-ports,publish-l
oop,systemd-debug,systemd-logs,systemd-remove,systemd-setup,systemd-status,systemd-stop,version}



╭─ options ──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ -h, --help        show this help message and exit                                                                  │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ subcommands ──────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ {debug-settings,edit-settings,print-definitions,print-registers,print-values,probe-usb-ports,publish-loop,systemd- │
│ debug,systemd-logs,systemd-remove,systemd-setup,systemd-status,systemd-stop,version}                               │
│     debug-settings                                                                                                 │
│                   Display (anonymized) MQTT server username and password                                           │
│     edit-settings                                                                                                  │
│                   Edit the settings file. On first call: Create the default one.                                   │
│     print-definitions                                                                                              │
│                   Print RAW modbus register data                                                                   │
│     print-registers                                                                                                │
│                   Print RAW modbus register data                                                                   │
│     print-values  Print all values from the definition in endless loop                                             │
│     probe-usb-ports                                                                                                │
│                   Probe through the USB ports and print the values from definition                                 │
│     publish-loop  Publish all values via MQTT to Home Assistant in a endless loop.                                 │
│     systemd-debug                                                                                                  │
│                   Print Systemd service template + context + rendered file content.                                │
│     systemd-logs  Show systemd service logs. (May need sudo)                                                       │
│     systemd-remove                                                                                                 │
│                   Stops the systemd service and removed the service file. (May need sudo)                          │
│     systemd-setup                                                                                                  │
│                   Write Systemd service file, enable it and (re-)start the service. (May need sudo)                │
│     systemd-status                                                                                                 │
│                   Display status of systemd service. (May need sudo)                                               │
│     systemd-stop  Stops the systemd service. (May need sudo)                                                       │
│     version       Print version and exit                                                                           │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

start development

~$ git clone https://github.com/jedie/energymeter2mqtt.git
~$ cd energymeter2mqtt
~/energymeter2mqtt$ ./dev-cli.py --help

dev CLI

usage: ./dev-cli.py [-h]
                    {coverage,create-default-settings,install,lint,mypy,nox,pip-audit,publish,test,update,update-readm
e-history,update-test-snapshot-files,version}



╭─ options ──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ -h, --help        show this help message and exit                                                                  │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ subcommands ──────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ {coverage,create-default-settings,install,lint,mypy,nox,pip-audit,publish,test,update,update-readme-history,update │
│ -test-snapshot-files,version}                                                                                      │
│     coverage      Run tests and show coverage report.                                                              │
│     create-default-settings                                                                                        │
│                   Create a default user settings file. (Used by CI pipeline ;)                                     │
│     install       Install requirements and 'energymeter2mqtt' via pip as editable.                                 │
│     lint          Check/fix code style by run: "ruff check --fix"                                                  │
│     mypy          Run Mypy (configured in pyproject.toml)                                                          │
│     nox           Run nox                                                                                          │
│     pip-audit     Run pip-audit check against current requirements files                                           │
│     publish       Build and upload this project to PyPi                                                            │
│     test          Run unittests                                                                                    │
│     update        Update dependencies (uv.lock) and git pre-commit hooks                                           │
│     update-readme-history                                                                                          │
│                   Update project history base on git commits/tags in README.md Will be exited with 1 if the        │
│                   README.md was updated otherwise with 0.                                                          │
│                                                                                                                    │
│                   Also, callable via e.g.:                                                                         │
│                       python -m cli_base update-readme-history -v                                                  │
│     update-test-snapshot-files                                                                                     │
│                   Update all test snapshot files (by remove and recreate all snapshot files)                       │
│     version       Print version and exit                                                                           │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Backwards-incompatible changes

v0.6.2

Config value slave_id was renamed to device_id, like in pymodbus v3.10.0

History

  • v0.6.3
    • 2025-09-25 - Fix unsupported sensor combination
    • 2025-09-17 - Rename "slave_id" to "device_id" (Also in the config file!)
    • 2025-09-17 - Log exceptions better
    • 2025-09-17 - Add "systemd-logs" to cli
    • 2025-09-17 - Update requirements
  • v0.6.1
    • 2025-09-13 - Apply manageprojects updates
  • v0.6.0
    • 2025-04-22 - Update ha-services from v0.6 to v2.10
  • v0.5.0
    • 2025-04-22 - Switch pip-tools to uv
    • 2024-09-06 - Update requirements
Expand older history entries ...
  • v0.4.0
    • 2024-09-04 - "retry_on_empty" -> "retries"
    • 2024-09-04 - update project via manageprojects
  • v0.3.0
    • 2024-07-12 - bugfix packaging
    • 2024-07-12 - Bugfix wrong path loading definitions
    • 2024-07-12 - Update requirements adn split CLI code
    • 2024-02-22 - Update requirements
    • 2024-01-01 - Update README.md
  • v0.2.0
    • 2023-08-29 - NEW command "probe-usb-ports"
    • 2023-08-29 - update requirements
    • 2023-08-29 - Remove nonsens doc string
  • v0.1.2
    • 2023-08-10 - adjust scale factor for double registers
    • 2023-06-27 - fix: adjust scale factor for double registers
  • v0.1.1
  • v0.1.0
    • 2023-05-21 - fix README example
    • 2023-05-21 - README
    • 2023-05-21 - Bugfix unit of "Power Factor (cos phi)"
    • 2023-05-21 - Bugfix systemd config
    • 2023-05-21 - Add logging to get_ha_values()
    • 2023-05-21 - fix wait
    • 2023-05-21 - Bugfix endless loop prints
    • 2023-05-21 - Bugfix voltage scale
    • 2023-05-21 - Publish all values via MQTT to Home Assistant in a endless loop.
    • 2023-05-21 - Fix CI
    • 2023-05-21 - More info in README
    • 2023-05-21 - Split commands
    • 2023-05-21 - Split CLI and use toml settings for energy meter modbus info
    • 2023-05-21 - Working "serial-test" with Saia PCD ALD1D5FD
    • 2023-04-28 - WIP: Test Serial connection
    • 2023-04-30 - Update README.md
    • 2023-04-28 - first commit

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

energymeter2mqtt-0.6.3.tar.gz (83.1 kB view details)

Uploaded Source

Built Distribution

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

energymeter2mqtt-0.6.3-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file energymeter2mqtt-0.6.3.tar.gz.

File metadata

  • Download URL: energymeter2mqtt-0.6.3.tar.gz
  • Upload date:
  • Size: 83.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for energymeter2mqtt-0.6.3.tar.gz
Algorithm Hash digest
SHA256 940b68493d36ac7a3e03787dddd3420dbc7fa0370e75d1cef157cc8a7ee19f7e
MD5 df8245dd48b7274efb1ca59a2c9ee75d
BLAKE2b-256 718adc45a8e999519e86dabd307f16df53e7011c1d11c53e6e0a5f5b0580d5a2

See more details on using hashes here.

File details

Details for the file energymeter2mqtt-0.6.3-py3-none-any.whl.

File metadata

File hashes

Hashes for energymeter2mqtt-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6e4acf3383c4e8dbb05b3b3a7513379df3db01d112e4b249093b704b6b8c4fc4
MD5 5ce693d8edd2e67ac546fea24ae90e9e
BLAKE2b-256 679fb51c66ac4130630b63cd978815c601e861965cb0ce2d5931bdc771144739

See more details on using hashes here.

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