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:
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).
Thank you for the explanation, now at least I understand the problem.
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.
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?
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) }}
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.