weltenwort / py-power-meter-monitor

A bridge between the RS-485 interface of certain Logarex power meters and MQTT with Home Assistant support.
MIT License
2 stars 0 forks source link

Running PY meter as a service #213

Closed asprickmann closed 1 year ago

asprickmann commented 1 year ago

Hi Felix, after the py meter is running perfectly well on my Pi, I would like to run it as a service. Currently I start the process with poetry run py-power-meter-monitor --config-file /home/pi/PVRS485/py-power-meter-monitor/etc/logarex-config.toml

Can you help to convert this into a service I can automatically start at the reboot of the machine and which restarts automatically in the case of an error? I tried to install it as a service I can start with systemctl but didn't succeed.

weltenwort commented 1 year ago

Hi @asprickmann,

for running it as a podman container, there is an example service file at https://github.com/weltenwort/py-power-meter-monitor/blob/main/etc/py-power-meter-monitor.service. If you want to run the python process directly without a container, a comparable service file could look like this:

[Unit]
Description=Power Meter Monitor
Wants=network-online.target
After=network-online.target
StartLimitIntervalSec=30min
StartLimitBurst=59

[Service]
Restart=on-failure
RestartSec=30
TimeoutStopSec=90
ExecStart=/path/to/poetry run py-power-meter-monitor --config-file /home/pi/PVRS485/py-power-meter-monitor/etc/logarex-config.toml
WorkingDirectory=/home/pi/PVRS485/py-power-meter-monitor
Type=simple

[Install]
WantedBy=multi-user.target default.target

I don't have a way to test this right now, but it should be similar to this. You'd have to adjust the /path/to/poetry in the ExecStart line and maybe some other things depending on your environment. While logged in as user pi:

Hope it helps you out and I didn't introduce too many mistakes :crossed_fingers: Depending on the kernel and systemd version of your OS this service could be further improved by dropping capabilities and running it in a restricted namespace (see https://systemd.io/PORTABLE_SERVICES/ and https://0pointer.net/blog/walkthrough-for-portable-services.html).

Let me know if it works out or if you running into problems.

asprickmann commented 1 year ago

Hi Felix, many thanks again. It is amazing, how fast and detailed you are answering requests.

I tried to start it as you described but it didn't work. Looks like there is a missing module in th environment. Could you please have a look to the result:

PV_PY_SERVICE.service: Description=PV Power Meter Monitor Wants=network-online.target After=network-online.target StartLimitIntervalSec=30min StartLimitBurst=59

[Service] Restart=on-failure RestartSec=30 TimeoutStopSec=90 ExecStart=/home/pi/.local/bin/poetry run py-power-meter-monitor --config-file /home/pi/PVRS485/py-power-meter-monitor/etc/logarex-config.toml WorkingDirectory=/home/pi/PVRS485/py-power-meter-monitor Type=simple

[Install] WantedBy=multi-user.target default.target

systemctl status PV_PY_SERVICE: ● PV_PY_SERVICE.service - PV Power Meter Monitor Loaded: loaded (/lib/systemd/system/PV_PY_SERVICE.service; disabled; vendor preset: enabled) Active: activating (auto-restart) (Result: exit-code) since Fri 2023-01-13 09:18:19 CET; 6s ago Process: 18023 ExecStart=/home/pi/.local/bin/poetry run py-power-meter-monitor --config-file /home/pi/PVRS485/py-power-meter-monitor/etc/logarex-config.toml (code=exited, status=1/FAILURE) Main PID: 18023 (code=exited, status=1/FAILURE) CPU: 142ms

journalctl -u PV_PY_SERVICE: Jan 13 09:18:19 raspberrypi systemd[1]: PV_PY_SERVICE.service: Main process exited, code=exited, status=1/FAILURE Jan 13 09:18:19 raspberrypi systemd[1]: PV_PY_SERVICE.service: Failed with result 'exit-code'. Jan 13 09:18:49 raspberrypi systemd[1]: PV_PY_SERVICE.service: Scheduled restart job, restart counter is at 59. Jan 13 09:18:49 raspberrypi systemd[1]: Stopped PV Power Meter Monitor. Jan 13 09:18:49 raspberrypi systemd[1]: Started PV Power Meter Monitor. Jan 13 09:18:49 raspberrypi poetry[18031]: Traceback (most recent call last): Jan 13 09:18:49 raspberrypi poetry[18031]: File "/home/pi/.local/bin/poetry", line 5, in Jan 13 09:18:49 raspberrypi poetry[18031]: from poetry.console.application import main Jan 13 09:18:49 raspberrypi poetry[18031]: ModuleNotFoundError: No module named 'poetry' Jan 13 09:18:49 raspberrypi systemd[1]: PV_PY_SERVICE.service: Main process exited, code=exited, status=1/FAILURE Jan 13 09:18:49 raspberrypi systemd[1]: PV_PY_SERVICE.service: Failed with result 'exit-code'.

Best regards, Andreas

Am Do., 12. Jan. 2023 um 00:51 Uhr schrieb Felix Stürmer < @.***>:

Hi @asprickmann https://github.com/asprickmann,

for running it as a podman container, there is an example service file at https://github.com/weltenwort/py-power-meter-monitor/blob/main/etc/py-power-meter-monitor.service. If you want to run the python process directly without a container, it could look like this:

[Unit] Description=Power Meter Monitor Wants=network-online.target After=network-online.target StartLimitIntervalSec=30min StartLimitBurst=59

[Service] Restart=on-failure RestartSec=30 TimeoutStopSec=90 ExecStart=/path/to/poetry run py-power-meter-monitor --config-file /home/pi/PVRS485/py-power-meter-monitor/etc/logarex-config.toml WorkingDirectory=/home/pi/PVRS485/py-power-meter-monitor Type=simple

[Install] WantedBy=multi-user.target default.target

I don't have a way to test this right now, but it should be similar to this. You'd have to adjust the /path/to/poetry in the ExecStart line and maybe some other things depending on your environment. While logged in as user pi:

  • put all this into a file called /home/pi/.config/systemd/user/py-power-meter-monitor.service
  • run systemctl --user daemon-reload to let systemd load the new file
  • run systemctl --user enable --now py-power-meter-monitor.service to enable and start the service
  • run sudo loginctl enable-linger pi to make sure the services of the pi user are automatically started it logging in

Hope it helps you out and I didn't introduce too many mistakes 🤞 Depending on the kernel and systemd version of your OS this service could be further improved by dropping capabilities and running it in a restricted namespace (see https://systemd.io/PORTABLE_SERVICES/ and https://0pointer.net/blog/walkthrough-for-portable-services.html).

Let me know if it works out or if you running into problems.

— Reply to this email directly, view it on GitHub https://github.com/weltenwort/py-power-meter-monitor/issues/213#issuecomment-1379629006, or unsubscribe https://github.com/notifications/unsubscribe-auth/A46WMTJS54ZJGKY33RCSE2TWR5BPBANCNFSM6AAAAAATWONZLI . You are receiving this because you were mentioned.Message ID: @.***>

weltenwort commented 1 year ago

Hm, that's hard to diagnose without more context. Can you tell me which distro and version you're running on that RPi and how you installed poetry?

weltenwort commented 1 year ago

Now that I take another look at your answer, I notice that the systemctl and journalctl calls don't refer to the user session. But the service file I outlined is designed to be run in the pi user's session. Maybe there's an important reason you want it as a global service, but I'd recommend to stick with the lower-privileged user session when possible.

asprickmann commented 1 year ago

Hi Felix, thanks for your feedback. There was no reason for running it as a global service. I started it now with pi user and it is working with the service definition below.

With all your help I am now able to run the system reliably with all components. Many thanks again. Is there any way I can show my appreciation to you?

Best regards Andreas

[Unit] Description=PV Power Meter Monitor Wants=network-online.target After=network-online.target StartLimitIntervalSec=30min StartLimitBurst=59

[Service] Restart=on-failure RestartSec=30 TimeoutStopSec=90 ExecStart=/home/pi/.local/bin/poetry run py-power-meter-monitor --config-file /home/pi/PVRS485/py-power-meter-monitor/etc/logarex-config.toml WorkingDirectory=/home/pi/PVRS485/py-power-meter-monitor Type=simple

User=piGroup=pi [Install] WantedBy=multi-user.target default.target

Am Fr., 13. Jan. 2023 um 22:54 Uhr schrieb Felix Stürmer < @.***>:

Now that I take another look at your answer, I notice that the systemctl and journalctl calls don't refer to the user session. But the service file I outlined is designed to be run in the pi user's session. Maybe there's an important reason you want it as a global service, but I'd recommend to stick with the lower-privileged user session when possible.

— Reply to this email directly, view it on GitHub https://github.com/weltenwort/py-power-meter-monitor/issues/213#issuecomment-1382434872, or unsubscribe https://github.com/notifications/unsubscribe-auth/A46WMTITEH4GDE5IZAWVAXLWSHFJTANCNFSM6AAAAAATWONZLI . You are receiving this because you were mentioned.Message ID: @.***>

weltenwort commented 1 year ago

I'm just glad to hear it works for you :tada: If it enables you to use energy more consciously and efficiently that's rewarding enough.

You can contribute something to this project, though, by telling me your exact power meter model. That way I can add it to the README as a successfully tested model.

asprickmann commented 1 year ago

Happy to contribute to the project. My power meter is a Logarex LK13BE803039.

I also want to let you know, why it was and is so important for me to use your software: I was measuring my meters a while already but the IR diodes stopped working in between the project of adding a Sonnenbatteri (battery for my house) to even more use my own produced electricity. So I needed to use the second option via rs485. Due to the visibility on the meter measurements I was and am able to see that the battery installation was not done properly and I am able to work with the supplier to get it fixed. Without the visibility of the meter data I would just have recognized once much later that the battery is not paying off very well. From my perspective the supplier is not doing any quality assurance of the final installation and I assume there are a lot of batteries out there not working properly due to bad installations.

Many thanks again for all your help.

Best regards, Andreas

Am Mo., 16. Jan. 2023 um 01:35 Uhr schrieb Felix Stürmer < @.***>:

I'm just glad to hear it works for you 🎉 If it enables you to use energy more consciously and efficiently that's rewarding enough.

You can contribute something to this project, though, by telling me your exact power meter model. That way I can add it as a successfully tested model to the README.

— Reply to this email directly, view it on GitHub https://github.com/weltenwort/py-power-meter-monitor/issues/213#issuecomment-1383304032, or unsubscribe https://github.com/notifications/unsubscribe-auth/A46WMTL55GWLKSZZ7ZKNJLLWSSJVPANCNFSM6AAAAAATWONZLI . You are receiving this because you were mentioned.Message ID: @.***>

weltenwort commented 1 year ago

Thanks for sharing this and thanks for supporting me!

I assume there are a lot of batteries out there not working properly due to bad installations

Agreed - it's depressing to imagine how many of such small inefficiencies and mistakes are out there and to how much wasted energy and money they likely add up.

I'll mark this as resolved, but feel free to open a new issue when you have a feature idea or run into problems.

asprickmann commented 1 year ago

Thanks again for your support and openness for new ideas. I will contact you if it is the case. As a next project I will now focus in EVCC. We just received an EV and it is interesting to steer the charging depending on the PV yield. It also has the potential to deal with multiple energy storages like EV, battery and heat pump, which is not yet there but I am sure, it will come.. In addition it shall already be able to use dynamic tariffs like from tibber and awattar and a cost optimized target loading.

All the best for now, Andreas

Am Mo., 16. Jan. 2023 um 21:13 Uhr schrieb Felix Stürmer < @.***>:

Thanks for sharing this and thanks for supporting me!

I assume there are a lot of batteries out there not working properly due to bad installations

Agreed - it's depressing to imagine how many of such small inefficiencies and mistakes are out there and to how much wasted energy and money they likely add up.

I'll mark this as resolved, but feel free to open a new issue when you have a feature idea or run into problems.

— Reply to this email directly, view it on GitHub https://github.com/weltenwort/py-power-meter-monitor/issues/213#issuecomment-1384510687, or unsubscribe https://github.com/notifications/unsubscribe-auth/A46WMTORRVCSMPX6CXE5TY3WSWTXTANCNFSM6AAAAAATWONZLI . You are receiving this because you were mentioned.Message ID: @.***>