首页 智能家居homeassistant正文

教程:HomeAssistant核心--自动化

仁镜 homeassistant 2019-11-28 20:18:57 4828 0 homeassistant智能家居

自动化三要素

第一是触发(trigger) 自动化的规则。 触发描述事件将触发自动化规则。 到家在这种情况下,它是一个人,可以观察到在HA通过观察太阳的状态改变从“not_home”到“回家”。

第二是条件(condition)。 条件是可选的,可以限制一个自动化规则只在特定的用例。 将测试一个条件对系统的当前状态。 这包括当前时间、设备、人员和其他东西像太阳。 例如我们只希望当太阳下山时已采取行动。

第三部分是行动(action)触发时,将执行规则和所有条件得到满足。 例如,它可以打开一盏灯,设置恒温器的温度或激活一个场景。

所以下面用实际例子来说明

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

trigger触发器的几种条件

1.1:事件触发

automation:
  trigger:
    platform: event
    event_type: MY_CUSTOM_EVENT
    # optional
    event_data:
      mood: happy

1.2.HA触发

可以使用HA的启动与停止来触发

automation:
  trigger:
    platform: homeassistant
    # Event can also be 'shutdown'
    event: start

1.3.MQTT消息触发

接收到特定的消息即触发自动化

automation:
  trigger:
    platform: mqtt
    topic: living_room/switch/ac
    # Optional
    payload: 'on'

1.4.数值触发

这个就非常常见了,某个传感器低于某个数值即触发

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

1.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

1.6.日落触发

这个条件就顾名思义了

automation:
  trigger:
    platform: sun
    # Possible values: sunset, sunrise
    event: sunset
    # Optional time offset. This example is 45 minutes.
    offset: '-00:45:00'

1.7.模板触发

automation:
  trigger:
    platform: template
    value_template: "{% if is_state('device_tracker.paulus', 'home') %}true{% endif %}"

1.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

1.9.定位触发

automation:
  trigger:
    platform: zone
    entity_id: device_tracker.paulus
    zone: zone.home
    # Event is either enter or leave
    event: enter  # or "leave"

1.10.多个触发条件

automation:
  trigger:
      # first trigger
    - platform: time
      minutes: 5
      seconds: 00
      # our second trigger is the sunset
    - platform: sun
      event: sunset

Condition的几种条件

condition是用来完善trigger触发后执行条件,防止误操作,此项可有可无。先说说几种条件的逻辑关系

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'

2.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.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

2.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

2.4.模板条件

condition:
  condition: template
  value_template: '{{ states.device_tracker.iphone.attributes.battery > 50 }}'

2.5.时间条件

condition:
  condition: time
  # At least one of the following is required.
  after: '15:00:00'
  before: '02:00:00'
  weekday:
    - mon
    - wed
    - fri

2.6.区域条件

condition:
  condition: zone
  entity_id: device_tracker.paulus
  zone: zone.home

实例

以下是一个带注释例子 配合上面就很好理解了

#客厅-感应人体自动开客厅灯
- 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"


版权声明

1.本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行测试。
2.本站资源仅供学习和交流使用,版权归资源原作者所有,请在下载后24小时之内自觉删除。
3.若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。
4.若内容涉及侵权或违法信息,请联系本站管理员进行下架处理,邮箱ganice520@163.com(本站不支持其他投诉反馈渠道,谢谢合作)

本文链接:http://apod.cc/index.php/post/136.html

发表评论

评论列表(0人评论 , 4828人围观)
☹还没有评论,来说两句吧...