Color temperature change action

Setup Esp32 with this configuration ESPHome:

esphome:
  name: room_light

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "2bb927ef9bd6f92cf78e314d882176e7"

wifi:
  ssid: 
  password: 

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32 Fallback Hotspot"
    password: "ytjYEl8Vudtl"

captive_portal:

output:
  - platform: ledc
    id: gpio_19
    pin: GPIO19
    frequency: 19531Hz
    
  - platform: ledc
    id: gpio_18
    pin: GPIO18
    frequency: 19531Hz


light:
  - platform: cwww
    name: "Livingroom Lights"
    cold_white: gpio_18
    warm_white: gpio_19
    cold_white_color_temperature: 6536 K
    warm_white_color_temperature: 2000 K
    constant_brightness: false

When I create automation in HA, there is no actions to change color temperature, only for brightness. But I can change it from UI card. How can I add actions to automation?

UPD
I need to change color temperature with rotation of Mi Cube. As I understand, is a can do it with change set color_temp parameter of service Light: Turn on, but I don’t understand how to do this.

UPD2
Now I can change color temperature to any specified value in mireds with this automation YAML code, but I don’t understand how to increase and decrease it with rotation.:

- id: '4637506987354'
  alias: ChangeColorTemp
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: 7741d39c619b7082d69125c514ccec22
    type: action
    subtype: rotate_right
    discovery_id: 0x00158d000708191f action_rotate_right
  condition: []
  action:
  - service: light.turn_on
    target:
     entity_id: light.livingroom_lights
    data: 
     color_temp: 255
  mode: single

Go to dev tools and find the entity that it created. There you will see the color setting mode for how temperature is set. Use that setting to change the value in the service call.

Finally, I found the solution. Perhaps this can be done easier or more correctly. But it works)
Maybe it will helpful for someone else.

- id: '4637506987354'
  alias: DecreaseColorTemp
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: 7741d39c619b7082d69125c514ccec22
    type: action
    subtype: rotate_right
    discovery_id: 0x00158d000708191f action_rotate_right
  condition: []
  action:
  - service: light.turn_on
    target:
     entity_id: light.livingroom_lights
    data:
     color_temp: >
       {{state_attr('light.livingroom_lights', 'color_temp')|int + 40}}
  mode: restart
  
- id: '4567851253698'
  alias: EncreaseColorTemp
  description: ''
  trigger:
  - platform: device
    domain: mqtt
    device_id: 7741d39c619b7082d69125c514ccec22
    type: action
    subtype: rotate_left
    discovery_id: 0x00158d000708191f action_rotate_left
  condition: []
  action:
  - service: light.turn_on
    target:
     entity_id: light.livingroom_lights
    data:
      color_temp: >
       {{state_attr('light.livingroom_lights', 'color_temp')|int - 40}}
  mode: restart
1 Like

Thanks for this, I;ve used this script to program my STYRBAR left / right to change color temp using the ZHA - IKEA four button remote (Styrbar) for lights (E2001, E2002) blueprint.

In this scenario I’ve had the issue that when the lamp has been off the color_temp value is None so parsing it to an int causes an error. I’ve solved it by adding the following filter:

{{ state_attr('light. livingroom_lights', 'color_temp') | default(0,True) | int + 50 }}

The default(0,True) sets the value to zero, True is required to make it work.

Entire Yaml script for the left button:

service: light.turn_on
target:
  entity_id: light.livingroom_lights
data:
  color_temp: >
    {{ state_attr( 'light.livingroom_lights', 'color_temp' ) | default( 0, True
    ) | int + 50 }}

Right button uses the same script, but with - 50.

I hope this helps someone.

1 Like