Phil7989
(philippA)
July 31, 2023, 6:52pm
1
Hi all,
i‘m a beginner at YAML.
i want to calculate the remaining days to next event on a schedule helper. I cant find any solution on the www. Here my code snippets
{% set duedate = now().fromtimestamp(as_timestamp(state_attr("schedule.ventil6", "next_event"))).astimezone %}
{% set currentdate = now().astimezone() %}
{% set days = currentdate - duedate %}
{{days}}
Error: TypeError: ‘datetime.datetime’ object cannot be interpreted as an integer
is there anyone out there who can help me?
Thanks a lot!
For full days you would use
{{ (state_attr('schedule.ventil6', 'next_event') - now()).days }}
For partial days, you will need to describe how you want the information to be shown.
Phil7989
(philippA)
August 1, 2023, 5:59am
3
Hey, thank you for your fast response. @Didgeridrew You’re right, i think i need a other solution.
I want to show the time and string (today, tommorow…) from the next event. So i think the full days are the wrong way?
Here the full code.
{% if state_attr('schedule.ventil6', 'next_event') == None %}
{{ 'no schedule' }}
{% else %}
{% set days = (state_attr('schedule.ventil6', 'next_event') - now()).days %}
{% if days == 3 %}
{{ state_attr('schedule.ventil6', 'next_event').strftime('day after tomorrow %H:%M Uhr') }}
{% elif days == 2 %}
{{ state_attr('schedule.ventil6', 'next_event').strftime('tommorow %H:%M Uhr') }}
{% elif days == 1 %}
{{ state_attr('schedule.ventil6', 'next_event').strftime('today %H:%M Uhr') }}
{% endif %}
{% endif %}
{% set next = state_attr('schedule.ventil6', 'next_event') %}
{% set days = (next.date() - now().date()).days if next != None %}
{% set mapper = {2: 'day after tomorrow', 1: 'tomorrow', 0: 'today'} %}
{% if days in mapper.keys() %}
{{ mapper.get(days) }}{{ next.strftime(' %H:%M Uhr') }}
{% elif next == None %}
{{ 'no schedule' }}
{% endif %}
1 Like
Phil7989
(philippA)
August 2, 2023, 2:51am
5
Thank you, for the clean code! Works perfect!