bluefire1982 发表于 2018-3-1 15:09:14

【更新】场景切换执行自动化

本帖最后由 bluefire1982 于 2018-3-1 22:48 编辑

更新部分:**** Hidden Message *****


----------------------------------------------我是分隔线--------------------------------------------------------------
原帖部分:
先用input_select定义场景。我设置了2个:“在家”和“离开”。初始状态为“在家”。input_select:
scenes:
    name: '场景'
    options:
      - 在家
      - 离开
    initial: '在家'
    icon: mdi:home-assistant

可以通过下拉框手动选择,或根据其他设备联动。我通过group.all_devices的状态自动切换。

因为场景有初始值,重启会恢复默认值。这里我加入了重启HA时的触发,确保重启后仍能保持场景正确。
automation:
# 根据家人是否在家触发场景切换
- alias: 'Select_the_scene'
    initial_state: true
    trigger:
      - platform: homeassistant
      event: start
      - platform: state
      entity_id: group.all_devices
    action:
      - service: input_select.select_option
      data_template:
          entity_id: input_select.scenes
          option: >
            {% if is_state('group.all_devices', 'home') %}
            在家
            {% else %}
            离开
            {% endif %}
然后根据选择的场景执行设备联动。我这里设置了在家关闭/离开打开小米网关警戒(另一个自动化)和小方摄像头(小米智能插座控制)。
- alias: 'Scene_Trigger'
    initial_state: true
    trigger:
      - platform: homeassistant
      event: start
      - platform: state
      entity_id: input_select.scenes
    action:
      - service_template: "automation.turn_{% if trigger.to_state.state == '离开' %}on{% else %}off{% endif %}"
      entity_id: automation.alert_playing
      - service_template: "switch.turn_{% if trigger.to_state.state == '离开' %}on{% else %}off{% endif %}"
      entity_id: switch.xiaomi_smart_wifi_plug
这里用了service_template简化了脚本,不用分2种场景写2个自动化。

分享完毕!






发表于 2018-3-1 15:10:44

干货贴太多,分加不过来啦!

hebmagic 发表于 2018-3-1 17:08:35

全都是知识点,Mark

debitus 发表于 2018-3-1 18:26:03

这个思路真不错,谢谢~

27hh 发表于 2018-3-1 18:41:47

为什么不使用Home Assistant自带的场景功能呢?

bluefire1982 发表于 2018-3-1 21:15:50

27hh 发表于 2018-3-1 18:41
为什么不使用Home Assistant自带的场景功能呢?

惭愧,新手没看到过Scenes,这就去看看文档。

paochu_2007 发表于 2018-3-2 00:26:33

学习一下

wilsoncan 发表于 2018-3-2 10:04:24

学到新知识了,虽然我已用HASS自带的场景组件

mr-one 发表于 2018-3-2 11:30:19

nice, thanks!

saoye 发表于 2018-3-4 11:27:39

学习一下
页: [1] 2 3 4 5 6 7 8 9 10
查看完整版本: 【更新】场景切换执行自动化