I am pulling my hair out trying to generalize a script using fields.
Here is the script I am trying to generalize. It has the fields defined but I am not using them since I can’t get them to work correclty.
alias: update_timer
description: this script will update the specified timer with the increment of more time
fields:
timer_id:
description: timer entity id that you want to update
time_increment:
description: time increment entity to add to timer in minutes
sequence:
- service: timer.start
data:
duration: |-
{% if is_state( 'timer.bar_pantry', 'idle' ) -%}
{{ states('input_number.bar_pantry_inc') | int * 60 }}
{%- else -%}
{{ ((state_attr( 'timer.bar_pantry', 'finishes_at' )| as_datetime) - now()).total_seconds() | int + (states('{{ time_increment }}') | int *60) }}
{%- endif -%}
target:
entity_id: "{{ timer_id }}"
mode: single
This version that does not use the fields works fine. However, every time I try to change out the entity id’s with the fields it fails to evaluate the entity id correctly. Example below
alias: update_timer
description: this script will update the specified timer with the increment of more time
fields:
timer_id:
description: timer entity id that you want to update
time_increment:
description: time increment entity to add to timer in minutes
sequence:
- service: timer.start
data:
duration: |-
{% if is_state( '{{ timer_id }}', 'idle' ) -%}
{{ states('input_number.bar_pantry_inc') | int * 60 }}
{%- else -%}
{{ ((state_attr( 'timer.bar_pantry', 'finishes_at' )| as_datetime) - now()).total_seconds() | int + (states('{{ time_increment }}') | int *60) }}
{%- endif -%}
target:
entity_id: "{{ timer_id }}"
mode: single
this fails using the following service call
service: script.update_timer
data:
timer_id: timer.bar_pantry
time_increment: input_number.bar_pantry_inc
Also, what I am trying to use this on several lights and fans. When the light switch is turned on when the timer is not active, it turns on the light and sets the timer to the appropriate time increment. If the light switch is turned on again while it is already on it adds the time increment to the timer, not just reset it to the time increment. That way, if you want the light (or fan) to run longer, you just push the switch several time. I have this setup in node-red but I am trying to move everything to automations and would like to use a script that is called from the automation that handles incrementing the timer rather than putting that code in each automation. If there is a more elegant way of doing this, please let me know. I am considering putting in a feature request to add a timer service to increase the time by X rather than just the duration which overwrites the current time remaining.
I will replace the other entity id’s with the fields once I figure out what I am doing wrong.
thanks