Hi.
I am trying to send a bunch of data, fetched from database and formatted in json to MQTT via python script. I call them from HA. Anyhow. The “old” was I was/am doing this was by using shelly commands like this:
run_script: 'python /config/my_config/python_scripts/meteo_call.py'
Works excellent, grabs data from website, parses it to json and posts to MQTT.
Since this was working excellent and I was wanting to transfer more than 255 chars to UI with querries from DB and without the need of history as state/sql sensors have I created another python script to do this. Connects to the DB, executes SQL querries, transforms them to JSON and posts to MQTT. All fine and dandy, when runnig via command line (testing), it was perfect. Fast, readable, good. As soon as I tried to execute it within HA with automation I was getting “unable to open DB” like messages. I thought that it was the path but no. That was OK as it turned out.
I was reading what might be wrong and came across command line approach. I created a command line switch sensor and executed command like this:
bash -c 'python /config/my_config/python_scripts/sql_call.py &'
Had to format it exactly that. And this works. But is SLOW to execute or finish. For instance, script itself is over in about 1s or less if run from command line. When it is run like above it takes up to 60s. As far as I was able to figure out it is due to some schedulers in HA itself. But not sure. And the execution time does not botter me as I run this every 6hours. So, no real problem.
I would just like to know what is going on with shell command not being able to open/establish database connection. I am running things through Docker Desktop, so I am a bit limited but still, what is going on. Does the HA process that runs this shell command not have the priviledges/access rights? Why would it not be working?
Thanks for helping me understand