Add equation to light temperature

Hi, I am trying to set up a light with temperature changing according to a value from a template.

I was trying the following approach:

trigger:
  - platform: device
    domain: mqtt
    device_id: 07554d93badae49787e17516cc820532
    type: action
    subtype: "on"
    discovery_id: 0x943469fffe75dad1 action_on
condition: []
action:
  - service: light.turn_on
    data:
      kelvin: {{ (-2300/(as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(state_attr("sun.sun", "next_setting")))) * as_timestamp(utcnow()) + 4500 - (-2300/(as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(state_attr("sun.sun", "next_setting")))) * as_timestamp(state_attr("sun.sun", "next_setting")) }}
mode: single

But seems to not work at all. The question here is how do I add the value of that equation to the light temperature kelvin?

Your template needs to be wrapped in quotes or preceded by a block scalar indicator symbol like >.

But, I think there’s something wrong with the math too. The value I’m getting is over 16,000; which isn’t a valid Kelvin value for most light bulbs (2,000-6,500 is a typical range).

Your may also be running into issues because the values of those attributes change to the following day once they pass. Describe what it is you are trying to do and someone can help you redesign the template.

The math will only work on specific hours with an if. This is just an example.

Tried the following:

trigger:
  - platform: device
    domain: mqtt
    device_id: 07554d93badae49787e17516cc820532
    type: action
    subtype: "on"
    discovery_id: 0x943469fffe75dad1 action_on
condition: []
action:
  - service: light.turn_on
    data:
      kelvin: "{{ (-2300/(as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(state_attr("sun.sun", "next_setting")))) * as_timestamp(utcnow()) + 4500 - (-2300/(as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(state_attr("sun.sun", "next_setting")))) * as_timestamp(state_attr("sun.sun", "next_setting")) }}"
mode: single

Still getting a red bar on the left and it doesn’t save the YAML config.

on the kelvin: line, your inner and outer double quotations marks are canceling each other out. you can either double the double quotation marks on the inner ones

state_attr(""sun.sun"", ""next_dusk"")

or change the inner or outer pairs to a single quotation mark:

kelvin: '{{ (-2300/(as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(state_attr("sun.sun", "next_setting")))) * as_timestamp(utcnow()) + 4500 - (-2300/(as_timestamp(state_attr("sun.sun", "next_dusk")) - as_timestamp(state_attr("sun.sun", "next_setting")))) * as_timestamp(state_attr("sun.sun", "next_setting")) }}'

This allows me to save the config, but as a side note, your formula is outputting 96,000 or so kelvin for me