ESPHome IR Fan control

I have some Genio IR devices flashed with ESPhome which are working well to send IR signals to the ceiling fans I have. What I am trying to do is use the switch templates I’ve created to create a fan entity in home assistant so this works natively, with all code on the ESPHome. I’m sure it’s possible but I think I’m missing something, probably simple.

This is part of the code I’ve got for the switch. Then I have switches for Med and High.


remote_transmitter:
  pin: GPIO14
  carrier_duty_percent: 50%
  
remote_receiver:
  pin:
    number: GPIO5
    inverted: True
#  dump: raw

switch:
  - platform: template
    name: "Fan Off"
    turn_on_action:
      remote_transmitter.transmit_raw:
        code: [1238, -438, 1238, -439, 340, -1272, 1239, -438, 1239, -438, 1239, -439, 340, -1274, 340, -1273, 340, -1272, 1239, -438, 1240, -439, 340, -7436, 1238, -438, 1239, -439, 340, -1273, 1239, -438, 1240, -438, 1240, -439, 340, -1275, 340, -1273, 340, -1273, 1240, -437, 1240, -439, 340, -7263, 1239, -437, 1240, -439, 340, -1274, 1240, -438, 1240, -438, 1240, -440, 340, -1275, 340, -1274, 340, -1274, 1239, -438, 1240, -440, 340, -7242, 1239, -438, 1240, -439, 340, -1274, 1240, -438, 1240, -439, 1240, -440, 340, -1275, 340, -1275, 340, -1273, 1240, -438, 1240, -440, 340, -7266, 1238, -439, 1239, -440, 339, -1275, 1239, -440, 1239, -439, 1240, -440, 340, -1276, 339, -1276, 339, -1274, 1239, -439, 1240, -440, 339, -7246, 1238, -440, 1239, -440, 339, -1276, 1239, -439, 1239, -440, 1240, -441, 339, -1276, 339, -1277, 339, -1274, 1239, -440, 1239, -441, 339, -7574, 1238, -440, 1239, -441, 338, -1276, 1239, -440, 1239, -441, 1239, -441, 339, -1277, 339, -1277, 338, -1276, 1238, -440, 1239, -441, 339, -7452, 1238, -440, 1239, -441, 339, -1276, 1238, -441, 1239, -440, 1240, -441, 338, -1278, 338, -1277, 339, -1275, 1239, -441, 1238, -442, 338]
        carrier_frequency: 38kHz
  - platform: template
    name: "Fan Low"
    turn_on_action:
      remote_transmitter.transmit_raw:
        code: [1239, -438, 1239, -439, 339, -1273, 1239, -438, 1239, -439, 340, -1274, 340, -1272, 1240, -438, 340, -1274, 340, -1273, 340, -1273, 340, -7280, 1239, -438, 1240, -438, 340, -1273, 1240, -438, 1240, -439, 340, -1275, 340, -1273, 1239, -439, 340, -1275, 340, -1274, 340, -1273, 341, -7348, 1239, -438, 1240, -438, 341, -1273, 1240, -438, 1240, -439, 341, -1274, 341, -1273, 1240, -439, 340, -1274, 341, -1274, 340, -1273, 341, -7349, 1239, -438, 1240, -439, 340, -1274, 1240, -438, 1240, -439, 340, -1275, 341, -1273, 1240, -439, 340, -1275, 340, -1274, 340, -1274, 340, -7351, 1239, -439, 1239, -440, 340, -1274, 1240, -439, 1240, -440, 339, -1276, 340, -1274, 1240, -440, 339, -1276, 339, -1275, 340, -1275, 340, -7330, 1238, -439, 1239, -440, 340, -1274, 1240, -439, 1240, -440, 339, -1276, 340, -1274, 1240, -440, 339, -1276, 339, -1276, 340, -1274, 340, -7658, 1239, -439, 1239, -441, 339, -1275, 1239, -439, 1240, -441, 339, -1276, 340, -1274, 1239, -441, 339, -1277, 339, -1276, 339, -1275, 340, -7294, 1238, -439, 1239, -441, 339, -1276, 1239, -440, 1239, -441, 339, -1277, 339, -1275, 1239, -441, 339, -1276, 339, -1276, 339, -1276, 339]
        carrier_frequency: 38kHz

To have it all in ESPHome, it sounds like you would need the ESPHome equivalent of a Template Fan, which from what I can tell, doesn’t exist.

Perhaps there is a way to do it with say the speed fan and the template output.

Yes. I have been playing with the output option. Adding an id to an output and then specifying the switch commands, but so far failing!

output:
  - platform: template
    id: fan_1
    type: float 
    write_action:
      - output.set_level:
          id: fan_off
          level: !lambda return state;
      - output.set_level:
          id: fan_low
          level: !lambda return state;
      - output.set_level:
          id: fan_med
          level: !lambda return state;    
      - output.set_level:
          id: fan_high
          level: !lambda return state;

fan:
  - platform: speed
    id: officetestfan
    output: fan_1
    name: "Office Test Fan"
    speed_count: 3

The ID that I have on the switch template, doesn’t work. The error points to the IR code needing to be in the output field. But, I can’t seem to get the syntax right for that.

ID 'fan_high' of type template_::TemplateSwitch doesn't inherit from output::FloatOutput. Please double check your ID is pointing to the correct value.