mjmccans / airthings-mqtt-ha

Python script for Airthings devices that uses mqtt to integrate with Home Assistant via mqtt discovery.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Running as a service doesn't use the options file

eresgit opened this issue · comments

I am unable to use the options file when running as a service, using a rpi3 on raspbian, it works fine running it from the prompt.

Sep 30 00:41:57 piTre systemd[1]: Started airthings-mqtt-ha control script.
Sep 30 00:41:57 piTre python3[19593]: [2022-09-30 00:41:57] WARNING: Error reading ./options.json file. This script will search for devices and output a suggested configuration file.
Sep 30 00:41:57 piTre python3[19593]: [2022-09-30 00:41:57] INFO: Setting up Airthings sensors...
Sep 30 00:41:57 piTre python3[19593]: [2022-09-30 00:41:57] INFO: No devices provided, so searching for Airthings sensors...
Sep 30 00:41:57 piTre python3[19593]: [2022-09-30 00:41:57] INFO: Starting search for Airthings sensors...

I tried adding options.json to Environment="PATH=/usr/local/bin but it does not seem to help. Changing PATH to the directory where I run the python script from using prompt does not hjelp either.

I am wondering if you are bumping into a permissions issue. One thing to try would be using the --config option to expressly set out the full path to your options file.

On a side note, native support for Airthings devices using the new bluetooth integration is being added to Home Assistant Core so you may not need this script in the future. I believe it was merged into the dev branch yesterday but missed the beta window for version 2022.10 so it is now slated for version 2022.11. The native support will provide a much nicer user experience, including auto-discovery of devices.

Using the --config options and setting the output path made it run like expected, I am not sure the reason why Environment path does not work, some assumptions may be wrong on my end.

I prefer using only MQTT discovery so your project works perfect for me, what I would like however would be a way to upload the measurements to the airthings cloud without using my phone manually, but that's beside the point in this issue :)

Thanks

Sorry for the too late comment but I think I can clarify the confusion.

The script loads ./options.json if no config file is specified. This file is taken from the current directory of the python process running the script. The current directory is not where the script lies. If you expect this to work you have to change the service file to something like this:

[Unit]
Description=Airthings to Home Assistant via MQTT
After=network-online.target

[Service]
User=hass
Group=hass
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
ExecStart=python ./airthings-mqtt-ha.py
WorkingDirectory=/usr/local/src/airthings-mqtt/src
Restart=always
RestartSec=2s