Templete help

Hi, I hope someone can help with this one I have been using this code to monitor a plug running a washing machine. It’s the code of a manual card which displays an estimated time left.

`Manual Card 4:

square: false
columns: 2
type: grid
cards:
  - type: custom:state-switch
    entity: template
    template: >-
      {% if as_timestamp(states('input_datetime.washing_stopped')) <
      as_timestamp(states('input_datetime.washing_started')) %}
        true
      {% else %}
        false
      {% endif %}
    states:
      'true':
        type: custom:mushroom-template-card
        secondary: >-
          {{
          as_timestamp(states('input_datetime.washing_expected_time_to_stop'))
          | timestamp_custom(' %H:%M:%S ', True) }}
        icon: mdi:clock
        icon_color: blue
        layout: vertical
        fill_container: true
        primary: Expected End Time
  - type: custom:state-switch
    entity: template
    template: >-
      {% if (as_timestamp(states('input_datetime.washing_stopped')) <
      as_timestamp(states('input_datetime.washing_started'))) and
      (states('sensor.washing_machine_expected_time_left') | float(0) >
      0 ) %}
        true
      {% else %}
        false
      {% endif %}
    states:
      'true':
        type: custom:mushroom-template-card
        primary: Expected time left
        secondary: >-
          {{ (states('sensor.washing_machine_expected_time_left') |
          float| timestamp_custom(' %H:%M:%S ', False)) }}
        icon: mdi:clock
        entity: sensor.washing_machine_expected_time_left
        icon_color: blue
        fill_container: true
        layout: vertical`

The code gives lots of errors in the log so I have been trying to resolve them,
With research I have added the default =0 to the as_timestamp function.
So the code now looks like this:

square: false
columns: 2
type: grid
cards:
  - type: custom:state-switch
    entity: template
    template: >-
      {% if as_timestamp(states('input_datetime.washing_stopped'), default=0) <
      as_timestamp(states('input_datetime.washing_started'), default=0) %}
        true
      {% else %}
        false 
      {% endif %}
  states:
      'true':
        type: custom:mushroom-template-card
        secondary: >-
          {{
          as_timestamp(states('input_datetime.washing_expected_time_to_stop'), default=0)
          | timestamp_custom(' %H:%M:%S ', True) }}
        icon: mdi:clock
        icon_color: blue
        layout: vertical
        fill_container: true
        primary: Expected End Time
   - type: custom:state-switch
    entity: template
    template: >-
      {% if (as_timestamp(states('input_datetime.washing_stopped'), default=0) <
      as_timestamp(states('input_datetime.washing_started'), default=0) and
      (states('sensor.washing_machine_expected_time_left') | float(0) >
      0 ) %}
        true
      {% else %}
        false  
      {% endif %}
  states:
      'true':
        type: custom:mushroom-template-card
        primary: Expected time left
        secondary: >-
          {{ (states('sensor.washing_machine_expected_time_left') |
          float| timestamp_custom(' %H:%M:%S ', False)) }}
          icon: mdi:clock
        entity: sensor.washing_machine_expected_time_left
        icon_color: blue
        fill_container: true
        layout: vertical

but still getting an error of : TemplateSyntaxError: unexpected ‘}’, expected ‘)’ which I haven’t been able to resolve. I am sure it must be some thing I am missing.
Any help appreciated

Not sure if this would cause that error, but your last icon: (five lines from the bottom) is indented too far.

You are missing a ) here

This error :

TemplateSyntaxError: unexpected ‘}’, expected ‘)’

Means it’s expecting a ) as it is trying to match pairs. () And {} cannot interleave… it encountered } before encountering the matching )… thus the error is an unexpected } when a ) was expected instead

A suggestion for you is to put each template block in dev-tools template and see them eval. That tool will help flag and isolate issues quickly

Thanks Armedad I will have a look at that.

So am I right in when looking at the top line there is a ) missing at the end ?
like this {% if (as_timestamp(states('input_datetime.washing_stopped'), default=0))

Did u copy the code from elsewhere? Or did u write it yourself? should think through the grouping of the different expressions and what you intend for it to do…

I’m not sure of the purpose of the very first (. Get rid of that?

But I’m guessing the intent was :

(as_timestamp(states('input_datetime.washing_stopped'), default=0) <
      as_timestamp(states('input_datetime.washing_started'), default=0) )

Hi Armedad, re the code no I didn’t write it myself I got it from [Smart Home Junkie]. but it gives lots of errors in the logs. So I have been working through them and try to resolve them myself and learn at the same time. But this one was beating me.

HI A, removing the very first bracket results in the following errors

ValueError: Template error: float got invalid input 'unavailable' when rendering template 'square: false columns: 2 type: grid cards: - type: custom:state-switch entity: template template: >- {% if as_timestamp(states('input_datetime.washing_stopped'), default=0) < as_timestamp(states('input_datetime.washing_started'), default=0) %} true {% else %} false {% endif %} states: 'true': type: custom:mushroom-template-card secondary: >- {{ as_timestamp(states('input_datetime.washing_expected_time_to_stop'), default=0) | timestamp_custom(' %H:%M:%S ', True) }} icon: mdi:clock icon_color: blue layout: vertical fill_container: true primary: Expected End Time - type: custom:state-switch entity: template template: >- {% if as_timestamp(states('input_datetime.washing_stopped'), default=0) < as_timestamp(states('input_datetime.washing_started'), default=0) and (states('sensor.washing_machine_expected_time_left') | float(0) > 0 ) %} true {% else %} false {% endif %} states: 'true': type: custom:mushroom-template-card primary: Expected time left secondary: >- {{ (states('sensor.washing_machine_expected_time_left') | float| timestamp_custom(' %H:%M:%S ', False)) }} icon: mdi:clock entity: sensor.washing_machine_expected_time_left icon_color: blue fill_container: true layout: vertical' but no default was specified

HI A, I have looked again at the orginal code which is here:

    entity: template
    template: >-
      {% if (as_timestamp(states('input_datetime.washing_stopped')) <
      as_timestamp(states('input_datetime.washing_started'))) and
      (states('sensor.washing_machine_expected_time_left') | float(0) >
      0 ) %}
        true
      {% else %}
        false
      {% endif %}

So you were correct about the a missing bracket I have put that back in and now get the following error

TemplateSyntaxError: expected token ')', got '='

Since you are learning how to do this… You should do what I said above. Take each template block and put it in devtools- template

That will point you to the block that is in error

You have default=0 in your as_timestamp

Where did u get that from? Doesn’t look right

Hi Armedad, sorry I should have said that, that was what I was doing and I had got to this error in the code but I could not work out the solution was
Do

where did you get that from and why do you have it there? i don’t usually see that and also not completely sure how it helps you here.

but i guess an important question is to step back and understand if you’re just trying to “get this done” or if you are trying to learn this system, you should be sure to understand what you’re doing and why… don’t trial and error your way to it…

Hi Armedad, sorry the slight delay in coming back to you just had to take phone call.
As I said I was using the code and it was giveing errors in the logs. Which I wanted to see if I could fix myself.
I had started to researched the error messages and found solutions to the various errors I was getting. I worked with with each block, and adding the default =0 took the errors away. The error message changed as I worked through each section of code so I thought I was making progress
Hope this makes sense

can you paste current template that’s giving you and error? just the one template block.

by the way… this may be more expedient if we use chat. see the upper rigth of your screen…

please post the template block that is giving an error still.
please post only the template block that’s giving an error… use the dev-tools → templates to figure out which block is giving the error. then put that here.

  entity: template
    template: >-
      {% if (as_timestamp(states('input_datetime.washing_stopped')), default=0) <
      as_timestamp(states('input_datetime.washing_started'), default=0) and
      (states('sensor.washing_machine_expected_time_left') | float(0) >
      0 ) %}
        true
      {% else %}
        false  
      {% endif %}
  states:
      'true':
        type: custom:mushroom-template-card
        primary: Expected time left
        secondary: >-
          {{ (states('sensor.washing_machine_expected_time_left') |
          float| timestamp_custom(' %H:%M:%S ', False)) }}
        icon: mdi:clock
        entity: sensor.washing_machine_expected_time_left
        icon_color: blue
        fill_container: true
        layout: vertical

this is the error area

     {% if (as_timestamp(states('input_datetime.washing_stopped'), default=0) <
      as_timestamp(states('input_datetime.washing_started'), default=0)) and
      (states('sensor.washing_machine_expected_time_left') | float(0) >

the default=0 was indeed the issue. if you match the (), you closed off the as_timestamp. so default=0 was no longer being passed to as_timestamp. also i think the very first ( was intended to wrap the whole < compare.