Fan Speed Automation based on Temperature and Humidity


I’m trying to build a Fan Automation based on Temperature and Humidity with a custom formula within ESPHome.

I’ve played around a bit with templates and it kinda works, but it doesn’t feel right:

  - platform: template
    name: "Fan Test"
    id: fan_test
    update_interval: 5s
    lambda: |-
      if(id(hum).state < 35 && id(temp).state < 25) {
        return {"OFF"};
      } else if(id(hum).state < 40) {
        return {"LOW"};
      } else {
        return {"HIGH"};
        lambda: |-
          if(x == "LOW") {
          } else if(x == "HIGH") {
          } else {

(the formula is just for testing the code)

The function calls to turn on / off and set speed look strange, but it seems right according to the c++ source code:

And it seems a bit cumbersome to first return OFF / LOW / HIGH strings, and afterwards use these strings to trigger the according action.

Is this the right way to do it, or is there a better or simpler way to get this working?

(Basically I want a thermostat for temperature and humidity, which then controls a fan with multiple speeds)

Thanks in advance!

Could you use a climate control with a template (which takes into account humidity) for temperature?

I guess yes (?). But would that make things simpler?
Or maybe I didn’t understand what tried to tell me.