Hi!
How can I do a sensor that gives me days or hours (not both) until the next event in a google calendar? Someone that can help me?
Thanks in advances
Hi!
How can I do a sensor that gives me days or hours (not both) until the next event in a google calendar? Someone that can help me?
Thanks in advances
Hereâs a guide for days.
Thanks foe you answer and of course this works. But itâs not really what I am looking for. I am looking for just a number of days or hours. 1, 2, 3 etc to ba able to use it in conditions etc
Then just change the template to output days an use the template in your condition.
The reason I asked in the forum is because I donât know how to write templates, so for many itâs probably just to change and you describe but itâs not easy when you donât know how to. Thatâs way I asked
But I hear you, searching elsewhere
But if you canât write a template, how are you going to create a condition that uses the value? Youâve provided little to no information other than âI want the number of daysâ. Even if I were to write the condition template for you, youâd still need to manipulate it to your needs (that you havenât stated).
Sorry it wasnât at al my intention to step on your tows, my bad.
But I donât rely follows your logic here, just because I canât write Jinja2 doesnât mean I canât use condition card in LL, write a automation in yaml or GUI. So what I asked for help with was the template sensor in Jinja2 that says how many days itâs until next event in a calendar. With that I would have made a template sensor that got a state like 1 or 2. So what shouldnât I use the sensor for a condition in say LL. Show this markdown card if this sensor is 2
Sorry I didnât give you all the information you needed, but before you go to hard on me i deleted the post not long after I posted it and before anyone answered it because I I got help and solved it in a little different way.
But this doesnât rely help or support anyone and toke another direction so will delete it again.
Thanks for your time anyway @petro
Not going hard on you, just trying to get this information:
There are about 800 ways to do a condition. And when you want to get number of days until a calendar event, you need a template. Meaning you can only use a template condition or a template sensor to get that information out. This is all I was trying to get out of you. No one here is a mind reader. You could have wanted to get the number of days to your calendar event and then add 10 days for all I know.
This sensor here will provide number of days until an event, where 0 is today.
sensor:
- platform: time_date
display_options:
- 'date'
- platform: template
sensors:
event_day:
friendly_name: Event Day
entity_id: sensor.date
unit_of_measurement: days
value_template: >
{% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
{% set event = states('calendar.xxx') | as_timestamp %}
{{ ((event - midnight) // 86400) | int }}
Could anyone help me modify this to be used as a holiday scheduler for an alarm automation. Iâve got it working that it detects an event but I would like it to only detect if the message is âHolidayâ rather than every next event as I may put âHalf Dayâ in this field as well and I donât want those shown
This is my config entry
work_holiday:
friendly_name: "Work Holiday Countdown"
value_template: >
{% set midnight = today_at() %}
{% set event = state_attr('calendar.work_holidays','start_time') | as_datetime | as_local %}
{% set delta = event - midnight %}
{% if delta.days == 0 %}
Today
{% elif delta.days == 1 %}
Tomorrow
{% elif delta.days == 2 %}
Day After Tomorrow
{% else %}
In {{ delta.days }} Days
{% endif %}
Thank you
Itâs not possible. Calendars only show the next event. So your # of days calculation will be incorrect as you cant look beyond the next event.
Aaaah right that makes sense, Iâve been trying for hours and got nothing lol!
If you install the Hass Calendar Add-on you can access future events from Google and CalDav calendars.
template:
- sensor:
- unique_id: work_holiday
name: "Work Holiday Countdown"
state: >
{% set event = state_attr('sensor.work_holidays', 'data')
| selectattr('summary', 'search', 'Holiday')
| map(attribute='startDateISO') | map('as_datetime')
| map('as_local') | first %}
{% set delta = event - today_at() %}
{% if delta.days == 0 %}
Today
{% elif delta.days == 1 %}
Tomorrow
{% elif delta.days == 2 %}
Day After Tomorrow
{% else %}
In {{ delta.days }} Days
{% endif %}
I have a local calendar with entries for several types of wast: GFT, Rest and Paper.
I like to use one calendar for all these types of waste instead of one calender for each so I tried the above solution but I get an error.
My code is:
- platform: template
sensors:
afval_counter:
friendly_name: "Afval Countdown"
value_template: >
{% set midnight = today_at() %}
{% set event = state_attr('calendar.afval', 'data') | selectattr('summary', 'search', 'GFT') | map(attribute='startDateISO') | map('as_datetime') | map('as_local') | first %}
{% set delta = event - midnight %}
{% if delta.days == 0 %}
Vandaag
{% elif delta.days == 1 %}
Morgen
{% elif delta.days == 2 %}
Overmorgen
{% else %}
In {{ delta.days }} Dagen
{% endif %}
In the error log i find:
Error while processing template: Template<template=({% set midnight = today_at() %} {% set event = state_attr(âcalendar.afvalâ, âdataâ) | selectattr(âsummaryâ, âsearchâ, âGFTâ) | map(attribute=âstartDateISOâ) | map(âas_datetimeâ) | map(âas_localâ) | first %} {% set delta = event - midnight %} {% if delta.days == 0 %} Today {% elif delta.days == 1 %} Tomorrow {% elif delta.days == 2 %} Day After Tomorrow {% else %} In {{ delta.days }} Days {% endif %}) renders=2>
Traceback (most recent call last):
File â/usr/src/homeassistant/homeassistant/helpers/template.pyâ, line 568, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File â/usr/src/homeassistant/homeassistant/helpers/template.pyâ, line 2198, in _render_with_context
return template.render(**kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File â/usr/local/lib/python3.11/site-packages/jinja2/environment.pyâ, line 1301, in render
self.environment.handle_exception()
File â/usr/local/lib/python3.11/site-packages/jinja2/environment.pyâ, line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File ââ, line 1, in top-level template code
jinja2.exceptions.UndefinedError: No first item, sequence was empty.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File â/usr/src/homeassistant/homeassistant/helpers/template.pyâ, line 694, in async_render_to_info
render_info._result = self.async_render(
^^^^^^^^^^^^^^^^^^
File â/usr/src/homeassistant/homeassistant/helpers/template.pyâ, line 570, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: No first item, sequence was empty.
All help is appreciated.
Local Calendar has evolved rapidly, you can now get future events using the calendar.list_events
service call. This can even be done in a trigger-based template sensor.
template:
- trigger:
- platform: time
at: "00:00:00"
action:
- service: calendar.list_events
data:
duration:
hours: 144
target:
entity_id:
- calendar.afval
response_variable: agenda
sensor:
- name: "Afval Countdown"
state: >
{% set midnight = today_at() %}
{% set event = agenda.events | selectattr('summary', 'search', 'GFT') | map(attribute='start') | map('as_datetime') | first %}
{% set delta = (event - midnight).days %}
{% if delta == 0 %}
Vandaag
{% elif delta == 1 %}
Morgen
{% elif delta == 2 %}
Overmorgen
{% else %}
In {{ delta }} Dagen
{% endif %}
I tried to make use of this code, but it does not work. Any suggestions?
template:
- trigger:
- platform: time_pattern
minutes: "/10"
action:
- service: calendar.list_events
data:
duration:
hours: 144
target:
entity_id:
- calendar.home_assistant
response_variable: agenda
sensor:
- name: "Afval PMD"
state: >
{% set midnight = today_at() %}
{% set event = agenda.events | selectattr('summary', 'search', 'PMD-zak') | map(attribute='start') | map('as_datetime') | first %}
{% set delta = (event - midnight).days %}
{% if delta == 0 %}
Vandaag
{% elif delta == 1 %}
Morgen
{% elif delta == 2 %}
Overmorgen
{% else %}
Over {{ delta }} dagen
{% endif %}
When I run the list_events service manually i get:
events:
- start: "2024-02-14"
end: "2024-02-15"
summary: Groene container
description: De groene container wordt elke woensdag geleegd.
- start: "2024-02-14"
end: "2024-02-15"
summary: PMD-zak
description: PMD wordt elke woensdag ingezameld.