Hi all,
I was wondering if there was a way to pass computed values from a template trigger to the trigger variables.
As an example, consider the automation for timer announcement for alexa_media (which is what I’m trying to modify):
The trigger currently is as follows:
- id: timer_announcement
alias: Timer Announcement
description: 'Announce the time remaining on a timer at 5 minute intervals, if the timer is for longer than 5 minutes.'
trigger:
- platform: template
value_template: >
{% if not is_state('sensor.kitchen_next_timer', "unavailable") -%}
{%- set sorted_active = state_attr('sensor.kitchen_next_timer', 'sorted_active') | from_json -%}
{%- set duration = (sorted_active[0][1].originalDurationInMillis / 60000)|int %}
{% set remaining = ((as_datetime(states('sensor.kitchen_next_timer')) - states.sensor.time.last_changed|as_local).seconds // 60) -%}
{{ duration > 5 and remaining != duration and remaining != 0 and remaining % 5 == 0 }}
{% endif -%}
action:
- service: notify.alexa_media_kitchen
data:
message: >
You have {{ time_remaining }} {{ "minutes" if time_remaining > 1 else "minute" }} left on your {{ timer_duration }} minute timer.
data:
type: announce
method: all
mode: single
variables:
timer_duration: >
{% if not is_state('sensor.kitchen_next_timer', "unavailable") -%}
{%- set sorted_active = state_attr('sensor.kitchen_next_timer', 'sorted_active') | from_json -%}
{{ (sorted_active[0][1].originalDurationInMillis / 60000)|int }}
{% endif %}
time_remaining: >
{% if not is_state('sensor.kitchen_next_timer', "unavailable")-%}
{{ ((as_datetime(states('sensor.kitchen_next_timer')) - states.sensor.time.last_changed|as_local).seconds // 60) }}
{% endif %}
I’d like to reference the computed values (i.e. sorted_active, duration, etc) elsewhere in the trigger so I don’t need to perform the whole template to get those values again.
Something along the lines of:
<% set duration= ….%>
…
variables:
current_duration: “{{ duration }}”
As an aside, is it possible to do something like this:
variables:
current_duration: (some complex template here)
next_duration: “{{ current_duration + 1 }}”
Thanks…