Holiday sensor 1 week out

I would like to create a holiday sensor 1 week in advance. I’ve came up with this. It works, but will display the last holiday until the next one occurs. It poses an issue when HA reboots or restarts causing the automation to retrigger. When there is no holiday 1 week out, I want it to state none. Is this possible? I have tried a few ways, but does not work.

template

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.holidays_in_japan
        offset: "-168:00:00"
    sensor:
      - name: "Holidaytomorrow G"
        state: |
          {% if trigger.calendar_event.summary == 'Holidays in japan' %}
            {{ trigger.calendar_event.message }}
          {% endif %}

You are not using the jinja if test correctly. It must always have an else statement for the case when the if test is not true.

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.holidays_in_japan
        offset: "-168:00:00"
      - platform: time
        at: '23:59:59'
    sensor:
      - name: "Holidaytomorrow G"
        state: |
          {% if trigger.calendar_event.summary|default('nothing') == 'Holidays in japan' %}
            {{ trigger.calendar_event.message }}
          {% else %}
            No holiday in one week.
          {% endif %}

The added time trigger will reset the sensor to “No holiday” at the end of the day.

The default will prevent errors when there is no event.

1 Like

Hi. Thank you for your aide. This is my final code. Instead of waiting until midnight for testing, I have the time triggered in real time. There is no holiday in the up coming week.

Upon adding the code and a reboot, the state shows unknown.
When I have it trigger in real time with the time trigger, the state changed to unavailable.
When I restart HA, the state shows unknown.

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.holidays_in_japan
        offset: "-168:00:00"
      - platform: time # resets to none
        at: '23:59:59'
    sensor:
      - name: "Holidayinaweek G"
        state: |
          {% if trigger.calendar_event.summary|default('nothing') == 'Holidays in japan' %}
            {{ trigger.calendar_event.message }}
          {% else %}
            none
          {% endif %}

That’s because you changed what I wrote. Use what I wrote and see what happens.

I’ve only changed the no holiday in one week.
I have pasted your code. It still says unavailable when the time is triggered.

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.holidays_in_japan
        offset: "-168:00:00"
      - platform: time
        at: '15:41:00'
    sensor:
      - name: "Holidaytomorrow G"
        state: |
            {% if trigger.calendar_event.summary|default('nothing') == 'Holidays in japan' %}
              {{ trigger.calendar_event.message }}
            {% else %}
              No holiday in one week.
            {% endif %}

These are the attribute for the holiday sensor.

message: Sea Day
all_day: true
start_time: 2023-07-17 00:00:00
end_time: 2023-07-18 00:00:00
location: 
description: Public holiday
offset_reached: false
friendly_name: Holidays in japan

I cannot edited the holiday. This is what it shows when I click on the next holiday.
2023-05-21_15-46-32

I think there will be an error. If the time trigger executed, then there will be no trigger.calendar_event and the template will fail with a “trigger has no attribute” message.

What does your log show?

this is the error:

error:

Logger: homeassistant.helpers.sensor
Source: helpers/template_entity.py:596
First occurred: 1:05:00 PM (6 occurrences)
Last logged: 3:41:00 PM

Error rendering state template for sensor.holidayinaweek_g: UndefinedError: 'dict object' has no attribute 'calendar_event'
Error rendering state template for sensor.holidaytomorrow_g: UndefinedError: 'dict object' has no attribute 'calendar_event'

Try:

{% if 'calendar_event' in trigger and  trigger.calendar_event.summary|default('nothing') == 'Holidays in japan' %}

{% if ‘calendar_event’ in trigger and trigger.calendar_event.summary|default(‘nothing’) == ‘Holidays in japan’ %}

Edited that bit of code does work. It is showing the else statement now.

What happened at “now”? Did you manually trigger the automation?

No, I used the platform: time to triggered the automation.

Ok, I’m not sure what you expect to see at this stage, but I’d expect the else to execute in that case.

Yes, the else was executed. I will have to wait until the next holiday to come around for the if.

Thank you both for the assist. This is my final code.

template:

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.holidays_in_japan
        offset: "-168:00:00"
      - platform: time # resets to none
        at: '23:59:59'
    sensor:
      - name: "Holidayinaweek G"
        state: |
          {% if 'calendar_event' in trigger and  trigger.calendar_event.summary|default('nothing') == 'Holidays in japan' %}
            {{ trigger.calendar_event.message }}
          {% else %}
            none
          {% endif %}