分享 透過紅外以及計電量插座 控制風扇速度

如題先上圖


使用計電量插座來算出目前風扇速度
在使用其他方式來設定你要調整的風速

前置作業

  1. 先到助手這邊新增幾個虛擬按鍵(虛擬按鍵數量依照你的風速去設定)以及2個計數器


    以我的奇美風扇來說 風速有七段所以新增了7個風速按鈕 以及2個計數器

    計數器請依照你的風速設定最小值為0 最大值為你的風扇段數-1
    升速降速設定都一樣只有名稱不同而已

  2. 設定風扇的升降速腳本範例如下請依照你的實體調整yaml內容
    至於要放哪邊依照你的習慣放置

script:
  fan_speed_up:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.fang_jian_rm4c_mini2
          device: Chimei DF-14D601
          command: Speedup
  fan_speed_down:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.fang_jian_rm4c_mini2
          device: Chimei DF-14D601
          command: Speeddown
  1. 使用 template 創建一個判斷目前風速的 sensor
    範例如下
sensor:
  - platform: template
    sensors:
      fan_speed:
        friendly_name: "電風扇風速"
        value_template: >-
          {% set consumption = states('sensor.qi_mei_feng_shan_cha_zuo_current_consumption')|float %}
          {% if consumption == 0 %}
            Off
          {% elif consumption <= 2.1 %}
            Speed 1
          {% elif consumption <= 2.4 %}
            Speed 2
          {% elif consumption <= 4.0 %}
            Speed 3
          {% elif consumption <= 6.2 %}
            Speed 4
          {% elif consumption <= 12.8 %}
            Speed 5
          {% elif consumption <= 18.0 %}
            Speed 6
          {% elif consumption <= 23.5 %}
            Speed 7
          {% else %}
            Unknown
          {% endif %}

sensor.qi_mei_feng_shan_cha_zuo_current_consumption 替換成你的電量偵測插座功耗實體
範例中value_template:裏面的數字請依照你的風扇實際功耗去調整 不要直接套用我的數字不然會不準

到此前置作業都已經完成
開始NR的設定


函數主要的作用是判斷哪個按鈕被按下去然後依照目前的風速去產出要升降速的回圈數量並設定計數器
函數內容如下

const ha = global.get('homeassistant').homeAssistant;
const FanSpeedState = ha.states["sensor.fan_speed"].state;

const speedMatch = FanSpeedState.match(/\d+/); 
let NowSpeed;

if (speedMatch) {
    NowSpeed = parseInt(speedMatch[0]);
} else {
    NowSpeed = 0;
}

var income_id;

if (msg.topic === "input_button.fan_speed_1") {
    income_id = 1;
} else if (msg.topic === "input_button.fan_speed_2") {
    income_id = 2;
} else if (msg.topic === "input_button.fan_speed_3") {
    income_id = 3;
} else if (msg.topic === "input_button.fan_speed_4") {
    income_id = 4;
} else if (msg.topic === "input_button.fan_speed_5") {
    income_id = 5;
} else if (msg.topic === "input_button.fan_speed_6") {
    income_id = 6;
} else if (msg.topic === "input_button.fan_speed_7") {
    income_id = 7;
} else {
    income_id = 0; 
}

const setCount = NowSpeed - income_id;

if (setCount < 0) {
    const positiveSetCount = Math.abs(setCount);
    msg.payload_inc_count = { "value": positiveSetCount };
    msg.payload_dec_count = { "value": "0" };
} else if (setCount > 0) {
    msg.payload_dec_count = { "value": setCount };
    msg.payload_inc_count = { "value": "0" };
} else {
    msg.payload_inc_count = { "value": "0" };
    msg.payload_dec_count = { "value": "0" };
}

return msg;

input_button.fan_speed_? 請改成你設定的風扇速度按鈕實體ID

其他節點設定方式如下

降速部分




升速部分




面板的multiple-entity-row

  - entity: switch.qi_mei_feng_shan_cha_zuo
    type: custom:multiple-entity-row
    toggle: true
    name: 速度設定
    state_header: 電源
    state_color: true
    icon: mdi:fan
    entities:
      - entity: sensor.fan_speed
        name: 目前風速
      - icon: mdi:numeric-1-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_1
      - icon: mdi:numeric-2-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_2
      - icon: mdi:numeric-3-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_3
  - entity: switch.qi_mei_feng_shan_cha_zuo
    type: custom:multiple-entity-row
    toggle: true
    name: 速度設定
    state_header: 電源
    state_color: true
    icon: mdi:fan
    entities:
      - icon: mdi:numeric-4-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_4
      - icon: mdi:numeric-5-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_5
      - icon: mdi:numeric-6-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_6
      - icon: mdi:numeric-7-box-outline
        tap_action:
          action: call-service
          service: input_button.press
          service_data:
            entity_id: input_button.fan_speed_7

switch.qi_mei_feng_shan_cha_zuo 請改成你的風扇電源

以上為本次分享
希望能幫到有需要的朋友

2個讚

無聊做了一些變更讓目前的風速直接顯示在按鈕上

要做這些修改 須增加一些template switch

switch:
  - platform: template
    switches:
      fan_speed_1:
        friendly_name: "Fan Speed 1"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 1') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_1
        turn_off:

  - platform: template
    switches:
      fan_speed_2:
        friendly_name: "Fan Speed 2"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 2') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_2
        turn_off:

  - platform: template
    switches:
      fan_speed_3:
        friendly_name: "Fan Speed 3"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 3') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_3
        turn_off:

  - platform: template
    switches:
      fan_speed_4:
        friendly_name: "Fan Speed 4"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 4') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_4
        turn_off:

  - platform: template
    switches:
      fan_speed_5:
        friendly_name: "Fan Speed 5"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 5') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_5
        turn_off:

  - platform: template
    switches:
      fan_speed_6:
        friendly_name: "Fan Speed 6"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 6') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_6
        turn_off:

  - platform: template
    switches:
      fan_speed_7:
        friendly_name: "Fan Speed 7"
        value_template: "{{ is_state('sensor.fan_speed', 'Speed 7') }}"
        turn_on:
          service: input_button.press
          data:
            entity_id: input_button.fan_speed_7
        turn_off:

面板設定

  - entity: switch.qi_mei_feng_shan_cha_zuo
    type: custom:multiple-entity-row
    toggle: true
    name: 速度設定
    state_header: 電源
    state_color: true
    icon: mdi:fan
    entities:
      - entity: sensor.fan_speed
        name: 目前風速
      - entity: switch.fan_speed_1
        name: false
        icon: mdi:numeric-1-box-outline
        state_color: true
        toggle: true
        tap_action:
          action: toggle
      - entity: switch.fan_speed_2
        name: false
        icon: mdi:numeric-2-box-outline
        state_color: true
        toggle: true
        tap_action:
          action: toggle
      - entity: switch.fan_speed_3
        name: false
        icon: mdi:numeric-3-box-outline
        state_color: true
        toggle: true
        tap_action:
          action: toggle
  - entity: switch.qi_mei_feng_shan_cha_zuo
    type: custom:multiple-entity-row
    toggle: true
    name: 速度設定
    state_header: 電源
    state_color: true
    icon: mdi:fan
    entities:
      - entity: switch.fan_speed_4
        name: false
        icon: mdi:numeric-4-box-outline
        state_color: true
        toggle: true
        tap_action:
          action: toggle
      - entity: switch.fan_speed_5
        name: false
        icon: mdi:numeric-5-box-outline
        state_color: true
        toggle: true
        tap_action:
          action: toggle
      - entity: switch.fan_speed_6
        name: false
        icon: mdi:numeric-6-box-outline
        state_color: true
        toggle: true
        tap_action:
          action: toggle
      - entity: switch.fan_speed_7
        name: false
        icon: mdi:numeric-7-box-outline
        toggle: true
        state_color: true
        tap_action:
          action: toggle

做了一些修改讓fan可以依照目前耗電量顯示比例
前置作業
configuration.yaml添加

python_script:

在config底下創建 python_scripts資料夾


下載remote_fan_speed_control.py並放到python_scripts底下

Remote Fan Speed Control 下載點
連結如果失效再通知我更改

完成上面動作後重開HA讓設定生效

設定資料看你要放configuration.yaml還是packages底下都可以
依照個人習慣去設定

input_text:
  status_chimei_fan_speed:
    name: 'Chimei DF-14D601 Speed'
    initial: 0 # //!<<< Adjust this value if your actual fan speed is not zero. Formula: round( current_speed / speed_count * 100 );

input_select:
  fan_osc:
    name: 'Fan osc'
    options:
      - 'True'
      - 'False'

script:
  chimei_fan_power:
      sequence:
        - service: input_text.set_value
          data_template:
            entity_id: input_text.status_chimei_fan_speed
            value: '{{4/7 * 100 | int}}'
        - service: remote.send_command
          data:
            entity_id: remote.rm4c_pro
            device: Chimei DF-14D601
            command: Power

switch:
  - platform: template
    switches:
      chimei_dc_fan_power:
        friendly_name: "Chimei DC Fan Power"
        value_template: "{{ states('sensor.qi_mei_feng_shan_cha_zuo_current_consumption')|float > 1.6 }}"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.chimei_fan_power
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.chimei_fan_power

fan:
  - platform: template
    fans:
      chimei_fan:
        friendly_name: "ChiMei Fan"
        speed_count: 7
        value_template: "{{ states('switch.chimei_dc_fan_power') }}"
        percentage_template: >-
          {% set consumption = states('sensor.qi_mei_feng_shan_cha_zuo_current_consumption')|float %}
          {% if consumption == 0 %}
            0
          {% elif consumption <= 2.0 %}
            14
          {% elif consumption <= 2.4 %}
            29
          {% elif consumption <= 4.0 %}
            43
          {% elif consumption <= 6.2 %}
            57
          {% elif consumption <= 12.8 %}
            71
          {% elif consumption <= 18.0 %}
            86
          {% elif consumption <= 23.5 %}
            100
          {% else %}
            0
          {% endif %}
        oscillating_template: "{{ states('input_select.fan_osc') }}"
        turn_on:
          - condition: state
            entity_id: switch.chimei_dc_fan_power
            state: 'off'
          - service: switch.turn_on
            entity_id: switch.chimei_dc_fan_power
        turn_off:
          - condition: state
            entity_id: switch.chimei_dc_fan_power
            state: 'on'
          - service: switch.turn_off
            entity_id: switch.chimei_dc_fan_power
        set_percentage:
          - service: python_script.remote_fan_speed_control
            data_template:
              percentage: "{{ percentage }}"
              entity_id: fan.chimei_fan
              speed_entity_id: input_text.status_chimei_fan_speed
              speed_count: 7
              service: remote.send_command
              service_data_decrease:
                entity_id: remote.rm4c_pro
                device: Chimei DF-14D601
                command: Speeddown
              service_data_increase:
                entity_id: remote.rm4c_pro
                device: Chimei DF-14D601
                command: Speedup
        set_oscillating:
          - condition: state
            entity_id: switch.chimei_dc_fan_power
            state: 'on'
          - service: remote.send_command
            data:
              entity_id: remote.rm4c_pro
              device: Chimei DF-14D601
              command: Oscillating
          - service: input_select.select_next
            entity_id: input_select.fan_osc

反應速度會依照你的計電量設備而有差別
本身用HS300反應就有點慢
如果用刷過esphome的s31應該可以很快

實體ID名稱有點多請自己看著修改了



匡起來的紅匡請依照你的速度去調整
我的是7段所以都是用7依照你的速度去調整段數


這邊請依照你的實際段數去修改相對應的比例
如果你也是七段就不需要修改
以上希望能幫到有需要的朋友

PS擺頭目前沒有加進來判斷 所以都是以不擺頭為準
當你風扇開始擺頭判斷就會失準