本帖最后由 dscao 于 2021-1-1 16:28 编辑
最近研究了一下Esphome,买了2个D80NK,加一个ESP8266。参考以下帖子:
https://bbs.hassbian.com/thread-11065-1-1.html
https://bbs.hassbian.com/thread-11313-1-1.html
动手连接时才发现,帖子中的是ESP32S,针脚不一样。D80NK说明书中明确要求5V电源,还好用万用表找到了esp8266上的5V针脚。
稳定工作时电压5V,电流0.07A。
最初使用 https://bbs.hassbian.com/thread-11749-1-1.html 中的esphome代码:
esphome:
name: 随便起个名吧
platform: ESP32
board: nodemcu-32s
wifi:
ssid: "你的WiFi名"
password: "你的WiFi密码"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esphome 32 Fallback Hotspot"
password: "zrSLkHpn8Ufd"
captive_portal:
# Enable Home Assistant API
api:
password: "自定义一个密码"
ota:
password: "自定义一个密码"
binary_sensor:
- platform: gpio
pin:
number: GPIO22
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_1
- platform: gpio
pin:
number: GPIO18
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_2
复制代码
人数增减原理:
触发:2号传感器变off,条件:时间1< 2 <3
反向同理。其它情况忽略,不做增减。
一开始用ha中的UI自动化来实现,增加以下辅助元素:
自动化代码:
- id: '1609167392619'
alias: zone_ppl_start
description: ''
trigger:
- type: turned_on
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_1
domain: binary_sensor
- type: turned_on
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_2
domain: binary_sensor
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.entity_id == "binary_sensor.throughs_sensor_2"
}}'
- condition: state
entity_id: binary_sensor.throughs_sensor_1
state: 'on'
sequence:
- service: input_number.increment
data: {}
entity_id: input_number.zone_ppl_num
- conditions:
- condition: template
value_template: '{{ trigger.entity_id == "binary_sensor.throughs_sensor_1"
}}'
- condition: state
entity_id: binary_sensor.throughs_sensor_2
state: 'on'
sequence:
- service: input_number.decrement
data: {}
entity_id: input_number.zone_ppl_num
default: []
mode: single
- id: '1609170604556'
alias: zone_ppl_end
description: ''
trigger:
- type: turned_off
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_1
domain: binary_sensor
- type: turned_off
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_2
domain: binary_sensor
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: '{{ trigger.entity_id == "binary_sensor.throughs_sensor_2"
}}'
- condition: state
entity_id: binary_sensor.throughs_sensor_1
state: 'off'
sequence:
- service: input_number.increment
data: {}
entity_id: input_number.zone_ppl_num
- conditions:
- condition: template
value_template: '{{ trigger.entity_id == "binary_sensor.throughs_sensor_1"
}}'
- condition: state
entity_id: binary_sensor.throughs_sensor_2
state: 'off'
sequence:
- service: input_number.decrement
data: {}
entity_id: input_number.zone_ppl_num
default: []
mode: single
- id: '1609173635413'
alias: zone_ppl_time1
description: ''
trigger:
- type: turned_on
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_1
domain: binary_sensor
condition: []
action:
- service: input_datetime.set_datetime
data:
entity_id: input_datetime.zone_ppl_time1
timestamp: '{{ now().timestamp() }}'
entity_id: input_datetime.zone_ppl_time1
mode: single
- id: '1609174891352'
alias: zone_ppl_time2
description: ''
trigger:
- type: turned_off
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_1
domain: binary_sensor
condition: []
action:
- service: input_datetime.set_datetime
data:
entity_id: input_datetime.zone_ppl_time2
timestamp: '{{ now().timestamp() }}'
entity_id: input_datetime.zone_ppl_time2
mode: single
- id: '1609174980524'
alias: zone_ppl_time3
description: ''
trigger:
- type: turned_on
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_2
domain: binary_sensor
condition: []
action:
- service: input_datetime.set_datetime
data:
entity_id: input_datetime.zone_ppl_time3
timestamp: '{{ now().timestamp() }}'
entity_id: input_datetime.zone_ppl_time3
mode: single
- id: '1609175003405'
alias: zone_ppl_time4
description: ''
trigger:
- type: turned_off
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_2
domain: binary_sensor
condition: []
action:
- service: input_datetime.set_datetime
data:
entity_id: input_datetime.zone_ppl_time4
timestamp: '{{ now().timestamp() }}'
entity_id: input_datetime.zone_ppl_time4
mode: single
- id: '1609175810473'
alias: zone_ppl_increment
description: ''
trigger:
- type: turned_off
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_2
domain: binary_sensor
condition:
- condition: template
value_template: '{{ state_attr(''input_datetime.zone_ppl_time3'', ''timestamp'')|float()
< state_attr(''input_datetime.zone_ppl_time2'', ''timestamp'')|float() }}'
- condition: template
value_template: '{{ state_attr(''input_datetime.zone_ppl_time1'', ''timestamp'')|float()
< state_attr(''input_datetime.zone_ppl_time3'', ''timestamp'')|float() }}'
action:
- service: input_number.increment
data: {}
entity_id: input_number.zone_ppl_num
mode: single
- id: '1609176481429'
alias: zone_ppl_decrement
description: ''
trigger:
- type: turned_off
platform: device
device_id: 50bbba6ebbc243238ddea4fea793594c
entity_id: binary_sensor.throughs_sensor_1
domain: binary_sensor
condition:
- condition: template
value_template: '{{ state_attr(''input_datetime.zone_ppl_time3'', ''timestamp'')|float()
< state_attr(''input_datetime.zone_ppl_time1'', ''timestamp'')|float() }}'
- condition: template
value_template: '{{ state_attr(''input_datetime.zone_ppl_time1'', ''timestamp'')|float()
< state_attr(''input_datetime.zone_ppl_time4'', ''timestamp'')|float() }}'
action:
- service: input_number.decrement
data: {}
entity_id: input_number.zone_ppl_num
mode: single
复制代码
计数能用,但不稳定,受网络状态等多种外界因素影响比较大,hass收到传感器的信息时间可能会滞后,严重时就乱了。后面打算在esphome中实现代码:参考:https://esphome.io/components/binary_sensor/
https://esphome.io/components/sensor/
https://esphome.io/components/se ... platform%20template
https://esphome.io/guides/automations.html?highlight=template
https://github.com/esphome/issues/issues/1099
发现这个esp8266的时间.timestamp每128秒才更新一次,难怪所有地时间显示都一样。导致这个按时间顺序的逻辑在esphome中无法实现。
只能试试传感器的on/off组合状态为条件来区分阶段,阶段完成后进行增减。
——————————
esphome本地自动化完成计数增减,效果相对比较好!
esphome:
name: 随便起个名吧
platform: ESP32
board: nodemcu-32s
wifi:
ssid: "你的WiFi名"
password: "你的WiFi密码"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esphome 32 Fallback Hotspot"
password: "zrSLkHpn8Ufd"
captive_portal:
# Enable Home Assistant API
api:
password: "自定义一个密码"
ota:
password: "自定义一个密码"
binary_sensor:
- platform: gpio
pin:
number: GPIO22
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_1
- platform: gpio
pin:
number: GPIO18
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_2
time:
- platform: sntp
id: sntp_time
timezone: Asia/Shanghai
- platform: homeassistant
id: hastime
timezone: Asia/Shanghai
globals:
- id: counter_int
type: int
restore_value: no
initial_value: '0'
- id: counter_increment
type: int
restore_value: no
initial_value: '0'
- id: counter_decrement
type: int
restore_value: no
initial_value: '0'
binary_sensor:
- platform: gpio
pin:
number: D1
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_1
id: throughs_sensor_1
device_class: motion
on_press:
then:
- lambda: |-
if ((id(throughs_sensor_2).state) and id(counter_decrement) == 1) {
id(counter_decrement) = 2;
}
if (id(throughs_sensor_2).state == false) {
id(counter_increment) = 1;
}
on_release:
then:
- lambda: |-
if ((id(throughs_sensor_2).state) and id(counter_increment) == 2) {
id(counter_increment) = 3;
}
if ((id(throughs_sensor_2).state == false) and id(counter_decrement) == 3) {
id(counter_decrement) = 0;
id(counter_int) -= 1;
}
- platform: gpio
pin:
number: D2
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_2
id: throughs_sensor_2
device_class: motion
on_press:
then:
- lambda: |-
if ((id(throughs_sensor_1).state) and id(counter_increment) == 1) {
id(counter_increment) = 2;
}
if (id(throughs_sensor_1).state == false) {
id(counter_decrement) = 1;
}
on_release:
then:
- lambda: |-
if ((id(throughs_sensor_1).state) and id(counter_decrement) == 2) {
id(counter_decrement) = 3;
}
if ((id(throughs_sensor_2).state == false) and id(counter_increment) == 3) {
id(counter_increment) = 0;
id(counter_int) += 1;
}
text_sensor:
- platform: wifi_info
ip_address:
icon: "mdi:ip"
name: Espcounter IP
ssid:
name: Espcounter SSID
icon: "mdi:access-point-network"
bssid:
name: Espcounter BSSID
icon: "mdi:access-point-network"
sensor:
- platform: wifi_signal
name: "WiFi Espcounter"
update_interval: 60s
## 8266运行时间
- platform: uptime
name: Uptime Sensor
- platform: template
name: counternum
lambda: !lambda |-
return id(counter_int);
update_interval: 5s
复制代码
第一时间发上来了,实际使用还需要更多测试。
——————————————
代码再次优化补充,弃用定时上报消息,改为按需上报消息,增加一个重置计数的开关,以便校正数据(20201230)。
效果:
日志:
[21:27:15][C][wifi_info:010]: WifiInfo SSID 'Espcounter SSID'
[21:27:15][C][wifi_info:010]: Icon: 'mdi:access-point-network'
[21:27:15][C][wifi_info:011]: WifiInfo BSSID 'Espcounter BSSID'
[21:27:15][C][wifi_info:011]: Icon: 'mdi:access-point-network'
[21:27:44][D][sensor:092]: 'Counter Number': Sending state 0.00000 with 1 decimals of accuracy
[21:27:48][D][sensor:092]: 'Uptime Sensor': Sending state 300.53400 s with 0 decimals of accuracy
[21:27:50][D][sensor:092]: 'WiFi Espcounter': Sending state -43.00000 dB with 0 decimals of accuracy
[21:28:10][D][api.connection:604]: Client 'Home Assistant 2020.12.1 (192.168.12.1)' connected successfully!
[21:28:10][D][time:040]: Synchronized time: Wed Dec 30 21:28:10 2020
[21:28:44][D][sensor:092]: 'Counter Number': Sending state 0.00000 with 1 decimals of accuracy
[21:28:48][D][sensor:092]: 'Uptime Sensor': Sending state 360.53601 s with 0 decimals of accuracy
[21:28:50][D][sensor:092]: 'WiFi Espcounter': Sending state -44.00000 dB with 0 decimals of accuracy
[21:29:05][D][binary_sensor:036]: 'throughs_sensor_1': Sending state ON
[21:29:05][D][binary_sensor:036]: 'throughs_sensor_1': Sending state OFF
[21:29:11][D][binary_sensor:036]: 'throughs_sensor_1': Sending state ON
[21:29:12][D][binary_sensor:036]: 'throughs_sensor_2': Sending state ON
[21:29:12][D][binary_sensor:036]: 'throughs_sensor_1': Sending state OFF
[21:29:12][D][binary_sensor:036]: 'throughs_sensor_2': Sending state OFF
[21:29:12][D][sensor:092]: 'Counter Number': Sending state 1.00000 with 1 decimals of accuracy
[21:29:12][D][binary_sensor:036]: 'Motion Sensor': Sending state ON
[21:29:28][D][binary_sensor:036]: 'throughs_sensor_1': Sending state ON
[21:29:28][D][binary_sensor:036]: 'throughs_sensor_2': Sending state ON
[21:29:28][D][binary_sensor:036]: 'throughs_sensor_1': Sending state OFF
[21:29:28][D][binary_sensor:036]: 'throughs_sensor_2': Sending state OFF
[21:29:28][D][sensor:092]: 'Counter Number': Sending state 2.00000 with 1 decimals of accuracy
[21:29:44][D][sensor:092]: 'Counter Number': Sending state 2.00000 with 1 decimals of accuracy
[21:29:45][D][binary_sensor:036]: 'throughs_sensor_2': Sending state ON
[21:29:45][D][binary_sensor:036]: 'throughs_sensor_1': Sending state ON
[21:29:46][D][binary_sensor:036]: 'throughs_sensor_2': Sending state OFF
[21:29:46][D][binary_sensor:036]: 'throughs_sensor_1': Sending state OFF
[21:29:46][D][sensor:092]: 'Counter Number': Sending state 1.00000 with 1 decimals of accuracy
[21:29:48][D][sensor:092]: 'Uptime Sensor': Sending state 420.53101 s with 0 decimals of accuracy
[21:29:49][D][binary_sensor:036]: 'throughs_sensor_2': Sending state ON
[21:29:49][D][binary_sensor:036]: 'throughs_sensor_1': Sending state ON
[21:29:49][D][binary_sensor:036]: 'throughs_sensor_2': Sending state OFF
[21:29:49][D][binary_sensor:036]: 'throughs_sensor_1': Sending state OFF
[21:29:49][D][sensor:092]: 'Counter Number': Sending state 0.00000 with 1 decimals of accuracy
[21:29:49][D][binary_sensor:036]: 'Motion Sensor': Sending state OFF
[21:29:50][D][sensor:092]: 'WiFi Espcounter': Sending state -49.00000 dB with 0 decimals of accuracy
[21:30:02][D][binary_sensor:036]: 'throughs_sensor_1': Sending state ON
[21:30:02][D][binary_sensor:036]: 'throughs_sensor_2': Sending state ON
[21:30:02][D][binary_sensor:036]: 'throughs_sensor_1': Sending state OFF
[21:30:02][D][binary_sensor:036]: 'throughs_sensor_2': Sending state OFF
[21:30:02][D][sensor:092]: 'Counter Number': Sending state 1.00000 with 1 decimals of accuracy
[21:30:02][D][binary_sensor:036]: 'Motion Sensor': Sending state ON
复制代码
esphome代码:
esphome:
name: espcounter
platform: ESP8266
board: nodemcuv2
wifi:
ssid: "家里的wifi信号名"
password: "wifi的密码"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Espcounter Fallback Hotspot"
password: "热点密码"
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
password: "设置一个密码"
ota:
password: "设置一个密码"
time:
- platform: sntp
id: sntp_time
timezone: Asia/Shanghai
- platform: homeassistant
id: hastime
timezone: Asia/Shanghai
globals:
- id: counter_int
type: int
restore_value: no
initial_value: '0'
- id: counter_increment
type: int
restore_value: no
initial_value: '0'
- id: counter_decrement
type: int
restore_value: no
initial_value: '0'
binary_sensor:
- platform: gpio
pin:
number: D1
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_1
id: throughs_sensor_1
device_class: motion
on_press:
then:
- lambda: |-
if ((id(throughs_sensor_2).state) and id(counter_decrement) == 1) {
id(counter_decrement) = 2;
}
if (id(throughs_sensor_2).state == false) {
id(counter_increment) = 1;
}
on_release:
then:
- lambda: |-
if ((id(throughs_sensor_2).state) and id(counter_increment) == 2) {
id(counter_increment) = 3;
}
if ((id(throughs_sensor_2).state == false) and id(counter_decrement) == 3) {
id(counter_decrement) = 0;
id(counter_int) -= 1;
id(counternum).publish_state(id(counter_int));
}
if (id(counter_int) == 0) {
id(motion_sens).publish_state(false);
}
if (id(counter_int) < 0) {
id(counter_int) = 0;
id(counternum).publish_state(id(counter_int));
}
- platform: gpio
pin:
number: D2
mode: INPUT_PULLUP
inverted: True
name: throughs_sensor_2
id: throughs_sensor_2
device_class: motion
on_press:
then:
- lambda: |-
if ((id(throughs_sensor_1).state) and id(counter_increment) == 1) {
id(counter_increment) = 2;
}
if (id(throughs_sensor_1).state == false) {
id(counter_decrement) = 1;
}
on_release:
then:
- lambda: |-
if ((id(throughs_sensor_1).state) and id(counter_decrement) == 2) {
id(counter_decrement) = 3;
}
if ((id(throughs_sensor_2).state == false) and id(counter_increment) == 3) {
id(counter_increment) = 0;
id(counter_int) += 1;
id(counternum).publish_state(id(counter_int));
}
if (id(counter_int) == 1) {
id(motion_sens).publish_state(true);
}
- platform: template
name: "Motion Sensor"
id: motion_sens
device_class: motion
text_sensor:
- platform: wifi_info
ip_address:
icon: "mdi:ip"
name: Espcounter IP
ssid:
name: Espcounter SSID
icon: "mdi:access-point-network"
bssid:
name: Espcounter BSSID
icon: "mdi:access-point-network"
sensor:
- platform: wifi_signal
name: "WiFi Espcounter"
update_interval: 60s
## 8266运行时间
- platform: uptime
name: Uptime Sensor
- platform: template
name: "Counter Number"
id: counternum
lambda: !lambda |-
return id(counter_int);
update_interval: 60s
switch:
- platform: template
name: "Reset Counter to 0"
lambda: |-
return false;
turn_on_action:
- lambda: |-
id(counter_int) = 0;
id(motion_sens).publish_state(false);
复制代码