Python calculations as variables for dynamic light

So I want to create my own blueprint for changing my light colors and temperature throughout the day.
I’m new to using Home Assistant, but I used a similar approach when I was using Google Home Automation.
Let me start by saying my lights (WiZ) do not support transition, so that doesn’t work.

Now I want to make a blueprint where the following aspects are chosen:

  • Start/stop-time OR duration time of the change in minutes.
  • Start temperature for the first time range
  • Start brightness for the first time range
  • End temperature for the first time range
  • End brightness for the first time range

Then the following variables are calculated:

duration: #If start/stop-time is selected, then "{{ stop - start }}", if duration is chosen, then duration in minutes
intervals: "{{ duration * 2 }}"
step_kelvin: "{{ ((stop_kelvin - start_kelvin) / intervals) }}"
step_brightness: "{{ ((stop_bright - start_bright) / intervals) }}"

I was wondering if I could use a Python script to do the calculation. I used the following Python script to find those variables:

from math import gcd

# Input values
initial_temp = 3500          # Initial temperature in Kelvin
target_temp = 5500           # Target temperature in Kelvin
initial_brightness = 50   # Initial brightness (percentage or units)
target_brightness = 90     # Target brightness (percentage or units)
duration = 600  # Total time in seconds

# Function to find the smallest interval for whole number decrease
def smallest_interval(initial_value, target_value, total_seconds):
    # Total decrease needed
    total_decrease = initial_value - target_value
    # Find the greatest common divisor (GCD) between total decrease and total seconds
    interval_gcd = gcd(total_decrease, total_seconds)
    # Calculate the interval in seconds
    interval_seconds = total_seconds // interval_gcd
    # Calculate the whole number decrease per interval
    decrease_per_interval = total_decrease // interval_gcd
    return interval_seconds, decrease_per_interval

# Calculate the smallest interval for color temperature
interval_seconds_temp, decrease_temp = smallest_interval(initial_temp, target_temp, duration)

# Calculate the smallest interval for brightness
interval_seconds_brightness, decrease_brightness = smallest_interval(initial_brightness, target_brightness, duration)

# Print results
print(f"Interval seconds: {interval_seconds_temp}")
print(f"Decrease brightness: {decrease_brightness}")
print(f"Decrease color: {decrease_temp}")

Hope someone can help me out!

There are lots of integrations that let you write things in Python in HASS. If it were me though and I really needed a GCD algorithm I’d just write one directly in Jinja…

Thanks. How would you use Jinja? Would you use it as a template in Developer Tools or as a helper?
If you’d use it as a helper, how would you use it in an automation? Sorry if this is basic skills, I’m very new to HA

Jinja is the built-in templating language that is already used in your first code snippet, those parts in the strings between the {{ and }}.

https://jinja.palletsprojects.com/en/stable/

I have no idea what programming experience you may or may not have. A GCD algorithm should be trivial to implement with even minimal experience, as long as you understand the peculiarities of Jinja. Pretty sure I did it on my Casio graphing calculator nearly 30 years ago, that sure was slow!

The way it’s used in Ashley’s Light Fader script.

I had suggested you use her script, or borrow ideas from it, in the first of the three topics you have now opened for the same subject: a light fader script.

The beauty of it is that it’s not just some generic example of Jinja2 templating but an implementation of the application you want to build.

Be advised that the python_script integration doesn’t support importing modules (the example you posted imports from the math library). It’s a sandboxed environment and not all of python’s functionality is supported.