Replicate fan code to light

Hi there. I have a new light-fan controlled by an IR remote controller. I already have the codes for every button of the controller.

I have configured the fan with enough success:

fan:
  - platform: template
    name: "Ventilador techo dormitorio"
    id: ventilador_techo_dormitorio
    speed_count: 6
    restore_mode: ALWAYS_OFF
    on_turn_on:
      - remote_transmitter.transmit_nec: #Send IR signal to turn on the fan
          address: 0x7F80
          command: 0xE01F
      - switch.turn_on: switch_ventilador_techo_dormitorio #Save fan state
    on_turn_off:
      - remote_transmitter.transmit_nec:
          address: 0x7F80
          command: 0xE01F
      - switch.turn_off: switch_ventilador_techo_dormitorio
    on_speed_set:
      - if:
          condition:
            lambda: return ((x>id(numero_velocidad).state));
          then:
            - repeat:
                count: !lambda |-
                  return (x-id(numero_velocidad).state);
                then:
                  - delay: 100ms                      
                  - remote_transmitter.transmit_nec:
                      address: 0x7F80
                      command: 0xE41B
      - if:
          condition:
            lambda: return ((x<id(numero_velocidad).state));
          then:
            - repeat:
                count: !lambda |-
                  return (id(numero_velocidad).state-x);
                then:
                  - delay: 100ms                      
                  - remote_transmitter.transmit_nec:
                      address: 0x7F80
                      command: 0xEE11
      - delay: 100 ms
      - number.set:
          id: numero_velocidad
          value: !lambda |-
            return (x);
    on_direction_set:
      - remote_transmitter.transmit_nec:
          address: 0x7F80
          command: 0xF807  

Now I have to create the entity light but seeing the Light Component and that Template does not exist I have no idea where to start.

The light has:

  • Two buttons for increase/crease color temperature.
  • Two buttons for increase/decrease brightness

I would expect a syntax that looks like the on_set_speed from the fan for both the color temperature and brightness.

Any idea on how to configure them?

Thank you!

I’m not sure but you might be able to use a:

with two:

Color temperature light could fit as well. Together with dummy gpio or template output.
You will have to play with lambdas…

output:
  - platform: ledc
    id: warm_output
    pin: GPIO13 #dummy output to some unused pin

  - platform: ledc
    id: cold_output
    pin: GPIO14 #dummy output to some unused pin


light:
  - platform: color_temperature
    name: "CT Light"
    id: ct_light
    warm_white: warm_output
    cold_white: cold_output
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K

    on_state:
      - lambda: |-
          float ct = x.current_values.get_color_temperature();
          float br = x.current_values.get_brightness();

I would need more for the lambdas.

I understand that “on_state” means: When I set a temperature for the light on HA, this send this temperature to ESPHome as a x variable and I need to code what IR code needs to be sent depending on this x value?

I don’t know where to start with the lambda syntax, though…

Any further help would be appreciatrd. Thank you.

More less like that. What you need here is brightness and color_temperature.
Brightness is float value from 0 to 1, half brightness for example gives 0.5.
But I’m not sure what units color_temperature outputs, the docs are not clear.
Add this line to the lambda and check what it prints when you set the temp to min and max.
ESP_LOGD("debug", "CT: %.2f Brightness: %.2f", ct, br);

It gives an error:

Compiling .pioenvs/luz-ventilador-dormitorio/src/main.cpp.o
/config/esphome/luz-ventilador-dormitorio.yaml: In lambda function:
/config/esphome/luz-ventilador-dormitorio.yaml:140:18: error: ‘x’ was not declared in this scope
140 | float ct = x.current_values.get_color_temperature();
| ^
*** [.pioenvs/luz-ventilador-dormitorio/src/main.cpp.o] Error 1
========================= [FAILED] Took 12.43 seconds =========================

This is what I have:

light:
  - platform: color_temperature
    name: "Luz dormitorio"
    id: luz_dormitorio
    color_temperature: warm_output
    brightness: cold_output   
    #warm_white: warm_output
    #cold_white: cold_output
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K

    on_state:
      - lambda: |-
          float ct = x.current_values.get_color_temperature();
          float br = x.current_values.get_brightness();
          ESP_LOGD("debug", "CT: %.2f  Brightness: %.2f", ct, br);


Ok, no x here. Try like this:

 on_state:
   - lambda: |-
       auto st = id(luz_dormitorio).state;
       float ct = st->current_values.get_color_temperature();
       float br = st->current_values.get_brightness();
       ESP_LOGD("debug", "CT: %.2f , Brightness: %.2f", ct, br);

Another error:

Compiling .pioenvs/luz-ventilador-dormitorio/src/main.cpp.o
/config/esphome/luz-ventilador-dormitorio.yaml: In lambda function:
/config/esphome/luz-ventilador-dormitorio.yaml:139:33: error: ‘class esphome::light::LightState’ has no member named ‘state’
139 | auto st = id(luz_dormitorio).state;
| ^~~~~
*** [.pioenvs/luz-ventilador-dormitorio/src/main.cpp.o] Error 1
========================= [FAILED] Took 11.72 seconds =========================

I give up trying to get the value directly from the light component itself, I had a look at the library and it’s a mess with all the light types.

Another approach to get the output values:

output:
  - platform: template
    id: brightness_output
    type: float
    write_action:
      - lambda: |-
         

  - platform: template
    id: ct_output
    type: float
    write_action:
      - lambda: |-
          


light:
  - platform: color_temperature
    id: luz_dormitorio
    name: "Luz dormitorio"
    brightness: brightness_output
    color_temperature: ct_output
    cold_white_color_temperature: 6500 K
    warm_white_color_temperature: 2700 K

    on_state:
      - lambda: |-
          float br = id(brightness_output).state; 
          float ct = id(ct_output).state;          

          ESP_LOGD("debug", "CT: %.2f, Brightness: %.2f", ct, br);

I swapped to template output.
Automations can be done either on light component or on output.

Error again…

Compiling .pioenvs/luz-ventilador-dormitorio/lib567/libsodium/crypto_core/ed25519/ref10/ed25519_ref10.o
/config/esphome/luz-ventilador-dormitorio.yaml: In lambda function:
/config/esphome/luz-ventilador-dormitorio.yaml:142:37: error: ‘class esphome::template_::TemplateFloatOutput’ has no member named ‘state’
142 | float br = id(brightness_output).state;
| ^~~~~
/config/esphome/luz-ventilador-dormitorio.yaml:143:29: error: ‘class esphome::template_::TemplateFloatOutput’ has no member named ‘state’
143 | float ct = id(ct_output).state;
| ^~~~~
*** [.pioenvs/luz-ventilador-dormitorio/src/main.cpp.o] Error 1
========================= [FAILED] Took 120.54 seconds =========================

My mistake, esphome doesn’t expose output state, so it needs to be done on output then.

output:
  - platform: template
    id: brightness_output
    type: float
    write_action:
      - lambda: |-
          ESP_LOGD("debug", "Brightness: %.2f", state);

  - platform: template
    id: ct_output
    type: float
    write_action:
      - lambda: |-
          ESP_LOGD("debug", "Color Temperature: %.2f", state);

light:
  - platform: color_temperature
    id: luz_dormitorio
    name: "Luz dormitorio"
    brightness: brightness_output
    color_temperature: ct_output
    cold_white_color_temperature: 6500
    warm_white_color_temperature: 2700

I added the “K” to temperatures and it does not show errors. When I change the brightness and temperaure shows:

For 2700 K it shows:

[16:59:18.008][D][debug:130]: Color Temperature: 1.00

For 6500 K:

[17:00:00.336][D][debug:130]: Color Temperature: 0.00

For 100 % brightness:

[17:00:36.946][D][debug:123]: Brightness: 1.00

For 1 % brightness:

[17:00:55.581][D][debug:123]: Brightness: 0.00

Ok, finally. Do you need both values together to do your remote transmits? Or they are separate transmits for color and brightness?

Interesting conclusion that CT-light state is not readily available on esphome for automations. At least for me, some smarter guys could add here.

They are separate. There is four IR codes; brigthness uo/down and color up/down.

Both magnitudes have 10 levels. On the fan I was able to template the difference between the current and proposed speed value so the action (IR codes sent) repeated that number of times. I would need the same here. I set 6 steps for the fan:

    speed_count: 6

But i didn’t see the “steps” code for the brightness and color temperature, do they exist?

No, but does it matter? If you have current brightness 0.3 and new value 0.6 you can calculate the repeats same way.

You’re right.

How should I define the lambda under the brightness/ct option to operate as I did with the x on the fan?

Same way, you have variable state .
Did you use template number to save the previous speed?

Yes I used a number.

Same way? I do not have an x this time, do I? Thank you very much.

I am seeing that there is no linear correspondence from the brigthness:

HA 50 % brigthness shows as 0.15 on ESPHome, if linear, it should show a 0.5.

If it is not linear is not possible to calculate the steps as I did with thw fan, is it?

If you have x or you have state, what’s the difference?

((x<id(numero_velocidad).state))
((state<id(numero_velocidad).state))

Light components are PITA when you try to “abuse” them :grinning:
Add this to light component and try again:
gamma_correct: 1.0