『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 918|回复: 2

[求助] esphome风扇挡位

[复制链接]

20

主题

265

帖子

1852

积分

金牌会员

Rank: 6Rank: 6

积分
1852
金钱
1587
HASS币
0
发表于 2023-3-25 20:08:36 | 显示全部楼层 |阅读模式
本帖最后由 kaka0992 于 2023-3-25 20:10 编辑

抄了个大神写的三档的风扇调节,我这边想增加到5档调节,不知道这个代码该怎么改

substitutions:
  device_name: "auto Fan"
  device_name_low: "auto-fan"
  comment: "auto Fan"
  version: "1.0.0"
  speed_low: "30"
  speed_med: "50"
  speed_full: "80"
  
esphome:
  name: "${device_name_low}"
  project:
    name: "n1.fan_controller"
    version: "${version}"
  on_boot:
    then:
      - delay: 10s
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_low}"
      - delay: 5s
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_full}"

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "12345678"

wifi:
  ssid: 1111  #WiFi SSID
  password:111  #WiFi密码

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name_low}"
    password: "12345678"

captive_portal:

web_server:
  port: 80




output:
  - platform: esp8266_pwm
    pin: GPIO2    #D4
    frequency: 15000 Hz
    id: pwm_output

fan:
  - platform: speed
    id: fan_speed
    output: pwm_output
    speed_count: 100
    name: "${device_name} 开关"
   
number:
  - platform: template
    icon: "mdi:fan"
    id: speed_input
    name: "${device_name} 风速"
    lambda: |-
      return id(fan_speed).speed;
    step: 1
    min_value: 20
    max_value: 100
    unit_of_measurement: "%"
    update_interval: 1s
    set_action:
      then:
        - fan.turn_on:
            id: fan_speed
            speed: !lambda "return x;"
        - component.update: fanmode
        - component.update: sensor_fanspeed

select:
  - platform: template
    icon: "mdi:fan"
    name: "${device_name} 模式"
    id: fanmode
    update_interval: 1s
    lambda: |-
      if(id(fan_speed).speed <= ${speed_low})
        return std::string("Low");
      else if(id(fan_speed).speed == ${speed_med})
        return std::string("Medium");
      else if(id(fan_speed).speed >= ${speed_full})
        return std::string("Full");
      else
        return std::string("Custom");
    set_action:
      then:
        - lambda: |-
            auto call = id(fan_speed).turn_on();
            if(x == "Low")
              call.set_speed(${speed_low});
            else if(x == "Medium")
              call.set_speed(${speed_med});
            else if(x == "Full")
              call.set_speed(${speed_full});
            else
              call.set_speed(id(speed_input).state);
              
            call.perform();
        - component.update: speed_input
        - component.update: sensor_fanspeed
    options:
      - Low
      - Medium
      - Full
      - Custom

button:
  - platform: template
    icon: "mdi:fan-speed-1"
    name: "${device_name} Low"
    id: fanlow
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_low}"
  - platform: template
    icon: "mdi:fan-speed-2"
    name: "${device_name} Med"
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_med}"
  - platform: template
    icon: "mdi:fan-speed-3"
    name: "${device_name} Full"
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_full}"
  - platform: restart
    name: "${device_name} 初始化"

sensor:
  - platform: template
    id: sensor_fanspeed
    icon: "mdi:fan"
    name: "风扇速度"
    lambda: |-
      return id(fan_speed).speed;
    update_interval: 1s
    accuracy_decimals: 0
    unit_of_measurement: "%"
  - platform: dht
    pin: GPIO4    #D2
    temperature:
      name: "${device_name} Temperature"
    humidity:
      name: "${device_name} Humidity"
    model: AUTO_DETECT
    update_interval: 5s #获取频率
    

  
   
 


评分

参与人数 1金钱 +20 收起 理由
dscao + 20 感谢楼主分享!

查看全部评分

回复

使用道具 举报

20

主题

265

帖子

1852

积分

金牌会员

Rank: 6Rank: 6

积分
1852
金钱
1587
HASS币
0
 楼主| 发表于 2023-3-25 20:29:53 | 显示全部楼层
这样改的 不知道对不对
substitutions:
  device_name: "auto Fan"
  device_name_low: "auto-fan"
  comment: "auto Fan"
  version: "1.0.0"
  speed_1: "10"
  speed_2: "30"
  speed_3: "50"
  speed_4: "80"
  speed_5: "100"
  
esphome:
  name: "${device_name_low}"
  project:
    name: "n1.fan_controller"
    version: "${version}"
  on_boot:
    then:
      - delay: 10s
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_1}"
      - delay: 5s
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_5}"

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "12345678"

wifi:
  ssid: 1111  #WiFi SSID
  password:111  #WiFi密码

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name_low}"
    password: "12345678"

captive_portal:

web_server:
  port: 80




output:
  - platform: esp8266_pwm
    pin: GPIO2    #D4
    frequency: 15000 Hz
    id: pwm_output

fan:
  - platform: speed
    id: fan_speed
    output: pwm_output
    speed_count: 100
    name: "${device_name} 开关"
   
number:
  - platform: template
    icon: "mdi:fan"
    id: speed_input
    name: "${device_name} 风速"
    lambda: |-
      return id(fan_speed).speed;
    step: 1
    min_value: 10
    max_value: 100
    unit_of_measurement: "%"
    update_interval: 1s
    set_action:
      then:
        - fan.turn_on:
            id: fan_speed
            speed: !lambda "return x;"
        - component.update: fanmode
        - component.update: sensor_fanspeed

select:
  - platform: template
    icon: "mdi:fan"
    name: "${device_name} 模式"
    id: fanmode
    update_interval: 1s
    lambda: |-
      if(id(fan_speed).speed <= ${speed_1})
        return std::string("1");
      else if(id(fan_speed).speed == ${speed_2})
        return std::string("2");
      else if(id(fan_speed).speed >= ${speed_3})
        return std::string("3");
      else if(id(fan_speed).speed == ${speed_4})
        return std::string("4");
      else if(id(fan_speed).speed >= ${speed_5})
        return std::string("5");
      else
        return std::string("Custom");
    set_action:
      then:
        - lambda: |-
            auto call = id(fan_speed).turn_on();
            if(x == "1)
              call.set_speed(${speed_1});
            else if(x == "2")
              call.set_speed(${speed_2});
            else if(x == "3")
              call.set_speed(${speed_3});
            else if(x == "4")
              call.set_speed(${speed_4});
            else if(x == "5")
              call.set_speed(${speed_5});
            else
              call.set_speed(id(speed_input).state);
              
            call.perform();
        - component.update: speed_input
        - component.update: sensor_fanspeed
    options:
      - 1
      - 2
      - 3
      - 4
      - 5
      - Custom

button:
  - platform: template
    icon: "mdi:fan-speed-1"
    name: "${device_name} 1"
    id: fanlow
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_1}"
  - platform: template
    icon: "mdi:fan-speed-2"
    name: "${device_name} 2"
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_2}"
  - platform: template
    icon: "mdi:fan-speed-3"
    name: "${device_name} 3"
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_3}"
  - platform: template
    icon: "mdi:fan-speed-4"
    name: "${device_name} 4"
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_4}"
  - platform: template
    icon: "mdi:fan-speed-5"
    name: "${device_name} 5"
    on_press:
      - fan.turn_on:
          id: fan_speed
          speed: "${speed_5}"
  - platform: restart
    name: "${device_name} 初始化"

sensor:
  - platform: template
    id: sensor_fanspeed
    icon: "mdi:fan"
    name: "风扇速度"
    lambda: |-
      return id(fan_speed).speed;
    update_interval: 1s
    accuracy_decimals: 0
    unit_of_measurement: "%"
  - platform: dht
    pin: GPIO4    #D2
    temperature:
      name: "${device_name} Temperature"
    humidity:
      name: "${device_name} Humidity"
    model: AUTO_DETECT
    update_interval: 5s #获取频率
    

  
   
 

评分

参与人数 1金钱 +10 收起 理由
sorrypqa + 10 大神666!

查看全部评分

回复

使用道具 举报

51

主题

1340

帖子

5600

积分

论坛DIY达人

积分
5600
金钱
4260
HASS币
20
发表于 2023-3-25 22:46:50 | 显示全部楼层
kaka0992 发表于 2023-3-25 20:29
这样改的 不知道对不对

好复杂,学习一下,谢谢分享!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-25 00:03 , Processed in 1.062190 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表