Incremental change of light temperature

Hi all,

I have both a light and a remote (the one with bright and temperature buttons) from Ikea integrated into Hassio. Yesterday, I coded a automation in which when brightness buttons in the remote are pressed, the brightness from the light is either increased or decreased. I used something similar to this and it is perfectly working:

- service: light.turn_on
  entity_id: light.light_name
  data_template:
    brightness: '{{states.light.light_name.attributes.brightness + 10}}'

However, I was trying to apply the same sort of code to change the light temperature but it was impossible. I manage to set a specific color through ā€œcolor_tempā€ attribute but I can not find and attribute in this light that stores the actual ā€œcolor_tempā€ so I can code something like:

- service: light.turn_on
  entity_id: light.light_name
  data_template:
    color_temp: '{{states.light.light_name.attributes.color_temp + 10}}'

Any ideas on how to do this?

Thank you!

Iā€™m surprised either of them are working as you are concatenating a string to a number with ā€œ+ā€

e.g. I think '24' + 10 gives '2410'

Try converting the attribute to an integer before adding 10:

- service: light.turn_on
  entity_id: light.light_name
  data_template:
    brightness: "{{ state_attr('light.light_name', 'brightness')|int + 10 }}"

and:

- service: light.turn_on
  entity_id: light.light_name
  data_template:
    color_temp: "{{ state_attr('light.light_name', 'color_temp')|int + 10}}"

Note: you should ensure that brightness does not exceed 255. Likewise colour temp maximum will depend on your light model. Use conditions to stop the automation from happening if adding 10 will exceed the maximum values.

Also note the format I used to get the attributes. You should use this to prevent errors if the attribute is unknown. See the warning here: https://www.home-assistant.io/docs/configuration/templating/#states

I agree with Tom.

Tom, I was going to say that the template should be expanded to do the limit checking. But we are just assuming brightness is 255 (dependant on light model but ā€˜mostā€™ manufacturers adhere to common sense standard so a pretty sure bet) but the colour temperature ??? I have no idea (2300 to 4800 ??? Or some other equally arbitrary number set!)
Alex seems new(ish) to be doing this but actually got 90% of it right so my hat off to him. If he need to get his head round this and expand it later, then thatā€™s a good strategy.
My question to you is how do you know how far to go?
I tend to persue a solution till itā€™s a gnats ā€˜pleasure stickā€™ short of perfection, but that does seem to overwhelm some people

For colour temperature look up the Kelvin range of your light (manufacturerā€™s website) and convert to mireds.

1 Like

@tom_l Thanks for answering.

As far as I knew (Iā€™m quite new in HA) before posting the question, states.light.light_name.attributes.brightness gave an integer, but I will double check when I arrive home to be 100% sure.

For sure, what you mention about the conditions to ensure that limits are not exceeded should be introduced in my automation so I will code that, thanks for the advice. I know both brightness limits (0-255) and temperature limits (min_mireds: 153, max_mireds: 500) so that should not be a problem.

I will try this solution and see if I can make it work.

Thanks!

As far as I knew (Iā€™m quite new in HA) before posting the question, states.light.light_name.attributes.brightness gave an integer

All Home Assistant states are strings.

No it doesnā€™t.
It appears to give a number but its a string.
All states are stored as strings and you have to convert them to a format you can appropriately manipulate, integer in this case

Edit: Snap !

1 Like

Iā€™ve never heard of mireds (how ignorant am I ?) prolly cos I donā€™t have any lights with varying color temperatures. Looking at your link itā€™s a brilliant way of linearising colour temperature rather than the apparent inverse square of the degrees Kelvin.
Do all such bulbs expose both as I think (I havenā€™t paid enough attention to be certain) I have only seen Kelvin previously.
Thank for the link

Mireds is what home assistant uses. You canā€™t use Kelvin in scenes any more, which is annoying as it means nothing to me. Iā€™m used to Kelvin.

1 Like

Me too,
But itā€™s like centigrade, ah! 50Ā° thatā€™s half way between water freezing and boiling (at a given average air pressure at sea level) Iā€™ve got a good feel for that. Go back to Fahrenheit ? - No Thank You !
Given the linearity of the model, it makes absolute sense and Iā€™m sure Iā€™ll get used to it (though Iā€™m going to have to do a lot of conversions before I get there)

@tom_l

I have been trying to implement the temperature change but I have not been able.

Following your suggestion, I recoded the automation and I have the one to change the brightness working like this (I know that I should include the conditions for the limits):

- id: '1574446880053'
  alias: Bajar Brillo Luz
  description: Automatizacion para bajar el brillo
  trigger:
  - device_id: f0c96649677f48af84e82f0a949a1118
    domain: deconz
    platform: device
    subtype: dim_down
    type: remote_button_short_press
  condition: []
  action:
  - data_template:
      brightness: '{{ state_attr(''light.luz_lampara_salon'', ''brightness'')|int
        - 50 }}'
    entity_id: light.luz_lampara_salon
    service: light.turn_on

However, if I replicate this automation for color temperature like:

- id: '1574447424187'
  alias: Subir Temperatura Luz
  description: Incrementa el color de temperatura de la luz. Mas naranja
  trigger:
  - device_id: f0c96649677f48af84e82f0a949a1118
    domain: deconz
    platform: device
    subtype: right
    type: remote_button_short_press
  condition: []
  action:
  - service: light.turn_on
    entity_id: light.luz_lampara_salon  
    data_template:
      color_temp: '{{ state_attr(''light.luz_lampara_salon'', ''color_temp'')|int + 100}}'

This is not working. Do you know what am I doing wrong?

Does light.luz_lampara_salon have a color_temp attribute?

Look in the developer tools states menu.

Also watch your quotes.

This:

color_temp: '{{ state_attr(''light.luz_lampara_salon'', ''color_temp'')|int + 100}}'

Would be better written as:

color_temp: "{{ state_attr('light.luz_lampara_salon', 'color_temp')|int + 100}}"

Thats what puzzles me. The light has a temperature color which I can modify via the Lovelace interface, but it does not seem to have an attribute color_temp:

min_mireds and max_mireds means it has color_temp.

Try putting the entity id inside the data:

 action:
  - service: light.turn_on
    data_template:
      entity_id: light.luz_lampara_salon  
      color_temp: "{{ state_attr('light.luz_lampara_salon', 'color_temp')|int + 100}}"

Neither like this:

action:
  - service: light.turn_on
    data_template:
      entity_id: light.luz_lampara_salon
      color_temp: "{{ state_attr('light.luz_lampara_salon', 'color_temp')|int
        + 100 }}"

:man_shrugging:

I still think that color_temp attribute should be displayed here, in ā€œStatesā€, but for some reason it is not.

Edit:
Seems Iā€™m not the only one with this issue, and it has something to do with Deconz:

In case anybody needs this several years later, I got this to work with limts.

service: light.turn_on
data:
  color_temp: >
    {% if state_attr('light.livingroom_lights', 'color_temp')|int <= 500 %} 
    {{ state_attr('light.livingroom_lights', 'color_temp')|int + 40 }} 
    {% else %}
    500 
    {% endif %}
target:
  entity_id: light.livingroom_lights

2 Likes

Hereā€™s mine:
kelvin: "{{ min(4000, (state_attr('light.1','color_temperature_kelvin') | int(3000) + 100) }}"

Hereā€™s a reusable script. The parameters are the light entity to control, the number of steps you wish to have, and the number of steps to adjust (can be negative). For example, steps -2 and total_steps 5 would decrease the colour temperature by 2/5 of the range of the light.

alias: Change relative colour temperature
fields:
  total_steps:
    name: Total steps
    description: Number of steps between minimum and maximum clour temperature
    selector:
      number:
        min: 1
        max: 10
        step: 1
    required: true
    default: 5
  light_entity:
    selector:
      entity: {}
    name: Entity
    required: true
    description: The light entity to adjust
  steps:
    selector:
      number:
        min: -10
        max: 10
        step: 1
    name: Steps
    description: Steps to adjust
    default: 1
    required: true
sequence:
  - service: light.turn_on
    data:
      color_temp: |-
        {{ min(
             max(
               (state_attr(light_entity, 'color_temp') or 0) + steps *
               ((state_attr(light_entity, 'max_mireds') or 1000) - (state_attr(light_entity, 'min_mireds') or 0))
               / total_steps,
               state_attr(light_entity, 'min_mireds') or 0
             ),
             state_attr(light_entity, 'max_mireds') or 1000
           )
        }}
    data_template:
      entity_id: "{{ light_entity }}"
icon: mdi:sun-thermometer-outline
mode: single
3 Likes

Try to replace color_temp with color_temp_kelvin .

service: light.turn_on
target:
  entity_id: light.arcobaleno
data:
  kelvin: '{{states.light.arcobaleno.attributes.color_temp_kelvin + 500}}'

So, why wonā€™t this automation work?

  • repeat:
    sequence:
    - action: light.turn_on
    metadata: {}
    data:
    brightness: ā€˜{{ state_attr(ā€™ā€˜light.living_room_recessedā€™ā€˜, ā€˜ā€˜brightnessā€™ā€™)|int -1}}ā€™
    target:
    entity_id: light.living_room_recessed
    - delay:
    hours: 0
    minutes: 0
    seconds: 6
    milliseconds: 0
    until:
    - condition: numeric_state
    entity_id: light.living_room_recessed
    attribute: brightness
    below: 30

I attempted the same thing in parallel with kelvin, itā€™s not working either. I thought perhaps they were conflicting, but I canā€™t get either to operate.
The template values show up correctly in the dev tools template editor.

This appears to be a problem with the smartthings integration I had to access these bulbs through. I am using Cloudflared to access my domain. Getting a p.client_exceptions.ClientResponseError: 520, message=ā€˜Server Error (520)ā€™, url=ā€˜https://api.smartthings.com/v1/devices/---redacted---/commandsā€™