本帖最后由 dgmax 于 2020-3-29 23:41 编辑
不用那么麻烦。 timer其实一开始官方的自动化示例中已经给出来了,只是我一年前没有认真留意,近期做了一个出租屋电动车充电桩又重新翻看了一次。
本身就1个人体感应器!建立一个timer定义5分钟,
1、当灯状态关-感应到人体-开灯-开始倒计时5分钟。
2、当灯状态为开-感应到人体-取消倒计时-重新开始倒计时。
3、当倒计时完成-关灯
在configuration.yaml配置文件中增加
timer:
m5_yt:
duration: '00:05:00'
以下自动化代码:
- alias: auto_ck_on
initial_state: true ##在你重启HA的时候这个自动化是开启(true)还是关闭(false)
trigger:
- platform: state
entity_id: binary_sensor.ck_pir
to: 'on'
- platform: state
entity_id: binary_sensor.ck_menci
to: 'on'
condition:
- condition: state
entity_id: switch.ck_l1
state: 'off'
action:
- service: switch.turn_on
entity_id: switch.ck_l1
- service: timer.start
entity_id: timer.m2_ck
- alias: auto_ck_time
initial_state: true ##在你重启HA的时候这个自动化是开启(true)还是关闭(false)
trigger:
- platform: state
entity_id: binary_sensor.ck_pir
to: 'on'
- platform: state
entity_id: binary_sensor.ck_menci
to: 'on'
condition:
- condition: state
entity_id: switch.ck_l1
state: 'on'
action:
- service: timer.cancel
entity_id: timer.m2_ck
- delay:
seconds: 0.5
- service: timer.start
entity_id: timer.m2_ck
- alias: auto_ck_off
initial_state: true ##在你重启HA的时候这个自动化是开启(true)还是关闭(false)
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.m2_ck
action:
service: switch.turn_off
entity_id: switch.ck_l1
|