Adding a Timer to Assist

Home Assistant

With the Alexa timers via Alexa media player being rather flaky, I decided to see if I could work them up in assist. I have only done one at the moment, but it would be easy to extend it to pass in a timer name.

Custom Sentences Code

  SetTimer:
    data: 
      - sentences:
        - "set timer {time_minutes} (minutes | minute)"
        - "set timer {time_seconds} (seconds | second)"
        - "set timer {time_hours} (hour | hours)"
        - "set timer {time_minutes} (minutes | minute) [and] {time_seconds} (seconds | second)"
        - "set timer {time_hours} (hour | hours) [and] {time_minutes} (minutes | minute) [and] {time_seconds} (seconds | second)"
        - "set timer {time_hours} (hour | hours) [and] {time_minutes} (minutes | minute)"
  CancelTimer:
    data: 
      - sentences:
        - "Cancel Timer"
        - "Stop Timer"
  TimerRemaining:
    data:
       - sentences:
         - "How much time is left" 

lists:  
  time_minutes:
    range:
      from: 1
      to: 600
  time_seconds:
    range:
      from: 1
      to: 600
  time_hours:
    range:
      from: 1
      to: 600

Intent

SetTimer:
  speech:
    text: >-
      {%from "messages.jinja" import timer_start_sentence %}
      {{timer_start_sentence(time_hours,time_minutes,time_seconds) }}
  action:
    - service: timer.start
      data:
        duration: >-
          {%from "messages.jinja" import timer_start_seconds %}
          {{timer_start_seconds(time_hours,time_minutes,time_seconds) }}
      target:
        entity_id: timer.timer   
CancelTimer:
  speech:
    text: "Timer cancelled"
  action:
    - service: timer.cancel
      target:
        entity_id: timer.timer   
TimerRemaining:
  speech:
    text: >-
       {% from 'messages.jinja' import timer_remaining  %}
       {{timer_remaining('timer.timer')}} 

Jinja Macros

{%- macro timer_remaining(timer) -%}
{% from 'easy_time.jinja' import easy_time  %}
{% set state = states(timer) %}
{{ 'The timer is idle' if state == 'idle'}}
{%- set  duration = state_attr(timer,'duration') %}
{%- set hours = duration.split(':')[0] | int %}
{%- set mins = duration.split(':')[1] | int %}
{%- set secs = duration.split(':')[2] | int %}
{{ 'There is ' + easy_time(state_attr(timer,'finishes_at')) + ' remaining' if state == 'active'}}
{%- if state == 'active' %}
{{- ' on the '}}{{hour | string + ' hours' if hours > 0}}
{{- ' ' + mins  | string + ' minute' if mins > 0}}
{{- ' ' + secs  | string + ' second' if secs < 3 and secs != 0 }} timer
{% endif %}
{%- endmacro -%}
{#

Take hours minutes and seconds and return string for sentence

{%from "messages.jinja" import timer_start_sentence %}
{{timer_start_sentence(time_hours,time_minutes,time_seconds) }}

#}
{%- macro timer_start_sentence(time_hours,time_minutes,time_seconds)%}
{%- set hours = time_hours | default(0)|int %}
{%- set minutes = time_minutes | default(0)|int %}
{%- set seconds = time_seconds | default(0)|int %}
{%- set time = minutes + hours * 60 %}
{%- if seconds > 0 %}
{%- set time = time * 60 + seconds  %}
{%- endif %}
{{- 'Timer started for ' + time | string + ' minutes' if seconds == 0 }}
{{- 'Timer started for ' + time | string + ' seconds' if seconds > 0 }}
{%- endmacro %}

{#

Take hours minutes and seconds and return number of seconds

{%from "messages.jinja" import timer_start_seconds %}
{{timer_start_seconds(time_hours,time_minutes,time_seconds) }}

#}
{%- macro timer_start_seconds(time_hours,time_minutes,time_seconds)%}
{%- set hours = time_hours | default(0)|int %}
{%- set minutes = time_minutes | default(0)|int %}
{%- set seconds = time_seconds | default(0)|int %}
{%- set time = minutes + hours * 60 %}
{%- set time = time * 60 + seconds  %}
{{time}}
{%- endmacro %}