本帖最后由 thang009 于 2020-9-20 11:12 编辑  
 
参考了论坛各种大神的自动化开关灯方案之后,发现只有计数器这个方法比较适合做自动化,尤其是【DIY】廉价人体存在解决方案,区域人数统计大神提出的方案实在是目前比较完美的方法.但是他提到的两个方案红外对射需要装修加持.激光需要自己编译.这个对于我这种什么都不会的人来说简直是灾难.不过自动化还是要做的,那就自己想办法呗.所以用了三个传感器实现了类似的功能,目前使用下来还是比较满意.也不需要自己写代码. 那么下面需要三种传感器分别是:  
- 关照感应器gy30/bh1750 用来检测环境光.当光照充足的时候不会触发开灯动作.
 - rcwl-0516 前置感应器
 - sr501 后置感应器
 - 一个esp8266
 
 
  
esp8266刷的是espeasy的固件,毕竟大神写好的东西,咱们没能力就伸手拿来用了..接线方式就不叙述了.论坛上面一搜就有.下面直接放出代码参考 
counter:
# 测试人体计数器
  ce_suo_count:
    name: 厕所人数
    initial: 0
    step: 1
    minimum: 0
    restore: false
  ce_suo_front:
    name: 前感应器
    initial: 0
    step: 1
    minimum: 0
    maximum: 1
  ce_suo_back:
    name: 后感应器
    initial: 0
    step: 1
    minimum: 0
    maximum: 1 
 
binary_sensor:
  # 前置感应器
    - platform: mqtt
      name: motion_sensor.ce_suo_front
      device_class: motion
      state_topic: ESP_ce_suo/motion_sensor.front/state
      qos: 0
      payload_on: "1"
      payload_off: "0"
      unique_id: motion_sensor.ce_suo_front
  # 后置感应器
    - platform: mqtt
      name: motion_sensor.ce_suo_back
      device_class: motion
      state_topic: ESP_ce_suo/motion_sensor.back/state
      qos: 0
      payload_on: "1"
      payload_off: "0"
      unique_id: motion_sensor.ce_suo_back 
 
sensor:
  # 光照传感器
    - platform: mqtt
      name: ce_suo_bh1750
      state_topic: ESP_ce_suo/sensor.bh1750/lux
      unit_of_measurement: lx
      unique_id: ce_suo_bh1750 
 
# 增加人数
- alias: 增加人数
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_ce_suo_front
      to: 'on'
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: counter.ce_suo_back
        below: 1
  action:
    - service: counter.increment
      entity_id: counter.ce_suo_front
    - service: script.toggle
      entity_id: script.add_ce_suo
    - delay: 00:00:05
    - service: script.turn_off
      entity_id: script.add_ce_suo
    - service: counter.decrement
      entity_id: counter.ce_suo_front 
 
# 减少人数
- alias: 减少人数
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_ce_suo_back
      to: 'on'
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: counter.ce_suo_front
        below: 1
  action:
      - service: counter.increment
        entity_id: counter.ce_suo_back
      - service: script.toggle
        entity_id: script.del_ce_suo
      - delay: 00:00:05
      - service: script.turn_off
        entity_id: script.del_ce_suo
      - service: counter.decrement
        entity_id: counter.ce_suo_back 
 
# 自动开厕所灯
- alias: 自动开厕所灯
  initial_state: true
  trigger:
    - platform: state
      entity_id: counter.ce_suo_count
      from: '0'
      to: '1'
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.ce_suo_bh1750
        below: 10
      - condition: state
        entity_id: light.ce_suo_deng
        state: 'off'
  action:
    - service: light.turn_on
      entity_id: light.ce_suo_deng 
 
# 自动关厕所灯
- alias: 自动关厕所灯
  initial_state: true
  trigger:
    - platform: state
      entity_id: counter.ce_suo_count
      to: '0'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: light.ce_suo_deng
        state: 'on'
      - condition: state
        entity_id: binary_sensor.door_window_sensor_158d000522504e
        state: 'on'
  action:
    - service: light.turn_off
      entity_id: light.ce_suo_deng
       
 
# 增加人数
add_ce_suo:
  alias: 增加人数
  sequence:
  - wait_template: >-
      {% if(states.binary_sensor.motion_sensor_ce_suo_back.state == 'on') %}
        true
      {% endif %}
  - data:
      entity_id: counter.ce_suo_count
    service_template: >-
      {% if( as_timestamp(states.counter.ce_suo_front.last_changed) - as_timestamp(states.binary_sensor.motion_sensor_ce_suo_back.last_changed) | float < 0 ) %}
        counter.increment
      {% endif %}
  - data:
      entity_id: counter.ce_suo_front
    service: counter.decrement 
 
# 减少人数
del_ce_suo:
  alias: 减少人数
  sequence:
  - wait_template: >-
      {% if(states.binary_sensor.motion_sensor_ce_suo_front.state == 'on') %}
        true
      {% endif %}
  - data:
      entity_id: counter.ce_suo_count
    service_template: >-
      {% if( as_timestamp(states.counter.ce_suo_back.last_changed) - as_timestamp(states.binary_sensor.motion_sensor_ce_suo_front.last_changed) | float < 0 ) %}
        counter.decrement
      {% endif %}
  - data:
      entity_id: counter.ce_suo_back
    service: counter.decrement 
 逻辑思路是:  
- 前置感应器触发之后改变前置统计人数保存触发时间.并且等待后置感应器触发,5秒内后置感应器触发的话通过计算前置触发器和后置触发器时间差判断人是否进入卫生间.人数统计+1,并且光照低的时候开灯
 - 前置感应器触发之后如果5秒内不进入卫生间,则关闭检测脚本.等待下一次触发,减少误触发情况
 - 后置感应器触发和前置触发器逻辑一样.
 
 
  
这里还有一种情况,当卫生间门关闭的时候为了防止在里面的传感器乱触发,加了一个小米的门窗感应器.当关闭的时候不触发关灯指令.但是目前我使用的情况下来,这种情况基本上没出现过.统计人数还是比较准确.不过这种方案有4秒的锁止时间,所以如果两个人前后脚进入的话很可能会失败,因此这个方案可能只适合单身狗  希望有大神可以告知还有什么传感器可以实现只在卫生间门口才会触发的.比如上面提到的大神用激光模块,但是又不需要自己撸代码的方案.最后祝各位都能实现自己的家庭自动化方案  
 
 
 
 
 |