Hello,
I woult like to output a sensor but only if the date from the sensor is the same date as today.
I try this code, but I think it would not be work:
{% if state_attr('sensor.ical_adler_gameday_event_0', 'start') == now() %}
***
# <font color="#1C86EE"> Gameday</font> Adler <font color="red">Mannheim</font>
## {{ as_timestamp(states.sensor.ical_adler_gameday_event_0.attributes.start) | timestamp_custom('%H:%M Uhr') }}
## {{ states.sensor.ical_adler_gameday_event_0.attributes.summary }}
{% else %}
{% endif %}
The format from the attribute start from the sensor is: 2021-08-26T20:00:00+02:00
I Think the format from the now() is: 2021-08-24 19:57:46.283403+02:00
I only compare the year-month-day not the time and so on.
What must I change, that I can compare the to dates?
123
(Taras)
August 26, 2021, 4:57am
3
{% if as_datetime(state_attr('sensor.ical_adler_gameday_event_0', 'start')).date() == now().date() %}
Thanks for your answer.
But with this code I get this error:
TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘elif’ or ‘else’ or ‘endif’. The innermost block that needs to be closed is ‘if’.
I have tray this code:
{% if as_datetime(state_attr('sensor.ical_adler_gameday_event_0', 'start')).date() == now().date() %}
{% else %}
{% endif %}
But with this I get this error:
TypeError: argument 1 must be str, not datetime.datetime
Sorry, but now I dont´t know, how to change it right.
123
(Taras)
August 26, 2021, 5:17pm
5
When I posted the example, it wasn’t meant to be used alone but as the first line of an if-else-endif statement.
That means the value of the start attribute is already understood to be a datetime object (conversion from string to datetime is unnecessary). Therefore we need to remove the as_datetime() function.
{% if state_attr('sensor.ical_adler_gameday_event_0', 'start').date() == now().date() %}
{% else %}
{% endif %}
NOTE
This seems to work only if the datetime object contains one’s timezone offset.
You have change your post.
But in your first version you post a code. I Think with this it will wort.
Here is my code:
{% if state_attr('sensor.ical_adler_gameday_event_0', 'start').date() == now().date() %}
***
# <font color="#1C86EE"> Gameday</font> Adler <font color="red">Mannheim</font>
## {{ as_timestamp(states.sensor.ical_adler_gameday_event_0.attributes.start) | timestamp_custom('%H:%M Uhr') }}
## {{ states.sensor.ical_adler_gameday_event_0.attributes.summary }}
{% else %}
{% endif %}
I must wait until tomorrow, because today the if state is true. Today it works, but I wait what is tomorror.
123
(Taras)
August 26, 2021, 5:33pm
7
Yes, I changed it because when I tried what I had originally posted, it failed to work on my system.
Paste this into the Template Editor and tell me what it reports:
{{ state_attr('sensor.ical_adler_gameday_event_0', 'start').date() }}
compoundbow83:
== now().date()
I change my code from above from == now().date() to != now().date() and then they don´t output anything. So I think it works.
123
(Taras)
August 26, 2021, 5:41pm
10
That’s good and exactly what we want.
In my case, if I do this:
{{ state_attr('sensor.laundry_room', 'last_user_time').date() }}
I get an error message:
UndefinedError: 'str object' has no attribute 'date'
even though the value of last_user_time` looks like this:
2021-08-26T15:30:10.127834+00:00
I am not sure why it works for you but not for me (and that’s why I had changed my post). However, what’s important is that it works for you.
EDIT
I think I found a clue that explains why it worked for you. I tried another entity containing a datetime in this format and it works correctly:
2021-08-26T11:30:00.976501-04:00
The sole difference is that it has the offset for my timezone.
123
(Taras)
August 26, 2021, 9:10pm
12
You’re welcome!
Please consider marking my post above with the Solution tag. You marked your own post but it contains a copy of what I originally posted. Thanks.
Now I have with this code a problem:
{% if state_attr('sensor.ical_adler_gameday_event_0', 'start').date() == now().date() %}
***
# <font color="#1C86EE"> Gameday</font> Adler <font color="red">Mannheim</font>
## Spielgebinn: {{ as_timestamp(states.sensor.ical_adler_gameday_event_0.attributes.start) | timestamp_custom('%H:%M Uhr') }}
## {{ states.sensor.ical_adler_gameday_event_0.attributes.summary }} {% else %} {% endif %}
Because the season is over and the sensor.ical_adler_gameday_event_0 is now unavailable and so the output crashed.
How can i catch the unavailable in this code?
123
(Taras)
April 21, 2022, 1:10pm
14
In the first line, add a test to ensure the sensor’s value is not unavailable
.
{% if states('sensor.ical_adler_gameday_event_0') != 'unavailable' and state_attr('sensor.ical_adler_gameday_event_0', 'start').date() == now().date() %}