Generic template function to tween values between times of day

I am trying to automate the lights in my home based on the time of day in order to help my circadian rhythm. I assumed doing things like smoothly starting my bedroom light in the morning to simulate sunrise would be simple in HA, but currently the only way to achieve it is using long chains of if/else template statements. This is cumbersome, verbose, error prone and difficult to test.

I have added a template function to my local HA instance to make this simpler and more declarative. It works like this:

  - service: light.turn_on
    entity_id: light.bedroom_lights
    data_template:
      brightness_pct: '{{
        time_tween_value({
            "00:00": 1,
            "06:00": 1,
            "08:00": 100,
            "16:00": 100,
            "20:00": 50,
            "22:00": 20,
        })
      }}'
      color_temp: '{{
        time_tween_value({
            "00:00": 400,
            "06:00": 400,
            "08:00": 150,
            "16:00": 150,
            "20:00": 300,
        })
      }}'
      transition: 1

This uses the current time to pick a value from the mapping. If the current time is not in the mapping, it returns a tweened value between the previous and next times. In the above example, the brightness percentage will be set to 1 at 6 AM, 9 at 6:10 AM, 18 at 6:20 AM etc. up to 100 at 8 AM. The color temperature will also start warm in the morning and tween to cold as the brightness increases.

This can be used to tween any integer value, even negative ones, so it can be used to tween any property that takes an integer value based on the time of day.

I am using the above configuration currently on my local HA instance, but it would be a pain to maintain my local code changes through updates.

Is this function something others would be interested in having available? I would be happy to create a pull request, but I don’t want to go through the trouble if it would only get declined anyway.

If you have any constructive comments or questions about the syntax or function name I would be happy to hear them.

Neat idea. Were you aware of this?

Also if you have colour smart lights this is a great sunrise alarm clock (from dim red through orange to bright white):

1 Like