Skip to content

Instantly share code, notes, and snippets.

@willwhui
Created December 6, 2017 02:29
Show Gist options
  • Save willwhui/25bfe2b213c1728bb9e02dbd6a8ca5ab to your computer and use it in GitHub Desktop.
Save willwhui/25bfe2b213c1728bb9e02dbd6a8ca5ab to your computer and use it in GitHub Desktop.
使用模板生成数据
@willwhui
Copy link
Author

willwhui commented Dec 6, 2017

在automation中使用template

专门文档:
https://home-assistant.io/docs/automation/templating/

  action:
    service: notify.notify
    data_template:
      message: >
        Paulus just changed from {{ trigger.from_state.state }}
        to {{ trigger.to_state.state }}

这里 https://home-assistant.io/components/binary_sensor.xiaomi_aqara/#gas
还提到:

  action:
    - service: notify.html5
      data_template:
        title: Gas alarm!
        message: 'Gas with a density of {{ states.binary_sensor.natgas_sensor_158dxxxxxxxxxx.attributes.density }} detected.'

这是如何获取其他属性的方法。

另外某处提到还有个写法:

data_template:
        message: >
          It is {{ states("sensor.time")}} now.

@willwhui
Copy link
Author

willwhui commented Dec 6, 2017

根据automation的tigger数据来调用不同的服务

参见:https://community.home-assistant.io/t/different-actions-depending-on-trigger-data-value/22440/3
比如,根据旋转方向来调整音量

 alias: "TTS on Magic Cube rotate: volume"
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d000103a704
      action_type: rotate
  action:
    - service_template: >
        {% if trigger.event.data.action_value > 0 %}
            media_player.volume_up
        {% else %}
            media_player.volume_down
        {% endif %}
      entity_id: media_player.living_room_home
    - service: tts.google_say
      entity_id: media_player.living_room_home
      data_template:
        message: >
          It is {{ states("sensor.time")}} now.
    - service: timer.cancel # restart timer
      entity_id: timer.timer_on_magic_tube_rotated
    - service: timer.start
      entity_id: timer.timer_on_magic_tube_rotated

@willwhui
Copy link
Author

willwhui commented Dec 6, 2017

时间的格式化:
{{ now().strftime('%-I:%M %P') }}
结果:
10:18 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment