Problem: Sending Hue & Saturation values with light.turn_on from ESPHome to Home Assistant

Hi Folks, I’m struggling to pass hs_color when calling homeassistant.service light.turn_on from ESPHome:

 - homeassistant.service:
     service: light.turn_on
     data:
       entity_id: light.my_light
       brightness: "255"
       hs_color: "??????"

Home Assistant expects a list for hs_color, but I can’t for the life of me figure out how to convert a float vector to a list. If anyone has been successful, I would value your input so much.

In Home Assistant I can use the following:

service: light.turn_on 
data: 
  brightness: 255
  hs_color: [120.0,50.0]

or

service: light.turn_on 
data: 
  brightness: 255
  hs_color:
    - 120.0
    - 50.0

Context: I have a regular light switch converted to momentary switch with an esp8266 attached. Trying to control brightness and color of a wifi bulb e.g. lifx mini with the switch.

Concept:

  • Single click = on/off
  • Press and hold = dim/brighten
  • Double click = switch to color change mode
  • Press and hold = cycle slowly through the color wheel
  • Double click = return to brightness control

Any suggestions or solutions will be much appreciated. At the moment I have a script in Home Assistant that I am calling from ESPHome to perform the action, but it’s a really clunky solution :shushing_face:

service: light.turn_on
data_template:
  entity_id: '{{ target_entity_id }}'
  transition: '{{ target_transition|float }}'
  hs_color:
    - '{{ target_hue|float }}'
    - '{{ target_sat|float }}'

TiA, D.

Okay! For anyone else looking for an answer to this - now that templates are strongly typed in HA (or are evaluated to types rather than just strings) this is no longer an issue. The following code works as expected:

- homeassistant.service:
    service: "light.turn_on"
    data:
      entity_id: "light.my_light"
      brightness: "255"
      transition: !lambda 'return id(transition_time);'
    data_template:
      hs_color: "{{ (target_hue|float, target_sat|float)|list }}"
    variables:
      target_hue: "return id(hue_state);"
      target_sat: "return id(sat_state);"

Thank you HA Team for making this available :+1: :+1: :raised_hands:

PS - note the difference where !lambda is used when sending the global variable id(transition_time) as a value for transition: BUT… when using variables: with data_template, the values for each variable are evaluated as lambdas by default, so don’t use the !lambda declaration.

The lambdas for variables target_hue and target_sat are evaluated by ESPHome prior to submitting the template for hs_color to Home Assistant. The template, with already evaluated lambdas for the variables is then sent to Home Assistant for evaluation, and HA evaluates the template to a list (which is required when specifying hs_color)

I hope this is helpful to someone.

2 Likes