Template sensor with multiple conditions

Hi everyone,

I’m creating a template sensor that compares the actual time with 4 input_datetime helpers and changes accordingly.
When I make it simple with just the first condition it works. When I add all the conditions I need the output is always “Pasto 4”, meaning that it always skip to the final {% else %}.
I guess there’s some typo but I can’t find it.
Here’s the new template sensor:

  - name: prossimo_orario_cibo_gatti_new
    state: >
        {% if now().timestamp() < (state_attr('input_datetime.pasto_gatti_1', 'timestamp')) %}
          Pasto 1
        {% elif now().timestamp() > (state_attr('input_datetime.pasto_gatti_1', 'timestamp'))
        and now().timestamp() < (state_attr('input_datetime.pasto_gatti_2', 'timestamp')) %}
          Pasto 2
        {% elif now().timestamp() > (state_attr('input_datetime.pasto_gatti_2', 'timestamp'))
        and now().timestamp() < (state_attr('input_datetime.pasto_gatti_3', 'timestamp')) %}
          Pasto 3
        {% else %}
          Pasto 4
        {% endif %}

Can someone help me?
Thank you!

Post the values of the three input_datetime entities.

Sure! Here they are :slight_smile:

The problem is that the input_datetimes only contain time but you’re comparing them to now() which is time and date. The template should convert each time value to a time and date value (where the date is today).

Try this version in the Template Editor:

Pasto {{ 4 - [ 'input_datetime.pasto_gatti_1', 'input_datetime.pasto_gatti_2', 'input_datetime.pasto_gatti_3' ]
  | map('states')| map('today_at') | select('ge', now()) | list | count }}

EDIT

Correction. Fixed entity_ids.

Without a date, the timestamp of the helper is always set to that time on January 1st, 1970.

Do whatever @123 says… :slight_smile:

1 Like

Thank you for the explanation, now at least I understand the problem. :smiley:
Unfortunateley I didn’t understand what to do with your code, I tried to paste it in the template editor but I get the error:
ValueError: could not convert str to datetime: 'unknown'

Should I put it somewhere inside the code I posted? Sorry, I’m quite a noob, I’ve been messing around with HA for 2 months now and I find that working with time and dates is the more difficult part for me.

You are getting that error because the number helpers are missing the pasto_ in their entitiy id’s… once the spelling is corrected, @123’s template is a direct replacement for the if/then template you had originally.

- name: prossimo_orario_cibo_gatti_new
    state: >
      Pasto {{ 4 - [ 'input_datetime.pasto_gatti_1', 'input_datetime.pasto_gatti_2', 
      'input_datetime.pasto_gatti_3' ] | map('states')  
      | map('today_at') | select('ge', now()) | list | count }}

I forgot to include the “pasto_” in “pasto_gatti”. I have corrected the example’s entity_ids. Try the corrected version.

Thank you guys for your support! I tried it and it works.
My final goal was not to display a string, but the value of the next input_datetime_pasto_gatti_#. So, for example, at 9 am I wanted this template to output 12:30.

When I shared my code I thought it was only a grammar typo and put a string as a result of the conditions, just to see if the template would work.
I’m deeply sorry I wasn’t clear from the beginning, it’s totally my bad. Is there a way to achieve my goal?

  - name: prossimo_orario_cibo_gatti_new
    state: >
      {% set n = 4 - [ 'input_datetime.pasto_gatti_1', 'input_datetime.pasto_gatti_2', 'input_datetime.pasto_gatti_3' ]
        | map('states')| map('today_at') | select('ge', now()) | list | count %}
      {{ states('input_datetime.pasto_gatti_' ~ n) }}

It works as a charm, thank you very much!
One last question, how can I remove seconds?
I tried the following (and any possible variations I could think of) but it doesn’t work:

{{ states('input_datetime.pasto_gatti_' ~ n , 'timestamp') | timestamp_custom('%H:%M', false) }}
....
{{ (states('input_datetime.pasto_gatti_' ~ n) | as_datetime).strftime('%H:%M')  }}

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

If you want to reduce a string like this:

12:35:00

to this:

12:35

then simply slice off the last three characters using [:-3]

- name: prossimo_orario_cibo_gatti_new
    state: >
      {% set n = 4 - [ 'input_datetime.pasto_gatti_1', 'input_datetime.pasto_gatti_2', 'input_datetime.pasto_gatti_3' ]
        | map('states')| map('today_at') | select('ge', now()) | list | count %}
      {{ states('input_datetime.pasto_gatti_' ~ n)[:-3] }}

Thank you guys for your support @123 @Didgeridrew
You were very helpful and this was very instructive for me.

Have a nice day!

1 Like