Retrieve the current speed of the fan

Im new to esphome and i have a problem about lambda calls, On Fan Components theres a lambda call to retrive the current speed of a Fan then do something according to speed. the code is below.

// Within lambda, get the fan speed and conditionally do something
if (id(my_fan).speed == 2) {
// Fan speed is 2, do something here
} else {
// Fan speed is not 2, do something else here
}

Can someone tell me where to paste or edit this code? because i already paste it on my ESPHOME module device code and its comming up with errors… could someone tell me what am i missing?
My goal is to get the fan speed then turn ON one of my 3 relay switch according to fan speed. hope someone can help me.

Sounds like you should put the code under an on_speed_set trigger.

You can use x rather than (id(my_fan).speed in that context per docs.

but how can i use [ x ] in my code? sorry im really new to this… heres what i got so far… check picture below.
crop1
and heres the error… below…


by reading the error seems i cannot use the bracket to replace the speed number to [ x ] can you pls tell me how to write this correct… thanks…

I’ve seen mideahvac.yaml code contains lambda calls to allow you to select the fan speed from the ESPHome Web UI. Perhaps it’ll give you some clues.

Midea branded AC’s with ESPhome (no cloud) - ESPHome - Home Assistant Community (home-assistant.io)

For your convenience I’ll paste the relevant part below

sensor:
  - platform: template
    name: ${friendly_node_name} fan mode
    id: ${node_id}_fan_mode
    internal: true
    update_interval: 10s
    lambda: !lambda |-
      if (id(${node_id}_my_climate).fan_mode == 2)
      {
        if (id(${node_id}_select_fan_mode).state != "Auto")
        {
          auto call = id(${node_id}_select_fan_mode).make_call();
          call.set_option("Auto");
          call.perform();
        }
      }
      else if (id(${node_id}_my_climate).fan_mode == 3)
      {
        if (id(${node_id}_select_fan_mode).state != "Low")
        {
          auto call = id(${node_id}_select_fan_mode).make_call();
          call.set_option("Low");
          call.perform();
        }
      }
      else if (id(${node_id}_my_climate).fan_mode == 4)
      {
        if (id(${node_id}_select_fan_mode).state != "Medium")
        {
          auto call = id(${node_id}_select_fan_mode).make_call();
          call.set_option("Medium");
          call.perform();
        }
      }
      else if (id(${node_id}_my_climate).fan_mode == 5)
      {
        if (id(${node_id}_select_fan_mode).state != "High")
        {
          auto call = id(${node_id}_select_fan_mode).make_call();
          call.set_option("High");
          call.perform();
        }
      }
      return id(${node_id}_my_climate).fan_mode;


# Select
select:
  - platform: template
    name: "Fan mode"
    id: ${node_id}_select_fan_mode
    icon: mdi:fan
    optimistic: true
    options:
      - Auto
      - Low
      - Medium
      - High
    on_value:
      then:
        - lambda: |-
            auto call = id(${node_id}_my_climate).make_call();
            call.set_fan_mode(x.c_str());
            call.perform();
1 Like

Which show on esp web like:


I added this upon request, as a user wanted to use standalone (so without HA):wink:

1 Like

Thanks a lot ill look on to this then make update here once i figure out how this work…:+1: