本帖最后由 MX10085 于 2024-3-1 19:26 编辑
[智能家居]: 普通风扇改装智能控制使用目的:改装家里的老式风扇,接入home-assistant平台,使其支持语音与无人时自动关闭,也可以根据环境温度自动调节风量档位。
1. 使用环境home-assistant node-red esphome
2. 准备材料 esp32 板子
4路继电器
调速电容1只
电阻1只(1~10K都可以)
3. 硬件接线忘记拍图了,借网友的图用 下,电阻是串在手动调速按钮上的,作下拉电阻。如果电机是2线的话改接调速电容之后就好。
3. esp32刷入程序esp32代码,以下x 自己j更改
####################################################
# 基本信息 #
####################################################
esphome:
name: esp32-erlouketing-fan
friendly_name: esp32-erlouketing-fan
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ota:
password: "xxxxxxxxxxxxxxxxxxxxxxx"
####################################################
# 网络信息 #
####################################################
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# manual_ip:
# static_ip: 192.168.2.55
# gateway: 192.168.2.1
# subnet: 255.255.255.0
# dns1: 223.5.5.5
# dns2: 223.6.6.6
# # Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "xxxxxxxxxxxxxxxxx"
password: "xxxxxxxxxxxxxxxx"
captive_portal:
####################################################
# 时间信息 #
####################################################
time:
- platform: sntp
id: sntp_time
timezone: Asia/Shanghai
servers:
- ntp1.aliyun.com
- ntp2.aliyun.com
- ntp3.aliyun.com
####################################################
# MQTT信息 #
####################################################
# mqtt:
# broker: 192.168.2.14
# username: !secret mqtt_username
# password: !secret mqtt_password
####################################################
# 风扇控制 #
####################################################
##################### 按键 #######################
binary_sensor:
- platform: status
name: "esp32"
- platform: gpio
name: "light"
pin: {number: GPIO3, mode: INPUT_PULLUP, inverted: True}
filters: [{delayed_on: 40ms}, {delayed_off: 40ms}]
on_press:
then: [{switch.turn_on: light}]
on_release:
then: [{switch.turn_off: light}]
- platform: gpio
name: "button"
pin: {number: GPIO21, mode: INPUT_PULLUP, inverted: True}
filters: [{delayed_on: 40ms}, {delayed_off: 40ms}]
# on_state: [{switch.toggle: low}]
##################### 开关 #######################
switch:
- platform: restart
name: "restart"
- platform: gpio
name: "light"
pin: 16
# pin: GPIO16
inverted: yes
id: light
- platform: gpio
name: "low"
pin: 17
inverted: yes
id: low
interlock: [mid, hig]
- platform: gpio
name: "medium"
pin: 18
inverted: yes
id: mid
interlock: [low, hig]
- platform: gpio
name: "high"
pin: 19
inverted: yes
id: hig
interlock: [low, mid]
output:
- platform: template
id: custom_fan
type: float
write_action:
- if:
condition:
lambda: return ((state == 0));
then:
# action for off
- switch.turn_off: low
- switch.turn_off: mid
- switch.turn_off: hig
- if:
condition:
lambda: return ((state > 0) && (state < 0.4));
then:
# action for speed 1
- switch.turn_off: mid
- switch.turn_off: hig
- switch.turn_on: low
- if:
condition:
lambda: return ((state > 0.4) && (state < 0.7));
then:
# action for speed 2
- switch.turn_off: low
- switch.turn_off: hig
- switch.turn_on: mid
- if:
condition:
lambda: return ((state > 0.7));
then:
# action for speed 3
- switch.turn_off: low
- switch.turn_off: mid
- switch.turn_on: hig
fan:
- platform: speed
id: gamesroomfan
output: custom_fan
name: "erlouketing-fan"
speed_count: 3
####################################################
# 传感器 #
####################################################
sensor:
#################### 无线信号 #####################
- platform: wifi_signal
name: "WiFi Signal"
update_interval: 60s
#################### 运行时间 #####################
- platform: uptime
name: Uptime Sensor
id: uptime_sensor
update_interval: 60s
4. home-assistant 设置
5. node-red 设置 node-red 代码:
[
{
"id": "6836b7fd08578f56",
"type": "group",
"z": "f6f2187d.f17ca8",
"style": {
"stroke": "#999999",
"stroke-opacity": "1",
"fill": "none",
"fill-opacity": "1",
"label": true,
"label-position": "nw",
"color": "#a4a4a4"
},
"nodes": [
"8b9ff480520b4f2f",
"e910097dd7e68731",
"ed08ea25ada86837",
"b0702dd4384454ad",
"4b8f8e642a80d506",
"a1035eb9cb61e7c3",
"941f7a1fe79447f1",
"5cf03e6da4744350",
"2cc857c4b5c1cbe9",
"1b81787d2234125a",
"e67af704c3d4f5b1",
"3b6736e2623d6d08",
"2f90baa596394ae9",
"4de5d220150f1c29",
"527fbe89e935a9c2",
"9a5f24df6bcfd9d6",
"f5a2869b96fc0710",
"e1a1e6bb9fef52e0"
],
"x": 14,
"y": 19,
"w": 1432,
"h": 282
},
{
"id": "8b9ff480520b4f2f",
"type": "api-call-service",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "风扇低速",
"server": "99c664b867ad2e30",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.esp32_erlouketing_fan_low"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1280,
"y": 100,
"wires": [
[
"f5a2869b96fc0710"
]
]
},
{
"id": "e910097dd7e68731",
"type": "api-call-service",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "风扇中速",
"server": "99c664b867ad2e30",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.esp32_erlouketing_fan_medium"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1280,
"y": 140,
"wires": [
[
"f5a2869b96fc0710"
]
]
},
{
"id": "ed08ea25ada86837",
"type": "api-call-service",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "风扇高速",
"server": "99c664b867ad2e30",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "turn_on",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.esp32_erlouketing_fan_high"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1280,
"y": 180,
"wires": [
[
"f5a2869b96fc0710"
]
]
},
{
"id": "b0702dd4384454ad",
"type": "function",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "次数加一",
"func": "var count = flow.get('count-01')||0;\nif (count > 0) {\n count = count+1;\n flow.set('count-01', count);\n}\n\nmsg.payload = count;\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 880,
"y": 180,
"wires": [
[
"4b8f8e642a80d506"
]
]
},
{
"id": "4b8f8e642a80d506",
"type": "switch",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "次数判断",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "2",
"vt": "str"
},
{
"t": "eq",
"v": "3",
"vt": "str"
},
{
"t": "eq",
"v": "4",
"vt": "str"
},
{
"t": "eq",
"v": "5",
"vt": "str"
},
{
"t": "gt",
"v": "5",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 5,
"x": 1060,
"y": 180,
"wires": [
[
"8b9ff480520b4f2f"
],
[
"e910097dd7e68731"
],
[
"ed08ea25ada86837"
],
[
"5cf03e6da4744350"
],
[
"a1035eb9cb61e7c3"
]
]
},
{
"id": "a1035eb9cb61e7c3",
"type": "link out",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "二楼客厅风扇-01",
"mode": "link",
"links": [
"2f90baa596394ae9"
],
"x": 1235,
"y": 260,
"wires": []
},
{
"id": "941f7a1fe79447f1",
"type": "function",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "设定基数",
"func": "flow.set('count-01', 1);\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 680,
"y": 260,
"wires": [
[
"b0702dd4384454ad"
]
]
},
{
"id": "5cf03e6da4744350",
"type": "api-call-service",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "风扇关闭",
"server": "99c664b867ad2e30",
"version": 5,
"debugenabled": false,
"domain": "fan",
"service": "turn_off",
"areaId": [],
"deviceId": [],
"entityId": [
"fan.esp32_erlouketing_fan"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1280,
"y": 220,
"wires": [
[]
]
},
{
"id": "2cc857c4b5c1cbe9",
"type": "server-state-changed",
"z": "f6f2187d.f17ca8",
"d": true,
"g": "6836b7fd08578f56",
"name": "风扇灯按键",
"server": "99c664b867ad2e30",
"version": 5,
"outputs": 2,
"exposeAsEntityConfig": "",
"entityId": "binary_sensor.esp32_erlouketing_fan_light",
"entityIdType": "exact",
"outputInitially": false,
"stateType": "str",
"ifState": "on",
"ifStateType": "str",
"ifStateOperator": "is",
"outputOnlyOnStateChange": true,
"for": "0",
"forType": "num",
"forUnits": "minutes",
"ignorePrevStateNull": false,
"ignorePrevStateUnknown": false,
"ignorePrevStateUnavailable": false,
"ignoreCurrentStateUnknown": false,
"ignoreCurrentStateUnavailable": false,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "eventData"
},
{
"property": "topic",
"propertyType": "msg",
"value": "",
"valueType": "triggerId"
}
],
"x": 180,
"y": 260,
"wires": [
[
"1b81787d2234125a"
],
[]
]
},
{
"id": "1b81787d2234125a",
"type": "api-call-service",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "风扇灯光",
"server": "99c664b867ad2e30",
"version": 5,
"debugenabled": false,
"domain": "switch",
"service": "toggle",
"areaId": [],
"deviceId": [],
"entityId": [
"switch.esp32_erlouketing_fan_light"
],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 380,
"y": 260,
"wires": [
[]
]
},
{
"id": "e67af704c3d4f5b1",
"type": "server-state-changed",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "风扇按键",
"server": "99c664b867ad2e30",
"version": 5,
"outputs": 2,
"exposeAsEntityConfig": "",
"entityId": "binary_sensor.esp32_erlouketing_fan_button",
"entityIdType": "exact",
"outputInitially": false,
"stateType": "str",
"ifState": "on",
"ifStateType": "str",
"ifStateOperator": "is",
"outputOnlyOnStateChange": true,
"for": "0",
"forType": "num",
"forUnits": "minutes",
"ignorePrevStateNull": false,
"ignorePrevStateUnknown": false,
"ignorePrevStateUnavailable": false,
"ignoreCurrentStateUnknown": false,
"ignoreCurrentStateUnavailable": false,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "eventData"
},
{
"property": "topic",
"propertyType": "msg",
"value": "",
"valueType": "triggerId"
}
],
"x": 680,
"y": 100,
"wires": [
[
"b0702dd4384454ad"
],
[]
]
},
{
"id": "3b6736e2623d6d08",
"type": "comment",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "二楼客厅风扇",
"info": "",
"x": 110,
"y": 60,
"wires": []
},
{
"id": "2f90baa596394ae9",
"type": "link in",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "二楼客厅风扇-01",
"links": [
"a1035eb9cb61e7c3"
],
"x": 545,
"y": 260,
"wires": [
[
"941f7a1fe79447f1"
]
]
},
{
"id": "4de5d220150f1c29",
"type": "inject",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 500,
"y": 180,
"wires": [
[
"527fbe89e935a9c2"
]
]
},
{
"id": "527fbe89e935a9c2",
"type": "function",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "设定基数",
"func": "flow.set('count-01', 4);\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 680,
"y": 180,
"wires": [
[
"b0702dd4384454ad"
]
]
},
{
"id": "9a5f24df6bcfd9d6",
"type": "api-current-state",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "客厅雷达",
"server": "99c664b867ad2e30",
"version": 3,
"outputs": 2,
"halt_if": "无人",
"halt_if_type": "str",
"halt_if_compare": "is",
"entity_id": "sensor.erlouketing_motion_radar",
"state_type": "str",
"blockInputOverrides": false,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "entity"
}
],
"for": "5",
"forType": "num",
"forUnits": "minutes",
"override_topic": false,
"state_location": "payload",
"override_payload": "msg",
"entity_location": "data",
"override_data": "msg",
"x": 1060,
"y": 260,
"wires": [
[
"5cf03e6da4744350"
],
[]
]
},
{
"id": "f5a2869b96fc0710",
"type": "link out",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "二楼客厅风扇-02",
"mode": "link",
"links": [
"e1a1e6bb9fef52e0"
],
"x": 1405,
"y": 140,
"wires": []
},
{
"id": "e1a1e6bb9fef52e0",
"type": "link in",
"z": "f6f2187d.f17ca8",
"g": "6836b7fd08578f56",
"name": "二楼客厅风扇-02",
"links": [
"f5a2869b96fc0710"
],
"x": 925,
"y": 260,
"wires": [
[
"9a5f24df6bcfd9d6"
]
]
},
{
"id": "99c664b867ad2e30",
"type": "server",
"name": "omv-homeassistant",
"version": 5,
"addon": false,
"rejectUnauthorizedCerts": true,
"ha_boolean": "y|yes|true|on|home|open",
"connectionDelay": true,
"cacheJson": true,
"heartbeat": false,
"heartbeatInterval": "30",
"areaSelector": "friendlyName",
"deviceSelector": "friendlyName",
"entitySelector": "friendlyName",
"statusSeparator": "at: ",
"statusYear": "hidden",
"statusMonth": "short",
"statusDay": "numeric",
"statusHourCycle": "h23",
"statusTimeFormat": "h:m",
"enableGlobalContextStore": true
}
]
6. 完成效果
|