Custom python script for publishing sensor values

Hi everyone,

I have created a Python script that calculates the optimal color temperature for my lights based on the sun’s position and weather information. I want to run this script every 5 minutes and publish the color temperature values as a sensor in Home Assistant.

Initial Approach

I tried using the command_line integration to run the Python script at an interval of 300 seconds (5 minutes) to update a sensor with the calculated values. This approach is neat and straightforward—if it works.

The Issue

My script relies on custom Python packages. I logged into my HA VM (using the SSH Terminal add-on) and created a virtual environment (venv) where I installed the required packages. However, when I try to run the script using the command_line integration with the venv interpreter, it fails to find the interpreter.

Why?
The command executed by the command_line integration runs inside the Home Assistant container (running on the HAOS VM), and the paths to the interpreter are not the same as on my host. I could log into the container and create the venv there, but I would prefer not to do that.

Request for Suggestions

Does anyone have suggestions on how I can solve this issue? Alternatively, is there another way to achieve my goal—perhaps by running the script as an add-on or using another method?

Thanks in advance for any help or advice!

Hi there trym.j.berg,
There may be a solution but there is a slight problem.

Sadly we’re not mind readers (any more anyway, not after the last time we tried). Please share the YAML and any errors so we can see what you’ve done.

Try pyscript custom integration, you can install custom packages and schedule function executions

1 Like

High-level suggestion: Run the script in an easier environment where you have more control and publish the values using MQTT. I can’t help you with how to do that, but it should be very doable, and HA’s MQTT integration is easy to work with.

That you @fuatakgun! I am already running some other scripts in pyscript, so I will test that!

@Sir_Goodenough I will post my errors and YAML, thanks!