Architectural question regarding execution of python script

hey guys,

I want to execute a python script that checks the forecast for the next days solarenergy and depending on that stops or starts the heating for warm water with the gas heating.

First guess was to put it into python_scripts and just execute it with an automation every 24hours. But that isnt working as there is only a limited python sandbox environment for these scripts with very less functionalities (so no imports, …).

Next I added the script as shell_command and executing within an automation. Its working but feels not perfect.

Are there any other (better) solutions out there?

Best,
Dirk

Sounds interesting. Would you mind sharing your script please?

Check it out here: Weishaupt integration

For Python based automations, and any complex automation at all, I prefer AppDaemon. It’s similar to python_script, but there is no sandbox, your code can be long lived, and you have a lot more tools at your disposal.

If your questions is “how do I run a python script every day?” then the answer is “cron”.

You can call any python script (e.g. my_script.py) using a shell command. Then all the packages in the home assistant virtual environment are available for import

1 Like

@swiftlyfalling using AppDaemon sounds so complex and over engineered. Do you have a simple example on how dealing with AppDaemon on Hassbian+Home Assistant+HACS?

@nickrout I dont want to use cron; it should run within Home Assistant. I want to have features like enabling/disabling it; getting the log output within Home Assistant and so son.

@robmarkcole thats what I already did and have. My question was more like: is there any better solution or approach.

OK so don’t put your script in that directory. Put it in the venv and call it. Make sure all your imports are installed in the venv.

I put it in .homeassistant/scripts/my_python_script.py and calling it with shell_command. Everything works already.

“better” is not helpful input to requirements capture :slight_smile:

Better would be: having the possibility to receive the result of the executed script. Currently I set debug level of shell_command to debug and need to check the log to see the result.

I assume there are two more ways: a) using AppDaemon, b) writing an own HA integration.

Another idea I had was using a command line sensor, but there is no possibility to set the execution based on cron. Only a time in seconds.

K.I.S.S
Simple is good.

Since I have appdaemon I would use it.
Else I would use cron if I don’t need HA modules
Else if I need them, cmdline.

what about the idea to write a home assistant integration for the heating system. everyone could use it, make it configurable (username, password), put his own geocoordinates or even reuse the one from HA, and so on?

1 Like

Go for it.

As long as you’re not running Hass.io, Running AppDaemon is easy. The setup steps (prior to configuration) are very similar to the steps for Home Assistant.

  1. Create a venv somewhere. (python3 -m venv ~/appdaemon)
  2. Activate venv (source ~/appdaemon/bin/activate)
  3. Install appdaemon (pip3 install appdaemon)
  4. Create a config directory (mkdir ~/appdaemon-conf)
  5. Build your config file per the docs (nano ~/appdaemon-conf/appdaemon.yaml)
  6. Run appdaemon (python3 -m appdaemon -c ~/appdaemon-conf)

Creating an “app” in AppDaemon is pretty simple too, though you’re best off reading the docs for that since it’ll largely depend on what you want to do.

It’s not complicated. Depending on what you’re doing with it, it may be “over engineered”. But if you want a Python Script that will listen to states or events and call services you’ll need a good chunk of what AppDaemon provides anyway. There are some bits you might never use, but they won’t likely get in your way.

1 Like

appdaemon is not needed. @dm82m has this working. I can’t even work out what he wants.

my next step would be to try to write an integration for home assistant. but I am already failing with getting configuration.yaml options (username, password) transmitted into the integration.

wemportal:
  username: "myuser"
  password: "mypassword"

So it will definitely take a lot of time and I am not sure if it will come to an end … :wink:

Writing Home Assistant Integrations is not exactly easy. While doing so certainly makes it easier for others to share the fruits of your efforts, the quickest route to a working solution is probably not that.

Since you already have the code written in Python, turning that into an AppDaemon app is pretty simple. However, as @nickrout said, AppDaemon is not NEEDED – there are lots of ways to accomplish this. However, AppDaemon is certainly one way to get there.

The output of a Home Assistant shell_command shows up in the Home Assistant log with “debug” log level turned on. AppDaemon apps have their own log outside of Home Assistant.

If you’re certain you’re only ever going to do a task like this once, just writing a shell command is probably easiest. It may not feel “perfect”, but the job is done. If you envision you might need more scripts like this in the future, the extra effort required to install AppDaemon is worth it, in my opinion. And then you have a reusable, repeatable, github/backup-able, solution for handling other problems like this in the future. In many cases (unless you need to use Home Assistant’s Config Flow, or you need to register a Home Assistant service) you can even write AppDaemon apps that function just like a Home Assistant integration would (creating Home Assistant entities and everything).

1 Like