Error in template

I’m trying to create a sensor template but getting an error…

- platform: template
  sensors:
    sun_left:
      friendly_name: "Sun Left"
      value_template: >-      
        {% if "(as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now())))"  == "00:00") %}
        {{ (as_timestamp(state_attr('sun.sun', 'next_rising')))| timestamp_custom('Sunrise in %-I:%M', 0) }}
        { % else % }
        {{ (as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now()))| timestamp_custom('Sunset in %H:%M', 0) }}
        {% endif %}

This is the error I get… tried multiple things… looked at brackets, quotes, etc…

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value @ data[‘sensors’][‘sun_left’][‘value_template’]. Got ‘{% if “(as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now())))” == “00:00”) %} {{ (as_timestamp(state_attr('sun.sun', 'next_rising')))| timestamp_custom('Sunrise in %-I:%M', 0) }} { % else % } {{ (as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now()))| timestamp_custom('Sunset in %H:%M', 0) }} {% endif %}’. (See ?, line ?).

Trying to find the error but it doesn’t seem to pop right out at me…

You have an unmatched ) somewhere.

1 Like

Red: remove these quotes
Purple: remove these parentheses
Blue: remove these spaces

1 Like
{% if "((as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now()))  == '00:00')" %}
{{ (as_timestamp(state_attr('sun.sun', 'next_rising')))| timestamp_custom('Sunrise in %-I:%M', 0) }}
{ % else % }
{{ (as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now()))| timestamp_custom('Sunset in %H:%M', 0) }}
{% endif %}

Found a couple issues… this is working now.
Thank you!

These quotes turns the condition always true.
As a non-empty string is true.

1 Like

Funny I was just writing about the return of the if statement being wrong… gives me the wrong return. So Thank you because that just fixed it…

{% if ((as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now()))  == '00:00') %}
{{ (as_timestamp(state_attr('sun.sun', 'next_rising')))| timestamp_custom('Sunrise in %-I:%M', 0) }}
{% else %}
{{ (as_timestamp(state_attr('sun.sun', 'next_setting')) - as_timestamp(now()))| timestamp_custom('Sunset in %H:%M', 0) }}
{% endif %}
1 Like

The first line of your template is an if statement that checks if the result of a subtraction is equal to '00:00' which is a string value.

The problem is that the result of the subtraction is a float value (like 43694.13402414322) and it will never be equal to '00:00'. That means that the if statement will always evaluate to false thereby never executing the Sunrise calculation.

That’s interesting… now trying to jam that into the if statement… LOL

Thank you!

Alternative:

{% set setting = state_attr('sun.sun', 'next_setting') | as_datetime | as_local %}
{% set rising = state_attr('sun.sun', 'next_rising') | as_datetime | as_local %}
Sun{{ iif(rising < setting, 'rise', 'set') }} in {{ (iif(rising < setting, rising, setting) - now()).total_seconds() | timestamp_custom('%-I:%M', 0) }}

Copy-paste it into the Template Editor and confirm it produces the desired result. You’ll probably need to wait until after sunset today to confirm it correctly reports tomorrow’s sunrise.

Works like a charm… Thank you… like making things compact and that’s easy to understand… I tend to complicate things… but not being a ‘programmer’ by education I’m learning as I go… can do javascript and php but this is new to me…

Thank you!!!

sunset

End result is working towards this →

1 Like

not trying to take away from the templating exercise, you are aware you can have the frontend do a lot of this for you natively?

check things like:

      - type: attribute
        entity: sun.sun
        attribute: next_rising
        name: Sun next rising
        icon: mdi:weather-sunset-up
        format: time
      - type: attribute
        entity: sun.sun
        attribute: next_setting
        name: Sun next setting
        icon: mdi:weather-sunset-down
        format: relative

might not be what you can use in a markdown card like the template above, but still very nice to use:

and even the more-info shows what you’re after:

1 Like

Yes and I’ll probably never really do anything but keep the templates I’m experimenting with to look back on… it’s all exercises to get to somewhere else :slight_smile:

Thank you!!!

1 Like

You must be using templates on those as well. So why not I’ll convert them directly to show what you’ve done… which is exactly what I want :slight_smile: