Skip to content

Home Assistant Templates

James Callaghan edited this page Nov 4, 2020 · 7 revisions

Resources

System

Count your entities.

{{ states | length }} 

Count your automations.

{{ states.automation | list | count }}

Automations

Time

Convert UNIX timestamp to local time

{{ (as_timestamp(state_attr('calendar.name','start_time')) - 18000) | timestamp_local }}

Get the time an automation was last triggered.

{{ as_timestamp(state_attr('automation.name','last_triggered')) | timestamp_custom('%H:%M') }}

Timestamp.

{{ now().strftime("%H:%M:%S")  }}

Wait template.

  wait_template: "{{ is_state('binary_sensor.window_door_sensor', 'on') }}"
  timeout: '00:30:00'
  continue_on_timeout: 'false'

String

Expanding groups.

    - service: script.say
      data_template:
        message: >
          {% set count = expand('group.occupancy') | count %}
          {% set person = expand('group.occupancy') | map(attribute='name') | join(' and ') %}
          {% if count == 1 %}
          {% set phrases = ["Hello! " ~ person+" is arrived home "] %}
          {% else %}
          {% set phrases = ["Hello. Nice to see you back home my lords"] %}
          {% endif %}
          {{ phrases | random}}

Split strings

{% set string = "WDC_WD40AFRX-68N22N0_WD-W17K02E9YF5: 34" %}
{{ string | split(': ')[-1] }}

Add new line space to message in a template.

action:
    - service: notify.html5_notification
      data:
        target: {{ entity_id }}
        message: >
          The system is Online

          The Alarm is currently {{ states('input_boolean.alarm') }}

          Current Mode is: {{ states('input_select.alarm_mode') }}

Credit: antikotah

Random phrases

          {% set count = expand('group.arriving') | count %}
          {% set person = expand('group.arriving')|map(attribute='name')|join(' and ') %}
          {% if count == 1 %}
          {% set phrases = ["Hello! little fart " ~ person+" is arrived home ",
          "And the house becomes a home. Welcome back " ~ person,
          "Just a quick announcement. " ~ person +" has arrived!",
          "My systems are picking up the presence of additional humans. " ~ person +" has been identified as home.",
          "Take a note "  ~ person +" is home.",
          "Hey there " ~ person + " Welcome Home!",
          "Life is like a song, you’re back where you belong. Welcome home " ~ person,
          "Welcome back home " ~ person] %}
          {% else %}
          {% set phrases = ["Hello. Nice to see you back home my lords",
          "Hello Guys. High five! How was your day?",
          "Oh! finally you are back. I have missed you",
          "And the house becomes a home. Welcome back " ~ person] %}
          {% endif %}
          {{ phrases | random}}
Clone this wiki locally