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

标题: 开关灯自动化分享 [打印本页]

作者: thang009    时间: 2020-9-20 11:04
标题: 开关灯自动化分享
本帖最后由 thang009 于 2020-9-20 11:12 编辑

参考了论坛各种大神的自动化开关灯方案之后,发现只有计数器这个方法比较适合做自动化,尤其是【DIY】廉价人体存在解决方案,区域人数统计大神提出的方案实在是目前比较完美的方法.但是他提到的两个方案红外对射需要装修加持.激光需要自己编译.这个对于我这种什么都不会的人来说简直是灾难.不过自动化还是要做的,那就自己想办法呗.所以用了三个传感器实现了类似的功能,目前使用下来还是比较满意.也不需要自己写代码.

那么下面需要三种传感器分别是:



esp8266刷的是espeasy的固件,毕竟大神写好的东西,咱们没能力就伸手拿来用了..接线方式就不叙述了.论坛上面一搜就有.下面直接放出代码参考

  1. configuration.yaml:
复制代码
  1. counter:
  2. # 测试人体计数器
  3.   ce_suo_count:
  4.     name: 厕所人数
  5.     initial: 0
  6.     step: 1
  7.     minimum: 0
  8.     restore: false
  9.   ce_suo_front:
  10.     name: 前感应器
  11.     initial: 0
  12.     step: 1
  13.     minimum: 0
  14.     maximum: 1
  15.   ce_suo_back:
  16.     name: 后感应器
  17.     initial: 0
  18.     step: 1
  19.     minimum: 0
  20.     maximum: 1
复制代码
  1. binary_sensor:
  2.   # 前置感应器
  3.     - platform: mqtt
  4.       name: motion_sensor.ce_suo_front
  5.       device_class: motion
  6.       state_topic: ESP_ce_suo/motion_sensor.front/state
  7.       qos: 0
  8.       payload_on: "1"
  9.       payload_off: "0"
  10.       unique_id: motion_sensor.ce_suo_front
  11.   # 后置感应器
  12.     - platform: mqtt
  13.       name: motion_sensor.ce_suo_back
  14.       device_class: motion
  15.       state_topic: ESP_ce_suo/motion_sensor.back/state
  16.       qos: 0
  17.       payload_on: "1"
  18.       payload_off: "0"
  19.       unique_id: motion_sensor.ce_suo_back
复制代码
  1. sensor:
  2.   # 光照传感器
  3.     - platform: mqtt
  4.       name: ce_suo_bh1750
  5.       state_topic: ESP_ce_suo/sensor.bh1750/lux
  6.       unit_of_measurement: lx
  7.       unique_id: ce_suo_bh1750
复制代码
  1. automations.yaml
复制代码
  1. # 增加人数
  2. - alias: 增加人数
  3.   initial_state: true
  4.   trigger:
  5.     - platform: state
  6.       entity_id: binary_sensor.motion_sensor_ce_suo_front
  7.       to: 'on'
  8.   condition:
  9.     condition: and
  10.     conditions:
  11.       - condition: numeric_state
  12.         entity_id: counter.ce_suo_back
  13.         below: 1
  14.   action:
  15.     - service: counter.increment
  16.       entity_id: counter.ce_suo_front
  17.     - service: script.toggle
  18.       entity_id: script.add_ce_suo
  19.     - delay: 00:00:05
  20.     - service: script.turn_off
  21.       entity_id: script.add_ce_suo
  22.     - service: counter.decrement
  23.       entity_id: counter.ce_suo_front
复制代码
  1. # 减少人数
  2. - alias: 减少人数
  3.   initial_state: true
  4.   trigger:
  5.     - platform: state
  6.       entity_id: binary_sensor.motion_sensor_ce_suo_back
  7.       to: 'on'
  8.   condition:
  9.     condition: and
  10.     conditions:
  11.       - condition: numeric_state
  12.         entity_id: counter.ce_suo_front
  13.         below: 1
  14.   action:
  15.       - service: counter.increment
  16.         entity_id: counter.ce_suo_back
  17.       - service: script.toggle
  18.         entity_id: script.del_ce_suo
  19.       - delay: 00:00:05
  20.       - service: script.turn_off
  21.         entity_id: script.del_ce_suo
  22.       - service: counter.decrement
  23.         entity_id: counter.ce_suo_back
复制代码
  1. # 自动开厕所灯
  2. - alias: 自动开厕所灯
  3.   initial_state: true
  4.   trigger:
  5.     - platform: state
  6.       entity_id: counter.ce_suo_count
  7.       from: '0'
  8.       to: '1'
  9.   condition:
  10.     condition: and
  11.     conditions:
  12.       - condition: numeric_state
  13.         entity_id: sensor.ce_suo_bh1750
  14.         below: 10
  15.       - condition: state
  16.         entity_id: light.ce_suo_deng
  17.         state: 'off'
  18.   action:
  19.     - service: light.turn_on
  20.       entity_id: light.ce_suo_deng
复制代码
  1. # 自动关厕所灯
  2. - alias: 自动关厕所灯
  3.   initial_state: true
  4.   trigger:
  5.     - platform: state
  6.       entity_id: counter.ce_suo_count
  7.       to: '0'
  8.   condition:
  9.     condition: and
  10.     conditions:
  11.       - condition: state
  12.         entity_id: light.ce_suo_deng
  13.         state: 'on'
  14.       - condition: state
  15.         entity_id: binary_sensor.door_window_sensor_158d000522504e
  16.         state: 'on'
  17.   action:
  18.     - service: light.turn_off
  19.       entity_id: light.ce_suo_deng
  20.       
复制代码
  1. scripts.yaml
复制代码
  1. # 增加人数
  2. add_ce_suo:
  3.   alias: 增加人数
  4.   sequence:
  5.   - wait_template: >-
  6.       {% if(states.binary_sensor.motion_sensor_ce_suo_back.state == 'on') %}
  7.         true
  8.       {% endif %}
  9.   - data:
  10.       entity_id: counter.ce_suo_count
  11.     service_template: >-
  12.       {% if( as_timestamp(states.counter.ce_suo_front.last_changed) - as_timestamp(states.binary_sensor.motion_sensor_ce_suo_back.last_changed) | float < 0 ) %}
  13.         counter.increment
  14.       {% endif %}
  15.   - data:
  16.       entity_id: counter.ce_suo_front
  17.     service: counter.decrement
复制代码
  1. # 减少人数
  2. del_ce_suo:
  3.   alias: 减少人数
  4.   sequence:
  5.   - wait_template: >-
  6.       {% if(states.binary_sensor.motion_sensor_ce_suo_front.state == 'on') %}
  7.         true
  8.       {% endif %}
  9.   - data:
  10.       entity_id: counter.ce_suo_count
  11.     service_template: >-
  12.       {% if( as_timestamp(states.counter.ce_suo_back.last_changed) - as_timestamp(states.binary_sensor.motion_sensor_ce_suo_front.last_changed) | float < 0 ) %}
  13.         counter.decrement
  14.       {% endif %}
  15.   - data:
  16.       entity_id: counter.ce_suo_back
  17.     service: counter.decrement
复制代码

逻辑思路是:



这里还有一种情况,当卫生间门关闭的时候为了防止在里面的传感器乱触发,加了一个小米的门窗感应器.当关闭的时候不触发关灯指令.但是目前我使用的情况下来,这种情况基本上没出现过.统计人数还是比较准确.不过这种方案有4秒的锁止时间,所以如果两个人前后脚进入的话很可能会失败,因此这个方案可能只适合单身狗

希望有大神可以告知还有什么传感器可以实现只在卫生间门口才会触发的.比如上面提到的大神用激光模块,但是又不需要自己撸代码的方案.最后祝各位都能实现自己的家庭自动化方案







作者: jyz_0501    时间: 2020-9-20 20:49
额,一前一后进入卫生间是要做什么???。。。画面自行脑补。。。
如果是干湿分离的卫生间,我觉得你这个已经不用改了,重点是装在湿区,干区这个一般一个小米人体就搞定了,毕竟洗脸化妆吹头发什么的都会一直运动的。主要还是蹲坑的时候。
作者: thang009    时间: 2020-9-20 21:26
jyz_0501 发表于 2020-9-20 20:49
额,一前一后进入卫生间是要做什么???。。。画面自行脑补。。。
如果是干湿分离的卫生间,我觉得你这个 ...

前后感应不是为了探测人体存在.是为了统计触发时间差,因为ha的last_changed会一直变化.所以用两个计数器保存触发时间而已.就是这种方案的核心
作者: zsqduke    时间: 2021-4-14 05:40
统计人数。可能只适合单身狗。。




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5