ThomDietrich / miflora-mqtt-daemon

Linux service to collect and transfer Xiaomi Mi Flora plant sensor data via MQTT to your smart home system, with cluster support 🌱🌼πŸ₯€πŸ‘🌳
MIT License
610 stars 140 forks source link

Not able to get systemd service working #52

Open cmburns opened 6 years ago

cmburns commented 6 years ago

Hi,

first I want to thank you for this great tool. Basically I could make my miflora sensor working. Now I want to make the systemd service working but got problems. I installed the service as written in the github documentation:

sudo cp /opt/miflora-mqtt-daemon/template.service /etc/systemd/system/miflora.service sudo systemctl daemon-reload sudo systemctl start miflora.service

But then the terminal shows:

Job for miflora.service failed because the control process exited with error code. See "systemctl status miflora.service" and "journalctl -xe" for details.

If I run systemctl status miflora.service it tells me:

miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Loaded: loaded (/etc/systemd/system/miflora.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2018-08-14 11:15:15 CEST; 19min ago Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon Process: 27488 ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (code=exited, status=1/FAILURE) Main PID: 27488 (code=exited, status=1/FAILURE)

Aug 14 11:15:15 openHABianPi systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Aug 14 11:15:15 openHAB systemd[1]: miflora.service: Unit entered failed state. Aug 14 11:15:15 openHAB systemd[1]: miflora.service: Failed with result 'exit-code'. Aug 14 11:15:15 openHAB systemd[1]: miflora.service: Service hold-off time over, scheduling restart. Aug 14 11:15:15 openHAB systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Aug 14 11:15:15 openHAB systemd[1]: miflora.service: Start request repeated too quickly. Aug 14 11:15:15 openHAB systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Aug 14 11:15:15 openHAB systemd[1]: miflora.service: Unit entered failed state. Aug 14 11:15:15 openHAB systemd[1]: miflora.service: Failed with result 'exit-code'.`

What I am missing?

Thanks a lot for any help!

mredone commented 6 years ago

Is it work when you run standard without service? Show your service file.

cmburns commented 6 years ago

Hi mredone,

thanks for your help. Yes it is working if I run it in terminal window using:

python3 /opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py

It reads out my miflora sensor and shows the JSON values. Then it goes to sleep for 300 sek.. Here comes my miflora.service file:

[Unit] Description=Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Documentation=https://github.com/ThomDietrich/miflora-mqtt-daemon After=bluetooth.service mosquitto.service

[Service] Type=notify User=daemon Group=daemon WorkingDirectory=/opt/miflora-mqtt-daemon/ ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py StandardOutput=null StandardError=journal Environment=PYTHONUNBUFFERED=true Restart=always

[Install] WantedBy=multi-user.target

cmburns commented 6 years ago

Hi,

is there anybody out there who can help me with this?

mredone commented 6 years ago

Can you start service later? sudo systemctl start miflora.service

cmburns commented 6 years ago

Hi mredone,

I tried that but it says:

Job for miflora.service failed because the control process exited with error code. See "systemctl status miflora.service" and "journalctl -xe" for details.

"systemctl status miflora.service" shows:

miflora.service - Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Loaded: loaded (/etc/systemd/system/miflora.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2018-08-21 11:01:17 CEST; 6min ago Docs: https://github.com/ThomDietrich/miflora-mqtt-daemon Process: 13939 ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py (code=exited, status=1/FAILURE) Main PID: 13939 (code=exited, status=1/FAILURE)

Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Unit entered failed state. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Failed with result 'exit-code'. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Service hold-off time over, scheduling restart. Aug 21 11:01:17 openHAB systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Start request repeated too quickly. Aug 21 11:01:17 openHAB systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Unit entered failed state. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Failed with result 'exit-code'.

and "journalctl -xe" says:

-- Unit miflora.service has failed. -- -- The result is failed. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Unit entered failed state. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Failed with result 'exit-code'. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Service hold-off time over, scheduling restart. Aug 21 11:01:17 openHAB systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. -- Subject: Unit miflora.service has finished shutting down -- Defined-By: systemd -- Support: https://www.debian.org/support -- -- Unit miflora.service has finished shutting down. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Start request repeated too quickly. Aug 21 11:01:17 openHAB systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. -- Subject: Unit miflora.service has failed -- Defined-By: systemd -- Support: https://www.debian.org/support -- -- Unit miflora.service has failed. -- -- The result is failed. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Unit entered failed state. Aug 21 11:01:17 openHAB systemd[1]: miflora.service: Failed with result 'exit-code'.

cmburns commented 6 years ago

And now please?

ajxn commented 6 years ago

Den ons 22 aug. 2018 kl 19:31 skrev cmburns notifications@github.com:

And now please?

Yes? Have you turned on all debugging messages that you can for the daemon? Have you turned on more debugging for systemd? Do the services have all the permissions that it might need to function?

β€” You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ThomDietrich/miflora-mqtt-daemon/issues/52#issuecomment-415114088, or mute the thread https://github.com/notifications/unsubscribe-auth/AANApMkanjHU_HfPpO9-Arw8jAxpz0E-ks5uTZWIgaJpZM4V8G53 .

cmburns commented 6 years ago

Hi ajxn,

I am not a Linux professional, can you please tell me how to do that?

cmburns commented 6 years ago

Can someone help me further please?

cmburns commented 6 years ago

Need help please...

ThomDietrich commented 6 years ago

Hello @cmburns, are you still in need for help? Please post the output of systemctl -n100 status miflora.service

jwhite commented 5 years ago

Jun 22 22:50:10 gardengateway systemd[1]: miflora.service: Unit entered failed state. Jun 22 22:50:10 gardengateway systemd[1]: miflora.service: Failed with result 'exit-code'. Jun 22 22:50:10 gardengateway systemd[1]: miflora.service: Service hold-off time over, scheduling restart. Jun 22 22:50:10 gardengateway systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Jun 22 22:50:10 gardengateway systemd[1]: miflora.service: Start request repeated too quickly. Jun 22 22:50:10 gardengateway systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon. Jun 22 22:50:10 gardengateway systemd[1]: miflora.service: Unit entered failed state. Jun 22 22:50:10 gardengateway systemd[1]: miflora.service: Failed with result 'exit-code'.

jwhite commented 5 years ago

Same issue here....

Status: "Jun 22 22:50:10 - MQTT connection error. Please check your settings in the configuration file "config.ini".

Perhaps the service comes up before the networking?

jwhite commented 5 years ago

I added these lines to the service control file...

[Unit] Description=Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon Documentation=https://github.com/ThomDietrich/miflora-mqtt-daemon After=bluetooth.service mosquitto.service network-online.target Wants=network-online.target

It seems to work now for me.

jwhite commented 5 years ago

Also should probably add a

StartLimitIntervalSec=0

under [Service] in case you have a power outage and the gateway comes up before the mqtt service.

https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6

ThomDietrich commented 5 years ago

Interesting. Thanks for sharing your solution. Please create a PR for those changes :)

stefanheijnen commented 4 years ago

I've the same issue as stated above, only difference is, is that I'm running this daemon on multiple devices in my home network and not on the openHAB Rpi.

The service would still start before the network was up:

22 09:25:48 spin miflora-mqtt-daemon.py[4925]: [2020-02-22 09:25:48] MQTT connection error. Please check your settings in the configuration file "config.ini"
feb 22 09:25:48 spin systemd[1]: miflora.service: Main process exited, code=exited, status=1/FAILURE
feb 22 09:25:48 spin systemd[1]: miflora.service: Failed with result 'exit-code'.
feb 22 09:25:48 spin systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
feb 22 09:25:48 spin audit[1]: SERVICE_START pid=1 uid=0 auid=4294967295 ses=4294967295 msg='unit=miflora comm="systemd" exe="/usr/lib/systemd/systemd" hostname=? addr=? terminal=? res=failed'
feb 22 09:25:48 spin systemd[1]: miflora.service: Scheduled restart job, restart counter is at 5.
feb 22 09:25:48 spin systemd[1]: Stopped Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
feb 22 09:25:48 spin systemd[1]: miflora.service: Start request repeated too quickly.
feb 22 09:25:48 spin systemd[1]: miflora.service: Failed with result 'exit-code'.
feb 22 09:25:48 spin systemd[1]: Failed to start Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon.
feb 22 09:25:49 spin kernel: igb 0000:08:00.0 enp8s0: igb: enp8s0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: RX
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.1714] device (enp8s0): carrier: link connected
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.1716] device (enp8s0): state change: unavailable -> disconnected (reason 'carrier-changed', sys-iface-state: 'managed')
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.1725] policy: auto-activating connection 'Wired connection 1' (4a226758-ed4e-3bd9-9124-3e69b5dfbae6)
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.1729] device (enp8s0): Activation: starting connection 'Wired connection 1' (4a226758-ed4e-3bd9-9124-3e69b5dfbae6)
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.1729] device (enp8s0): state change: disconnected -> prepare (reason 'none', sys-iface-state: 'managed')
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.1731] manager: NetworkManager state is now CONNECTING
feb 22 09:25:49 spin kernel: IPv6: ADDRCONF(NETDEV_CHANGE): enp8s0: link becomes ready
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.2210] device (enp8s0): state change: prepare -> config (reason 'none', sys-iface-state: 'managed')
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.2224] device (enp8s0): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed')
feb 22 09:25:49 spin NetworkManager[667]: <info>  [1582359949.2229] dhcp4 (enp8s0): activation: beginning transaction (timeout in 45 seconds)
feb 22 09:25:49 spin avahi-daemon[653]: Joining mDNS multicast group on interface enp8s0.IPv6 with address fe80::c360:91f3:a916:a9cf.
feb 22 09:25:49 spin avahi-daemon[653]: New relevant interface enp8s0.IPv6 for mDNS.
feb 22 09:25:49 spin avahi-daemon[653]: Registering new address record for fe80::c360:91f3:a916:a9cf on enp8s0.*.

Even though I configured it like this:

/etc/systemd/system/miflora.service 

[Unit]
Description=Xiaomi Mi Flora Plant Sensor MQTT Client/Daemon
Documentation=https://github.com/ThomDietrich/miflora-mqtt-daemon
After=network-online.target bluetooth.service bluetooth.target multi-user.target
Wants=network-online.target bluetooth.target

[Service]
Type=notify
User=daemon
Group=daemon
WorkingDirectory=/opt/miflora-mqtt-daemon/
ExecStart=/opt/miflora-mqtt-daemon/miflora-mqtt-daemon.py
StandardOutput=null
StandardError=journal
Environment=PYTHONUNBUFFERED=true
Restart=always
#StartLimitIntervalSec=0

[Install]
WantedBy=multi-user.target

To workaround this problem I created a systemd timer:

/etc/systemd/system/miflora.timer

[Unit]
Description=Miflora timer

[Timer]
# Time to wait after booting before it run for first time
OnBootSec=1m
Unit=miflora.service

[Install]
WantedBy=default.target

Don't forget to enable it with:

sudo systemctl enable miflora.timer

stefanheijnen commented 4 years ago

Or just RTFM: https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/

The right "wait" service must be enabled too (NetworkManager-wait-online.service if NetworkManager is used to configure the network, systemd-networkd-wait-online.service if systemd-networkd is used, etc.). systemd-networkd.service has Also=systemd-networkd-wait-online.service in its [Install] section, so when systemd-networkd.service is enabled, systemd-networkd-wait-online.service will be enabled too, which means that network-online.target will include systemd-networkd-wait-online.service when and only when systemd-networkd.service is enabled. NetworkManager-wait-online.service is set up similarly. Verify that the right service is enabled (usually only one should be):

$ systemctl is-enabled NetworkManager-wait-online.service systemd-networkd-wait-online.service
disabled
enabled
ThomDietrich commented 4 years ago

Thanks for the info. Would you like to summarize your findings in a Pull Request?