紅外控制風扇 透過計電量插座 及 esphome + mpu6050達成擺頭感測 做成 Fan Template 完整控制風扇及回饋分享

之前分享過分享 透過紅外以及計電量插座 控制風扇速度不過並不是很完整
現透過esphome + mpu6050 達成擺頭反饋
並做成完整的Fan Template

前置作業

  1. 準備 esp晶片及mpu6050 6軸陀螺儀加速器並參考官方資料Esphome Mpu6050燒錄
    並將做好的感測器裝在風扇上面

    觀察 Gyro xyz 這三個角度的變化找出適合你自己的一個去當偵測的感測器

    以我的來說 Gyro X 在風扇不動的時候 會在-2%~-6%之間跳動

    而在擺頭時則會超過這些數值因此可以用Binary_sensor Template寫一個二進位感測器來判斷是否正在擺頭yaml如下
binary_sensor:
  - platform: template
    sensors:
      fan_osc:
        friendly_name: "風扇擺頭"
        value_template: "{{ not (states('sensor.mpu6050_gyro_x') | float >= -6.00 and states('sensor.mpu6050_gyro_x') | float <= -2.00) }}"
        device_class: motion
        delay_off:
          minutes: 0
          seconds: 5

delay_off 是要持續5秒都在設定的範圍內 才判斷為擺頭 自己可以調整秒數
value_template 為數值設定 調整裡面的數值符合你的場景
2. 使用黑豆學習電風扇全部的控制碼
分享個人的黑豆學碼步驟及習慣
3. 將所有學到的碼寫成Script
分享 黑豆學完碼後 我是如何設定成開關或者是腳本
下面是我自己使用中的設定

script:
  fan_power:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Power

  fan_speed_up:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Speedup

  fan_speed_down:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Speeddown

  fan_osc:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Oscillating

  fan_eco:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: ECO

  fan_mode:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Fanmode

  fan_timeclose:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Timeclose

  fan_timeopen:
    sequence:
      - service: remote.send_command
        data:
          entity_id: remote.rm4c_pro
          device: Chimei DF-14D601
          command: Timeopen

  1. 使用助手新增按鈕及2個計數器及NR設定
    設定方式參閱分享 透過紅外以及計電量插座 控制風扇速度的前置作業部分
  2. 使用Sensor Template設定3個判斷用Sensor
sensor:
  - platform: template
    sensors:
      fan_speed:
        friendly_name: "電風扇風速"
        value_template: >-
          {% set consumption = states('sensor.qi_mei_feng_shan_cha_zuo_current_consumption')|float %}
          {% set osc_on = is_state('binary_sensor.fan_osc', 'on') %}
          {% if osc_on %}
            {% if consumption < 4.7 %}
              Speed 1
            {% elif consumption < 5.0 %}
              Speed 2
            {% elif consumption < 6.5 %}
              Speed 3
            {% elif consumption < 8.5 %}
              Speed 4
            {% elif consumption < 15.0 %}
              Speed 5
            {% elif consumption < 19.5 %}
              Speed 6
            {% elif consumption < 25.0 %}
              Speed 7
            {% else %}
              Unknown
            {% endif %}
          {% else %}
            {% if consumption == 0 %}
              Off
            {% elif consumption <= 2.0 %}
              Speed 1
            {% elif consumption <= 2.4 %}
              Speed 2
            {% elif consumption <= 4.0 %}
              Speed 3
            {% elif consumption <= 6.2 %}
              Speed 4
            {% elif consumption <= 15.0 %}
              Speed 5
            {% elif consumption <= 18.0 %}
              Speed 6
            {% elif consumption <= 23.5 %}
              Speed 7
            {% else %}
              Unknown
            {% endif %}
          {% endif %}

  - platform: template
    sensors:
      fan_speed_percentage:
        friendly_name: "電風扇風速百分比"
        unit_of_measurement: "%"
        value_template: >-
          {% set fan_speed = states('sensor.fan_speed') %}
          {% set speed_percentages = {'Off': 0, 'Speed 1': 14, 'Speed 2': 29, 'Speed 3': 43, 'Speed 4': 57, 'Speed 5': 71, 'Speed 6': 86, 'Speed 7': 100} %}
          {% if fan_speed in speed_percentages %}
            {{ speed_percentages[fan_speed] }}
          {% else %}
            Unknown
          {% endif %}

  - platform: template
    sensors:
      fan_osc:
        friendly_name: "風扇擺頭 (顯示)"
        value_template: "{{ 'True' if is_state('binary_sensor.fan_osc', 'on') else 'False' }}"

sensor.fan_speed 加上了擺頭的判斷請依照內容自己去調整相關的瓦數
sensor.fan_speed_percentage 用在 Fan template 裡面的 percentage_template
依照你自己的段數去調整
多設一個 sensor.fan_osc 是因為 Fan template 裡面的 oscillating_template 只能判斷 True False
所以才多加一個 sensor 來將 binary_sensor.fan_osc 輸出的 on off 轉換成 True False
試過直接將 binary_sensor.fan_osc 輸出設為True False
不過可能我功力不夠都是輸出 on off

  1. 設定含狀態的開關
switch:
  - platform: template
    switches:
      fan_power:
        friendly_name: "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.fan_power
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.fan_power

  - platform: template
    switches:
      fan_osc:
        friendly_name: "Fan osc"
        value_template: "{{ is_state('binary_sensor.fan_osc', 'on') }}"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.fan_osc
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.fan_osc

  - 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:

到此前置作業算是完成要弄的東西很多

風扇設定

fan:
  - platform: template
    fans:
      chimei_fan:
        friendly_name: "奇美風扇"
        speed_count: 7
        value_template: "{{ states('switch.fan_power') }}"
        percentage_template: "{{ states('sensor.fan_speed_percentage') }}"
        oscillating_template: "{{ states('sensor.fan_osc') }}"
        turn_on:
          - condition: state
            entity_id: switch.fan_power
            state: 'off'
          - service: switch.turn_on
            entity_id: switch.fan_power
        turn_off:
          - condition: state
            entity_id: switch.fan_power
            state: 'on'
          - service: switch.turn_off
            entity_id: switch.an_power
        set_percentage:
          service_template: >
            {% if percentage <= 15 %}
              switch.turn_on
            {% elif percentage <= 29 %}
              switch.turn_on
            {% elif percentage <= 43 %}
              switch.turn_on
            {% elif percentage <= 58 %}
              switch.turn_on
            {% elif percentage <= 72 %}
              switch.turn_on
            {% elif percentage <= 86 %}
              switch.turn_on
            {% else %}
              switch.turn_on
            {% endif %}
          data_template:
            entity_id: >
              {% if percentage <= 15 %}
                switch.fan_speed_1
              {% elif percentage <= 29 %}
                switch.fan_speed_2
              {% elif percentage <= 43 %}
                switch.fan_speed_3
              {% elif percentage <= 58 %}
                switch.fan_speed_4
              {% elif percentage <= 72 %}
                switch.fan_speed_5
              {% elif percentage <= 86 %}
                switch.fan_speed_6
              {% else %}
                switch.fan_speed_7
              {% endif %}
        set_oscillating:
          - condition: state
            entity_id: switch.chimei_dc_fan_power
            state: 'on'
          - service: script.turn_on
            data:
              entity_id: script.fan_osc

sensor.fan_speed_percentage 如果有調整過請調整 set_percentage 裡面的內容到對應的數值

custom:multiple-entity-row 設定

type: entities
entities:
  - entity: fan.chimei_fan
  - entity: switch.fan_power
    type: custom:multiple-entity-row
    name: 風扇控制
    state_header: 電源
    icon: mdi:fan
    toggle: true
    state_color: true
    entities:
      - entity: script.fan_speed_down
        name: Down
        icon: mdi:fan-minus
        toggle: false
        tap_action:
          action: toggle
      - entity: script.fan_speed_up
        name: UP
        icon: mdi:fan-plus
        toggle: false
        tap_action:
          action: toggle
      - entity: script.fan_timeopen
        name: 開
        icon: mdi:timer
        toggle: false
        tap_action:
          action: toggle
      - entity: script.fan_timeclose
        name: 關
        icon: mdi:timer-off
        toggle: false
        tap_action:
          action: toggle
  - entity: switch.fan_power
    type: custom:multiple-entity-row
    name: 風扇控制
    state_header: 電源
    icon: mdi:fan
    toggle: true
    state_color: true
    entities:
      - entity: binary_sensor.fan_osc
      - entity: switch.chimei_dc_fan_osc
        name: 擺頭
        icon: mdi:sync
        toggle: true
        state_color: true
        tap_action:
          action: toggle
      - entity: script.fan_eco
        name: ECO
        icon: mdi:leaf
        toggle: false
        tap_action:
          action: toggle
      - entity: script.fan_mode
        name: 模式
        icon: mdi:gesture-double-tap
        toggle: false
        tap_action:
          action: toggle
  - entity: switch.qi_mei_feng_shan_cha_zuo
    type: custom:multiple-entity-row
    name: 風扇功耗
    state_header: Power
    styles:
      width: 30px
    toggle: true
    state_color: true
    icon: mdi:power
    entities:
      - entity: sensor.qi_mei_feng_shan_cha_zuo_voltage
        name: 電壓
        styles:
          width: 30px
      - entity: sensor.qi_mei_feng_shan_cha_zuo_current
        name: 電流
        styles:
          width: 30px
      - entity: sensor.qi_mei_feng_shan_cha_zuo_current_consumption
        name: 功耗
        styles:
          width: 30px
      - entity: sensor.qi_mei_feng_shan_cha_zuo_today_s_consumption
        name: 今日
        styles:
          width: 30px
      - entity: sensor.qi_mei_feng_shan_cha_zuo_total_consumption
        name: Total
        styles:
          width: 30px
  - 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: sensor.fan_speed_percentage
        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
show_header_toggle: false
title: 電風扇

風扇功耗裡面的設定請改成自己的
switch.qi_mei_feng_shan_cha_zuo 改成你的插座

以上為本次分享 有問題可在群內提問

2個讚