Template works in template editor...not in automation

I have the following template:

- service: var.set
      data_template:
        entity_id: var.time_done
        value: "{{ 
                 (as_timestamp(now() + (60 * (states("var.time_left") | int))) | timestamp_custom("%H:%M", true) 
                }}"

the template works correctly in the template editor. The automation is flagged as having errors.

I have tried both data with value_template and data_template with value…neither works.

var.time_left is number of minutes so I multiply by 60 to get seconds.

I’m confused as to why I’m getting an error when I check the configuration.

What component exposes a var.set service?

Assuming this is some custom component and your service, the key ‘value’ and entity_id are correct…

- service: var.set
  data_template:
    entity_id: var.time_done
    value: "{{ (as_timestamp(now() + (60 (states("var.time_left") | int))) | timestamp_custom('%H:%M', true) }}"

I assume that when you did it in the template editor you did not have those outer quotation marks correct? You have a quotation mark issue, you need to escape the inner ones or use single quotes instead of double quotes either inside or outside. Try this:

- service: var.set
  data_template:
    entity_id: var.time_done
    value: "{{ (as_timestamp(now() + (60 * (states('var.time_left') | int))) | timestamp_custom('%H:%M', true) }}"
1 Like

The problem is incorrect usage of quoting in the template for value. Try this:

    - service: var.set
      data_template:
        entity_id: var.time_done
        value: "{{ 
                 (as_timestamp(now() + (60 * (states('var.time_left') | int))) | timestamp_custom('%H:%M', true) 
                }}"
1 Like

all suggestions so far have failed configuration check.

Here is the entire automation:

action:
    - service: var.set
      data_template:
        entity_id: var.time_left

        value: "{{ 
                  ((
                    ( ((states('input_number.smoker_target') |int)  -  (states('sensor.maverick_temp1')| int))
                      /
                      ((trigger.to_state.state | int)  -  (trigger.from_state.state | int))
                    ) *
                     
                     ( as_timestamp(now()) - as_timestamp(states.script.smoker_status.last_updated))
                  )
                  / 60 
                  )
                  | int
            
                 }}"
    
     
    - service: var.set
      data_template:
        entity_id: var.time_done
        value: "{{ 
                 (as_timestamp(now() + (60 * (states('var.time_left') | int))) | timestamp_custom('%H:%M', true) 
                }}"
      
    - service: script.smoker_status  

So when you said “works in the template editor” i assumed the only issue was quotation marks since the difference between the editor and automations.yaml is you wrap the template in quotation marks. So I pasted this into the template editor on my HA:

{{ 
                 (as_timestamp(now() + (60 * (states('var.time_left') | int))) | timestamp_custom('%H:%M', true) 
                }}

And I see this message:

Error rendering template: TemplateSyntaxError: unexpected '}', expected ')'

Configuration check gives a message stating what is wrong, please paste it in next time it will make this process a lot easier.

Fortunately that can be fixed by adding ) to the end. Unfortunately, that’s still not enough to get this template working in the template editor since now I get Unknown error rendering template. Realized that’s because you’re trying to add an integer to now() which isn’t a meaningful operation.

I can’t really fix your template at this point because I don’t really know what you’re trying to do, adding an integer to a date doesn’t make sense. Since you’re multiplying it by 60 I guess you’re probably either trying to add minutes or hours but I don’t know which. You’re going to have to explain what you want this to do for help making a working template.

EDIT: ok I get it now, you missed a parenthesis in the middle right after now() not at the end. Try this:

- service: var.set
  data_template:
    entity_id: var.time_done
    value: "{{ 
                 (as_timestamp(now()) + (60 * (states('var.time_left') | int))) | timestamp_custom('%H:%M', true) 
                }}"
1 Like

Indeed, if you say it works in the template editor we’re going to presume that it actually does and look for a solution elsewhere.

If it’s the entire automation, where’s the trigger? It only shows the automation’s action. The template refers to the Trigger State Object but, without seeing the trigger, we can’t be sure it’s being used correctly in the template.

Anyway, I untangled the first template’s spaghetti and it looks like this:

action:
    - service: var.set
      data_template:
        entity_id: var.time_left
        value: >
          {% set x = states('input_number.smoker_target') | int - states('sensor.maverick_temp1') | int %}
          {% set y = trigger.to_state.state | int - trigger.from_state.state | int %}
          {% set z =  now().timestamp() - states.script.smoker_status.last_updated.timestamp() %}
          {{ (x / y * z / 60) | int }}
alias: calculate_time_remaining
trigger: 
  - entity_id: sensor.maverick_temp1  
    platform: state
action:
    - service: var.set
      data_template:
        entity_id: var.time_left

        value: "{{ 
                  ((
                    ( ((states('input_number.smoker_target') |int)  -  (states('sensor.maverick_temp1')| int))
                      /
                      ((trigger.to_state.state | int)  -  (trigger.from_state.state | int))
                    ) *
                     
                     ( as_timestamp(now()) - as_timestamp(states.script.smoker_status.last_updated))
                  )
                  / 60 
                  )
                  | int
            
                 }}"
    
     
    - service: var.set
      data_template:
        entity_id: var.time_done
        value: "{{ 
                 (as_timestamp(now() + (60 * (states('var.time_left') | int))) | timestamp_custom('%H:%M', true) 
                }}"

        
    - service: script.smoker_status    
         ```

There’s still a missing service at the end but at least now we can see the trigger.

Add the missing part to the following example and let us know what happens.

- alias: calculate_time_remaining
  trigger: 
    - entity_id: sensor.maverick_temp1  
      platform: state
  action:
    - service: var.set
      data_template:
        entity_id: var.time_left
        value: >
          {% set x = states('input_number.smoker_target') | int - states('sensor.maverick_temp1') | int %}
          {% set y = trigger.to_state.state | int - trigger.from_state.state | int %}
          {% set z =  now().timestamp() - states.script.smoker_status.last_updated.timestamp() %}
          {{ (x / y * z / 60) | int }}
    - service: var.set
      data_template:
        entity_id: var.time_done
        value: >
          {{ (now().timestamp() + 60 * states('var.time_left') | int) | timestamp_custom('%H:%M', true) }}
    - service: script.smoker_status 
      # --- etc ---