MENU

【教程】HA核心——自动化

November 21, 2018 • Read: 185 • 技术,Home Assistant阅读设置

几个简单的自动化:

################################################风扇自动开########################################################################
- alias: switch.fan.auto_open
  trigger: 
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00015da35e
      to: 'on'
      for:
        minutes: 1
    - platform: numeric_state
      entity_id: sensor.rmpro_temperature
      below: 26
      value_template: '{{ state.state }}'   
  condition:
    - condition: time
      after: '07:00:00'
      before: '00:00:00'
    - condition: state
      entity_id: switch.fengshanchazuo
      state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.fengshanchazuo
    - delay:
        seconds: 5       
    - service: switch.turn_on
      entity_id: switch.openfan
    - delay:
        seconds: 1        
    - service: switch.turn_on
      entity_id: switch.fanspeed     
    - delay:
        seconds: 1        
    - service: switch.turn_on
      entity_id: switch.fanwave
    - delay:
        seconds: 1
    - service: switch.turn_on
      entity_id: switch.fanspeed
    - service: tts.baidu_say
      data_template: 
        entity_id: media_player.vlc_player
        message: "您好,当前温度{{states('sensor.rmpro_temperature')}}%,已为您自动打开风扇"
        cache: false      
#结束,实现感应到客厅有人,然后风扇自动打开,自动调到摇头模式,风速调为三档
##################################################按键风扇自动开######################################################################
- alias: switch.fan.open
  initial_state: true
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_158d00011354eb
        click_type: single  
  condition:
    - condition: time
      after: '07:00:00'
      before: '00:00:00'
    - condition: state
      entity_id: switch.fengshanchazuo
      state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.fengshanchazuo
    - delay:
        seconds: 5       
    - service: switch.turn_on
      entity_id: switch.openfan
    - delay:
        seconds: 1        
    - service: switch.turn_on
      entity_id: switch.fanspeed     
    - delay:
        seconds: 1        
    - service: switch.turn_on
      entity_id: switch.fanwave
    - delay:
        seconds: 1
    - service: switch.turn_on
      entity_id: switch.fanspeed
    - service: tts.baidu_say
      data_template: 
        entity_id: media_player.vlc_player
        message: "您好,当前温度{{states('sensor.rmpro_temperature')}}%,已为您自动打开风扇"
        cache: false 
#################################################风扇自动关#######################################################################        
- alias: switch.fan.auto_close
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00015da35e
      to: 'off'
      for:
        minutes: 3 
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00015e659b
      to: 'off'
      for:
        minutes: 3         
  action:
    - service: switch.turn_off
      entity_id: switch.fanspeed     
    - delay:
        seconds: 2        
    - service: switch.turn_off
      entity_id: switch.fanwave
    - delay:
        seconds: 2  
    - service: switch.turn_off
      entity_id: switch.fengshanchazuo
#################################################按键风扇自动关#######################################################################
- alias: switch.fan.close
  initial_state: true
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_158d00011354eb
        click_type: double
  action:
    - service: switch.turn_off
      entity_id: switch.fanspeed     
    - delay:
        seconds: 2        
    - service: switch.turn_off
      entity_id: switch.fanwave
    - delay:
        seconds: 2  
    - service: switch.turn_off
      entity_id: switch.fengshanchazuo
######################################################自动关闭所有灯##################################################################      
- alias: switch.alllight_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015da35e
    to: 'off'
    for:
      minutes: 3
  condition:
    condition: time
    after: '22:00:00'
    before: '07:00:00'
  action:
    - service: switch.turn_on
      entity_id: switch.restaurant
    - delay:
        seconds: 0.3 
    - service: switch.turn_off
      entity_id: switch.restaurant
    - service: switch.turn_on
      entity_id: switch.corridor
    - delay:
        seconds: 0.3       
    - service: switch.turn_off
      entity_id: switch.corridor
    - service: switch.turn_on
      entity_id: switch.saloon
    - delay:
        seconds: 0.3       
    - service: switch.turn_off
      entity_id: switch.saloon
    - service: switch.turn_on
      entity_id: switch.weishengjian
    - delay:
        seconds: 0.3   
    - service: switch.turn_off
      entity_id: switch.weishengjian
    - service: switch.turn_on
      entity_id: switch.yangtai
    - delay:
        seconds: 0.3 
    - service: switch.turn_off
      entity_id: switch.yangtai
    - service: switch.turn_on
      entity_id: switch.zoulang
    - delay:
        seconds: 0.3      
    - service: switch.turn_off
      entity_id: switch.zoulang
    - service: switch.turn_on
      entity_id: switch.chufang
    - delay:
        seconds: 0.3       
    - service: switch.turn_off
      entity_id: switch.chufang
    - service: switch.turn_on
      entity_id: switch.shenghuoyangtai
    - delay:
        seconds: 0.3     
    - service: switch.turn_off
      entity_id: switch.shenghuoyangtai
    - service: switch.turn_on
      entity_id: switch.zhuweipaiqishan
    - delay:
        seconds: 0.3      
    - service: switch.turn_off
      entity_id: switch.zhuweipaiqishan
    - service: switch.turn_off
      entity_id: switch.air
#################################################推门卫生间自动开#######################################################################
- alias: switch.weishengjian_on
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0001193a04
    from: 'off'
    to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.weishengjian
    - service: switch.turn_on
      entity_id: switch.zhuweipaiqishan
    - service: switch.turn_on
      entity_id: switch.cesuofengshanchazuo
##################################################有人卫生间自动开######################################################################
- alias: switch.weishengjian01_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00014dc24e
    from: 'off'
    to: 'on' 
#  condition:
#    condition: state
#    entity_id: binary_sensor.door_window_sensor_158d0001193a04
#    state: 'off'
  action:
    - service: switch.turn_on
      entity_id: switch.weishengjian
    - service: switch.turn_on
      entity_id: switch.zhuweipaiqishan
    - service: switch.turn_on
      entity_id: switch.cesuofengshanchazuo
#####################################################卫生间白天自动关###################################################################    
- alias: switch.weishengjian01_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00014dc24e
    to: 'off'
    for:
      minutes: 2
  condition:
    condition: time
    after: '00:00:00'
    before: '19:00:00'
  action:
    - service: switch.turn_on
      entity_id: switch.weishengjian
    - delay:
        seconds: 0.3 
    - service: switch.turn_off
      entity_id: switch.weishengjian      
    - service: switch.turn_on
      entity_id: switch.zhuweipaiqishan
    - delay:
        seconds: 0.3       
    - service: switch.turn_off
      entity_id: switch.zhuweipaiqishan
    - service: switch.turn_off
      entity_id: switch.cesuofengshanchazuo
####################################################卫生间晚上自动关####################################################################      
- alias: switch.weishengjian02_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00014dc24e
    to: 'off'
    for:
      minutes: 10
  condition:
    condition: time
    after: '19:00:00'
    before: '00:00:00'
  action:
    - service: switch.turn_on
      entity_id: switch.weishengjian
    - delay:
        seconds: 0.3 
    - service: switch.turn_off
      entity_id: switch.weishengjian      
    - service: switch.turn_on
      entity_id: switch.zhuweipaiqishan
    - delay:
        seconds: 300       
    - service: switch.turn_off
      entity_id: switch.zhuweipaiqishan
    - service: switch.turn_off
      entity_id: switch.cesuofengshanchazuo
#####################################################走廊的灯###################################################################
###################################################电影开关#####################################################################      
- alias: "openmovie trigger"
  initial_state: true
  hide_entity: true
  trigger:
    platform: state
    entity_id: input_boolean.openmovie
    to: 'on'
  action:
    - service: script.turn_on
      entity_id: script.openmovie
    - delay:
        seconds: 0.3
    - service: input_boolean.turn_off
      entity_id: input_boolean.openmovie

- alias: "closemovie trigger"
  initial_state: true
  hide_entity: true
  trigger:
    platform: state
    entity_id: input_boolean.closemovie
    to: 'on'
  action:
    - service: script.turn_on
      entity_id: script.closemovie
    - delay:
        seconds: 0.5
    - service: input_boolean.turn_off
      entity_id: input_boolean.closemovie
##############################################叮咚控制电影开##########################################################################
- alias: dingdong open movie
  trigger:
    platform: state
    entity_id: switch.touyingyi
    from: 'off'
    to: 'on'
  action:       
    - service: input_boolean.turn_on
      entity_id: input_boolean.openmovie
    - service: switch.turn_on
      entity_id: switch.saloon
    - delay:
        seconds: 2      
    - service: switch.turn_off
      entity_id: switch.saloon      
    - service: automation.turn_off
      entity_id: automation.lightcanting01_on
    - service: automation.turn_off
      entity_id: automation.lightcanting02_on
##############################################叮咚控制电影关##########################################################################
#- alias: dingdong close movie
#  trigger:
#    platform: state
#    entity_id: switch.touyingyi
#    from: 'on'
#   to: 'off'
#  action:       
#    - service: automation.turn_on
#      entity_id: automation.lightcanting01_on
#    - service: automation.turn_on
#      entity_id: automation.lightcanting02_on    
#    - service: input_boolean.turn_on
#      entity_id: input_boolean.closemovie      
##############################################无线开关控制电影##########################################################################
- alias: open movie
  initial_state: true
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_158d00011054e1
        click_type: single     #'single'单击, 'double'双击 'hold'长按
  action:   
    - service: switch.turn_on
      entity_id: switch.touyingyi  
    - delay:
        seconds: 10      
    - service: input_boolean.turn_on
      entity_id: input_boolean.openmovie
    - service: switch.turn_on
      entity_id: switch.saloon
    - delay:
        seconds: 2      
    - service: switch.turn_off
      entity_id: switch.saloon      
    - service: automation.turn_off
      entity_id: automation.lightcanting01_on
    - service: automation.turn_off
      entity_id: automation.lightcanting02_on  
##################################################无线开关控制电影######################################################################
- alias: close movie
  initial_state: true
  trigger:
    platform: event
    event_type: click
    event_data:
        entity_id: binary_sensor.switch_158d00011054e1
        click_type: double     #'single'单击, 'double'双击 'hold'长按
  action:
    - service: automation.turn_on
      entity_id: automation.lightcanting01_on
    - service: automation.turn_on
      entity_id: automation.lightcanting02_on    
    - service: input_boolean.turn_on
      entity_id: input_boolean.closemovie
    - delay:
        seconds: 300       
    - service: switch.turn_off
      entity_id: switch.touyingyi        
#################################################台灯自动开#######################################################################
- alias: light.taideng_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015da35e
    from: 'off'
    to: 'on'
  condition:
    condition: time
    after: '21:00:00'
    before: '06:00:00'
  action:
    service: light.turn_on
    entity_id: light.yeelight_white_f0b4290b1f2f
####################################################台灯自动关####################################################################    
- alias: light.taideng_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015da35e
    to: 'off'
    for:
      minutes: 3
  action:
    service: light.turn_off
    entity_id: light.yeelight_white_f0b4290b1f2f 
####################################################光线控制餐厅灯自动开####################################################################    
- alias: light.canting01_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e659b
    from: 'off'
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '18:00:00'
        before: '19:30:00'
      - condition: numeric_state
        entity_id: sensor.illuminance_f0b429cc81c3
        below: 550
        value_template: '{{ state.state }}' 
  action:
    service: switch.turn_on
    entity_id: switch.restaurant
#################################################餐厅灯自动开#######################################################################  
- alias: light.canting02_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e659b
    from: 'off'
    to: 'on'
  condition:
    condition: time
    after: '19:30:00'
    before: '21:00:00'     
  action:
    service: switch.turn_on
    entity_id: switch.restaurant
######################################################餐厅灯自动关##################################################################  
- alias: light.canting_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e659b
    to: 'off'
    for:
      minutes: 3
#  condition:
#    condition: or
#    conditions:
#      - condition: state
#        entity_id: binary_sensor.motion_sensor_158d00015da35e
#        state:  'off'        
#      - condition: state
#        entity_id: switch.saloon
#        state:  'on' 
#      - condition: state
#        entity_id: light.yeelight_white_f0b4290b1f2f
#        state:  'on'         
  action:
    service: switch.turn_off
    entity_id: switch.restaurant     
###########################################书房灯自动开#############################################################################    
- alias: light.shufang_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015a9a58
    to: 'on'
  condition:
    condition: time
    after: '18:00:00'
    before: '07:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.shufang
################################################书房灯自动关########################################################################    
- alias: light.shufang_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015a9a58
    to: 'off'
    for:
      minutes: 5
  action:
    service: switch.turn_off
    entity_id: switch.shufang 
##############################################厨房灯白天自动开##########################################################################    
- alias: light.chufang02_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e65a0
    to: 'on'
  condition:
    condition: time
    after: '06:00:00'
    before: '09:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.chufang
##########################################################厨房灯晚上自动开##############################################################    
- alias: light.chufang01_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e65a0
    to: 'on'
  condition:
    condition: time
    after: '18:00:00'
    before: '07:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.chufang
#######################################################厨房灯自动关#################################################################    
- alias: light.chufang_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e65a0
    to: 'off'
    for:
      minutes: 2
  action:
    service: switch.turn_off
    entity_id: switch.chufang 
####################################################厨房风扇自动开####################################################################    
- alias: switch.chufangfan_on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e65a0
    to: 'on'
    for:
      minutes: 2   
  action:
    service: switch.turn_on
    entity_id: switch.plug_158d00013f9e38 
#####################################################厨房风扇自动关###################################################################    
- alias: switch.chufangfan_off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00015e65a0
    to: 'off'
    for:
      minutes: 2   
  action:
    service: switch.turn_off
    entity_id: switch.plug_158d00013f9e38 
###################################################有危险煤气自动关#####################################################################    
- alias: switch.meiqi_off
  trigger:
    platform: state
    entity_id: binary_sensor.smoke_sensor_158d0001574264
    to: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.meiqi
    - delay:
        seconds: 1
    - service: switch.turn_off
      entity_id: switch.meiqi
######################################################煤气定时自动关##################################################################    
- alias: switch.meiqitime_off
  trigger:
    platform: time
    at: '00:00:00'
  action:
    service: switch.turn_off
    entity_id: switch.meiqi
#####################################################煤气定时自动开###################################################################    
- alias: switch.meiqitime_on
  trigger:
    platform: time
    at: '06:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.meiqi
#####################################################净化器自动开###################################################################
- alias: switch.plug_158d00010ec750_on
  trigger:
    platform: numeric_state
    entity_id: sensor.my_bedroom_co2
    above: 680
    value_template: '{{ state.state }}' 
  action:
    service: switch.turn_on
    entity_id: switch.plug_158d00010ec750
#####################################################净化器自动关###################################################################
- alias: switch.plug_158d00010ec750_off
  trigger:
    platform: numeric_state
    entity_id: sensor.my_bedroom_co2
    below: 600
    value_template: '{{ state.state }}' 
  action:
    service: switch.turn_off
    entity_id: switch.plug_158d00010ec750        
#####################################################空调状态tts#############################################################################################
- alias: '20'
  initial_state: True
  hide_entity: False
  trigger:
   - platform: state
     entity_id: switch.ac
     to: 'on'
  action:
    - service: tts.baidu_say
      data_template:
        entity_id: media_player.mpd
        message: "空调已打开,当前模式为{{states.climate.ac.state|replace('cool','制冷')|replace('heat','制热')|replace('auto','自动')}},
                  空调风速为{{state_attr('climate.ac','fan_mode')|replace('low','低')|replace('mid','中')|replace('high','高')}},
                  空调温度为{{state_attr('climate.ac','temperature') }}°C,室内现在温度为{{state_attr('climate.ac','current_temperature') }}°C,
                  友情提醒:当前电费可用余额为{{state_attr('sensor.balance','balance')}}元,请合理使用!"
        cache: false
    - service: notify.notify
      data:
       title: "空调状态"
       message: "空调已打开,当前模式为{{states.climate.ac.state|replace('cool','制冷')|replace('heat','制热')|replace('auto','自动')}},
                  空调风速为{{state_attr('climate.ac','fan_mode')|replace('low','低')|replace('mid','中')|replace('high','高')}},
                  空调温度为{{state_attr('climate.ac','temperature') }}°C,室内现在温度为{{state_attr('climate.ac','current_temperature') }}°C,
                  友情提醒:当前电费可用余额为{{state_attr('sensor.balance','balance')}}元,请合理使用!"
#######################################################电费低于30元自动关空调##############################################################################################
- alias: '21'
  initial_state: True
  hide_entity: False
  trigger:
   - platform: state
     entity_id: switch.ac
     to: 'on'
  condition:
     - condition: numeric_state
       entity_id: sensor.balance
       below: '30'
  action:
    - service: tts.baidu_say
      data_template:
        entity_id: media_player.mpd
        message: "截止{{state_attr('sensor.balance','time')}},电费余额低于{{states.sensor.balance.state}}元,请及时充值。10秒钟后为你关闭空调!"
        cache: False
    - delay:
       seconds: 10 
    - service: switch.turn_off
      entity_id: switch.dc1_s3
#####################################################HomeAssistant启动tts##########################################################################################
- alias: '22'
  initial_state: True
  hide_entity: True
  trigger:
   - platform: homeassistant
     event: start
  action:
   - service: tts.baidu_say
     data_template:
      entity_id: media_player.mpd
      message: "HomeAssistant启动成功!"
      cache: False
   - service: notify.notify
     data:
       title: "HomeAssistant"
       message: "服务启动成功!"
################################################################电费低余额tts#######################################################################
- alias: '23'
  initial_state: True
  hide_entity: False
  trigger:
   - platform: numeric_state
     entity_id: sensor.balance
     below: '30'
  action:
   - service: tts.baidu_say
     data_template:
      entity_id: media_player.mpd
      message: "截止{{state_attr('sensor.balance','time')}},电费余额低于{{states.sensor.balance.state}}元,请及时充值!"
      cache: False
   - service: notify.notify
     data:
      title: "电费低额提醒"
      message: "截止{{state_attr('sensor.balance','time')}},电费余额低于{{states.sensor.balance.state}}元,请及时充值!"
###################################################################文本输入tts##############################################################################
- alias: '24'
  initial_state: True
  hide_entity: False
  trigger:
   - platform: state
     entity_id: input_text.room
  action:
   - service: input_text.set_value
     data_template:
      entity_id: input_text.room
      value: "{{ states('input_text.room')}}"
   - service: tts.baidu_say
     data_template:
      entity_id: media_player.mpd
      message: "{{states.input_text.room.state}}"
      cache: False