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

 找回密码
 立即注册
查看: 4132|回复: 3

开关灯自动化分享

[复制链接]

1

主题

16

帖子

283

积分

中级会员

Rank: 3Rank: 3

积分
283
金钱
267
HASS币
0
发表于 2020-9-20 11:04:35 | 显示全部楼层 |阅读模式
本帖最后由 thang009 于 2020-9-20 11:12 编辑

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

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


  • 关照感应器gy30/bh1750 用来检测环境光.当光照充足的时候不会触发开灯动作.
  • rcwl-0516 前置感应器
  • sr501 后置感应器
  • 一个esp8266

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

configuration.yaml:
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
automations.yaml
# 增加人数
- 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
      
scripts.yaml
# 增加人数
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秒的锁止时间,所以如果两个人前后脚进入的话很可能会失败,因此这个方案可能只适合单身狗

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






RCWL-0516.jpg
sr501.jpg
bh1750.jpg
回复

使用道具 举报

175

主题

2956

帖子

7555

积分

超级版主

我就是六神

Rank: 8Rank: 8

积分
7555
金钱
4574
HASS币
398

活跃会员教程狂人灌水之王

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

使用道具 举报

1

主题

16

帖子

283

积分

中级会员

Rank: 3Rank: 3

积分
283
金钱
267
HASS币
0
 楼主| 发表于 2020-9-20 21:26:34 | 显示全部楼层
jyz_0501 发表于 2020-9-20 20:49
额,一前一后进入卫生间是要做什么???。。。画面自行脑补。。。
如果是干湿分离的卫生间,我觉得你这个 ...

前后感应不是为了探测人体存在.是为了统计触发时间差,因为ha的last_changed会一直变化.所以用两个计数器保存触发时间而已.就是这种方案的核心
回复

使用道具 举报

4

主题

590

帖子

2020

积分

金牌会员

Rank: 6Rank: 6

积分
2020
金钱
1425
HASS币
20
发表于 2021-4-14 05:40:39 | 显示全部楼层
统计人数。可能只适合单身狗。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-26 06:28 , Processed in 0.075116 second(s), 30 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表