Question on using fields in templates

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

You can’t nest templates. This is an example of attempting to put a template inside another template.

# This is invalid.
{% if is_state( '{{ timer_id }}', 'idle' ) -%}

Do it like this:

  - service: timer.start
    data:
      duration: |-
        {% if is_state(timer_id, 'idle') -%}
          {{ states('input_number.bar_pantry_inc') | int(0) * 60 }}
        {%- else -%}
          {{ ((state_attr('timer.bar_pantry', 'finishes_at') | as_datetime) - now()).total_seconds() | int(0) + (states(time_increment) | int(0) * 60) }}
        {%- endif -%}

Thanks, that was it. I was beating my head against the desk for 2 days with this. I did not realize the “fields” would be replaced by their value within the template without repeating ‘{{ }}’ syntax. That fixed the issue. For anyone who may want to see the working version:

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(time_increment) | int * 60 }}
        {%- else -%}
          {{ ((state_attr( timer_id, 'finishes_at' )| as_datetime) - now()).total_seconds() | int + (states(time_increment) | int *60) }}
        {%- endif -%}
    target:
      entity_id: '{{ timer_id }}'
mode: single

If you look at the example I posted, it employs int(0) as opposed to merely int. The 0 represents the default value that will be reported in the event that it cannot convert the supplied value to an integer (for example, if entity’s value is unavailable).

If you don’t specify a default and int encounters a value it can’t convert, the script will terminate with an error message.

thanks I did not catch that. I will make that update.