IKEA FÖRNUFTIG in Home-Assistant

lul


:arrow_down: Rotary switch (optional)

esphome:

output:
  - platform: esp8266_pwm
    pin: D1
    frequency: 150 Hz
    id: pwm_output
    min_power: 0.5
    max_power: 0.5
    
fan:
  - platform: speed
    output: pwm_output
    name: "Fornuftig Air Purifier"
    id: "fornuftig_fan"
    on_speed_set:
      lambda: |-
        if(id(fornuftig_fan).speed != id(fan_speed) && id(fornuftig_fan).state){
          id(fan_speed) = id(fornuftig_fan).speed;
          id(set_fan_freq).publish_state(id(fornuftig_fan).speed);
        }
    on_turn_on:
      lambda: |-
        if(id(fornuftig_fan).speed > 15){
          id(set_fan_freq).publish_state(id(fornuftig_fan).speed);
        }else{
          id(set_fan_freq).publish_state(id(speed_1));
        }
    on_turn_off:
      - output.esp8266_pwm.set_frequency:
          id: pwm_output
          frequency: !lambda 'return int(0);'
    
binary_sensor:
  - platform: template
    id: 'knob_0'
    filters:
      - delayed_on_off: 300ms
    lambda: |-
      if (!id(knob_1).state && !id(knob_2).state && !id(knob_3).state) {
        return true;
      } else {
        return false;
      }
    on_press: 
      lambda: |-
        if(!id(speed_0)){
          auto call = id(fornuftig_fan).turn_off();
          call.perform();
        }else{
          auto call = id(fornuftig_fan).turn_on();
          call.set_speed(id(speed_0));
          call.perform();
        }

  - platform: gpio
    id: 'knob_1'
    pin:
      number: D7
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on_off: 100ms
    on_press: 
      then:
        - fan.turn_on:
            id: fornuftig_fan
            speed: !lambda 'return id(speed_1);'
    
    
  - platform: gpio
    id: 'knob_2'
    pin:
      number: D6
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on_off: 100ms
    on_press: 
      then:
        - fan.turn_on:
            id: fornuftig_fan
            speed: !lambda 'return id(speed_2);'
  
  
  - platform: gpio
    id: 'knob_3'
    pin:
      number: D5
      mode: INPUT_PULLUP
      inverted: true
    filters:
      - delayed_on_off: 100ms
    on_press:
      then:
        - fan.turn_on:
            id: fornuftig_fan
            speed: !lambda 'return id(speed_3);'
    
    
      
      
sensor:
  - platform: template
    id: set_fan_freq
    filters:
      - multiply: 3
      - lambda: |-
          if (id(fornuftig_fan).state){
            if(x > 300){ // limit max frequency
              return 300;
              
            }else if(x < 60 && x > 3){ // limit low frequency
              return 60;
              
            } else if(x <= 3){
              return 0;
              
            } else {
              return x;
              
            }
          }else{
            return 0;
          }
    on_value:
      then:
        - output.esp8266_pwm.set_frequency:
            id: pwm_output
            frequency: !lambda 'return int(x);'
        - lambda: |- 
            // send current status to homeassistant
            if(x){
              auto call = id(fornuftig_fan).turn_on();
              call.set_speed(int(x)/3);
              call.perform();
            }else{
              auto call = id(fornuftig_fan).turn_off();
              call.perform();
            }



    
  - platform: homeassistant
    id: ha_fornuftig_speed_0
    entity_id: input_number.fornuftig_speed_0
    on_value:
      - globals.set:
          id: speed_0
          value: !lambda 'return int(x);'
          
          
  - platform: homeassistant
    id: ha_fornuftig_speed_1
    entity_id: input_number.fornuftig_speed_1
    on_value:
      - globals.set:
          id: speed_1
          value: !lambda 'return int(x);'
         
         
  - platform: homeassistant
    id: ha_fornuftig_speed_2
    entity_id: input_number.fornuftig_speed_2
    on_value:
      - globals.set:
          id: speed_2
          value: !lambda 'return int(x);'
        
        
  - platform: homeassistant
    id: ha_fornuftig_speed_3
    entity_id: input_number.fornuftig_speed_3
    on_value:
      - globals.set:
          id: speed_3
          value: !lambda 'return int(x);'
        
        
        
globals:
  - id: fan_speed
    type: int
    restore_value: yes
    initial_value: '0'

  - id: speed_0
    type: int
    restore_value: yes
    initial_value: '0'
    
  - id: speed_1
    type: int
    restore_value: yes
    initial_value: '55'
    
  - id: speed_2
    type: int
    restore_value: yes
    initial_value: '65'
    
  - id: speed_3
    type: int
    restore_value: yes
    initial_value: '100'
    

homeassistant:

input_number:
  fornuftig_speed_0:
    name: 'Fornuftig Speed 0'
    min: 0
    max: 100
    step: 1
    mode: slider
    unit_of_measurement: '%'
    icon: mdi:air-filter
  fornuftig_speed_1:
    name: 'Fornuftig Speed 1'
    min: 0
    max: 100
    step: 1
    mode: slider
    unit_of_measurement: '%'
    icon: mdi:air-filter
  fornuftig_speed_2:
    name: 'Fornuftig Speed 2'
    min: 0
    max: 100
    step: 1
    mode: slider
    unit_of_measurement: '%'
    icon: mdi:air-filter
  fornuftig_speed_3:
    name: 'Fornuftig Speed 3'
    min: 0
    max: 100
    step: 1
    mode: slider
    unit_of_measurement: '%'
    icon: mdi:air-filter
3 Likes