1.自动化三要素
(trigger) 触发
(condition) 条件
(action) 行动
第一是触发自动化的规则。 触发描述事件将触发自动化规则。 到家在这种情况下,它是一个人,可以观察到在HA通过观察太阳的状态改变从“not_home”到“回家”。
第二是条件。 条件是可选的,可以限制一个自动化规则只在特定的用例。 将测试一个条件对系统的当前状态。 这包括当前时间、设备、人员和其他东西像太阳。 例如我们只希望当太阳下山时已采取行动。
第三部分是行动触发时,将执行规则和所有条件得到满足。 例如,它可以打开一盏灯,设置恒温器的温度或激活一个场景。
所以下面用实际例子来说明
automation:
- alias: 'Rainy Day' #自动化别名
trigger: #动态触发
- platform: state
entity_id: sensor.precip_intensity
to: 'rain'
condition: #状态触发
- platform: state
entity_id: group.all_devices
state: 'home'
- platform: time
after: '14:00'
before: '23:00'
action: #行动
service: light.turn_on
entity_id: light.couch_lamp
当天气(DarkSky)预报下雨天,下午至晚上有人在家,即打开灯。
请在HA的配置文件中启用该自动化的文件
automation: !include automations.yaml
再附上官方学习例程!
https://home-assistant.io/cookbook/
-------------------------------------------------分割线-------------------------------------------------
2017年8月5日更新
trigger触发器的几种条件
1:事件触发
automation:
trigger:
platform: event
event_type: MY_CUSTOM_EVENT
# optional
event_data:
mood: happy
内置的事件请参考:https://home-assistant.io/docs/configuration/events/
2.HA触发
可以使用HA的启动与停止来触发
automation:
trigger:
platform: homeassistant
# Event can also be 'shutdown'
event: start
3.MQTT消息触发
接收到特定的消息即触发自动化
automation:
trigger:
platform: mqtt
topic: living_room/switch/ac
# Optional
payload: 'on'
4.数值触发
这个就非常常见了whatsapp web,某个传感器低于某个数值即触发
automation:
trigger:
platform: numeric_state
entity_id: sensor.temperature
# Optional
value_template: '{{ state.attributes.battery }}'
# At least one of the following required
above: 17
below: 25
5.状态触发
这个也是常用的触发条件,例如打开开关触发条件
automation:
trigger:
platform: state
entity_id: device_tracker.paulus, device_tracker.anne_therese
# Optional
from: 'not_home'
# Optional
to: 'home'
# If given, will trigger when state has been the to state for X time.
for:
hours: 1
minutes: 10
seconds: 5
6.日落触发
这个条件就顾名思义了
automation:
trigger:
platform: sun
# Possible values: sunset, sunrise
event: sunset
# Optional time offset. This example is 45 minutes.
offset: '-00:45:00'
7.模板触发
automation:
trigger:
platform: template
value_template: "{% if is_state('device_tracker.paulus', 'home') %}true{% endif %}"
8.时间触发
到某时刻触发条件
automation:
trigger:
platform: time
# Matches every hour at 5 minutes past whole
minutes: 5
seconds: 00
automation 2:
trigger:
platform: time
# When 'at' is used, you cannot also match on hour, minute, seconds.
# Military time format.
at: '15:32:00'
automation 3:
trigger:
platform: time
# You can also match on interval. This will match every 5 minutes
minutes: '/5'
seconds: 00
9.定位触发
automation:
trigger:
platform: zone
entity_id: device_tracker.paulus
zone: zone.home
# Event is either enter or leave
event: enter # or "leave"
10.多个触发条件
automation:
trigger:
# first trigger
- platform: time
minutes: 5
seconds: 00
# our second trigger is the sunset
- platform: sun
event: sunset
-------------------------------------------------分割线-------------------------------------------------
2017年8月15日更新
Condition的几种条件
condition是用来完善trigger触发后执行条件,防止误操作whatsapp网页版,此项可有可无。
先说说几种条件的逻辑关系
1): and
相当于‘与’只有多个条件同时满足才能执行action
condition:
condition: and
conditions:
- condition: state
entity_id: 'device_tracker.paulus'
state: 'home'
- condition: numeric_state
entity_id: 'sensor.temperature'
below: '20'
2): or
相当于 '或' 只要满足一个即执行action
condition:
condition: or
conditions:
- condition: state
entity_id: 'device_tracker.paulus'
state: 'home'
- condition: numeric_state
entity_id: 'sensor.temperature'
below: '20'
3): 混合使用‘and’和‘or’
condition:
condition: or
conditions:
- condition: state
entity_id: 'device_tracker.paulus'
state: 'home'
- condition: numeric_state
entity_id: 'sensor.temperature'
below: '20'
以上就是几种条件的逻辑关系
1.数字状态条件
condition:
condition: numeric_state
entity_id: sensor.temperature
above: 17
below: 25
# If your sensor value needs to be adjusted
value_template: {{ float(state.state) + 2 }}
2.状态条件
condition:
condition: state
entity_id: device_tracker.paulus
state: not_home
# optional: trigger only if state was this for last X time.
for:
hours: 1
minutes: 10
seconds: 5
3.太阳升起/落下 条件
condition:
condition: state
entity_id: device_tracker.paulus
state: not_home
# optional: trigger only if state was this for last X time.
for:
hours: 1
minutes: 10
seconds: 5
4.模板条件
condition:
condition: template
value_template: '{{ states.device_tracker.iphone.attributes.battery > 50 }}'
5.时间条件
condition:
condition: time
# At least one of the following is required.
after: '15:00:00'
before: '02:00:00'
weekday:
- mon
- wed
- fri
6.区域条件
condition:
condition: zone
entity_id: device_tracker.paulus
zone: zone.home
例子
condition:
- condition: numeric_state
entity_id: sun.sun
value_template: ''
below: 1
- condition: state
entity_id: light.living_room
state: 'off'
- condition: time
before: '23:00:00'
after: '14:00:00'
- condition: state
entity_id: script.light_turned_off_5min
state: 'off'
-------------------------------------------------分割线-------------------------------------------------
感谢小白分享的一个带注释例子 配合上面就很好理解了
#客厅-感应人体自动开客厅灯
- alias: auto_ketingled_ganying #自动化名称,可以自定义,会在homeassistant的states里面显示出来
initial_state: true ##在你重启HA的时候这个自动化是开启(true)还是关闭(false)
hide_entity: false #隐藏自动化
trigger:
- platform: state #设备状态
entity_id: binary_sensor.sonoff_pir #检测设备动作的设备ID,在homeassistant的states里面可以找到
from: 'off' #状态转换,这段代码意思就是"从(from):关闭(off)"
to: 'on' #状态转换,衔接上句,"到(to):开启(on)
condition: #condition-条件:就是要达到下面这些条件,才会继续执行命令
condition: and #condition:and-一起满足以下这些条件才会动作
conditions: #开始写条件了哦
- condition: numeric_state #第一个条件:设备状态达到以下数值
entity_id: sensor.sonoff_light_sensor_bh1750 #这是我的光线传感器ID
below: 1 #意思就是光线亮度达到1lux以下,意思就是很暗啦
- condition: state #第二个条件:设备状态
entity_id: switch.sonoff_ketingled #这个是我的客厅灯的sonoff ID
state: 'off' #(state)状态:(off)关闭-意思就是如果我客厅灯是关闭的
action: #开始执行动作了哦
- service: switch.turn_on #开启这个设备
entity_id: switch.sonoff_ketingled #这个就是开启这个设备的ID,这个是我的客厅灯的sonoff ID
- service: tts.baidu_say #这个就是语音播报
data_template:
entity_id: media_player.mpd #播放设备的ID,比如蓝牙音箱啊,usb音箱啊,可以在homeassistant的states里面找到
message: >
"欢迎主人回家,已自动为您打开客厅大灯,5分钟后关闭"
cache: false
- delay: #delay:延迟,延迟多少时间后,时间可以是秒(seconds),也可以是分钟(minutes)
minutes: 4 #minutes(分钟):4(额..就是4分钟)
- service: tts.baidu_say #继续语音播报
data_template:
entity_id: media_player.mpd #播放设备的ID,比如蓝牙音箱啊,usb音箱啊,可以在homeassistant的states里面找到
message: >
"主人,客厅大灯将在一分钟后关闭"
cache: false
- delay: #延迟
minutes: 1 #1分钟
- service: switch.turn_off #将开关关闭
entity_id: switch.sonoff_ketingled #需要关闭的设备ID,就是我的客厅灯的sonoff
#天黑自动开启小米网关小夜灯
- alias: If dark turn on the gateway light
hide_entity: True
condition:
condition: and
conditions:
- condition: state
entity_id: sun.sun
state: "below_horizon"
- condition: numeric_state
entity_id: sensor.illumination_286c07888a77
below: 100
trigger:
- platform: time
minutes: '/1'
action:
- service: light.turn_on
entity_id: light.gateway_light_286c07888a77
data:
brightness: 200
color_name: "blue"
总之,善于使用论坛搜索功能whatsapp网页版,以及在论坛里多提问!!!附文件是 别人共享的自动化文件 大家可参考下!
automation.zip
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



