Issue adding start_time attr to template sensor

Hello there,
I am trying to add the start_time for this sensor, but it comes up blank.

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.appointments
        offset: "-24:00:00"
      - platform: time
        at: '23:59:58' # resets to none
    sensor:
      - name: "Appointmenttomorrow"
        state: |
          {% if 'calendar_event' in trigger and  trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
            {{ trigger.calendar_event.summary }}
          {% else %} none {% endif %}
        attributes:
          start_time: "{{ trigger.calendar_event.start_time }}" # <-- not working.
          filter_key: "tomorrow"

I also tried adding an if statement, but that does not work as well.

{% if 'calendar_event' in trigger and  trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
  {{ trigger.calendar_event.start_time }}
{% else %} t {% endif %}

isn’t the trigger var trigger.calendar_event.start ?

It is. See: https://www.home-assistant.io/docs/automation/templating/#calendar

But it is not available for the second time trigger. That only has these variables: https://www.home-assistant.io/docs/automation/templating/#time

So you cant use trigger.calendar_event.start when the time trigger occurs. You will need some other way of getting the event start time (t).

{% if trigger.id == 0 and trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
  {{ trigger.calendar_event.start }}
{% else %} t {% endif %}

Another way of determining which trigger it was: trigger.platform == 'calendar'

2 Likes

need to fix up his “then” clause too:

{% if trigger.id == 0 and trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
  {{ trigger.calendar_event.start }}
{% else %} t {% endif %}
2 Likes

Thanks, done.

Hey. Thanks for the replies guys. I tested the code, but it still not showing up. I am assuming `.id == 01 is the 1st trigger?? Should I add an id=name to the trigger instead??

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.appointments
        offset: "-24:00:00"
      - platform: time
        at: '23:59:58' # resets to none
    sensor:
      - name: "Appointmenttomorrow"
        state: |
          {% if 'calendar_event' in trigger and  trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
            {{ trigger.calendar_event.summary }}
          {% else %} none {% endif %}
        attributes:
          start_time: |
            {% if trigger.id == 0 and trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
              {{ trigger.calendar_event.start }}
            {% else %} {% endif %}
          filter_key: "tomorrow"

Correct. Yes that is another option as is this:

1 Like

Thank you tom. We are almost there. Now I want to only display the appointment time 14:30. It is adding +09:00:00 to the end and I do know know how to interpret that. I think to get the at, I need something similar to this? It is throwing an error due to the +09:00:00 or is there another way to achieve what I want???

start_time: 2024-10-18T14:30:00+09:00
filter_key: tomorrow
friendly_name: Appointmenttomorrow

{% set at = state_attr('sensor.appointmenttomorrow', 'start_time') %}
{% set at = as_timestamp(strptime(at,'%Y-%m-%dT%H:%M:%S')) %}
{{ at | timestamp_custom("%H:%M") }}

Error:

ValueError: Template error: strptime got invalid input '' when rendering template '{% set ct = state_attr('sensor.appointmenttomorrow', 'start_time') %} {% set at = as_timestamp(strptime(at,'%Y-%m-%dT%H:%M:%S')) %} {{ ct | timestamp_custom("%H:%M") }}' but no default was specified

ok. I think I got. This will give me the appointment time in 24hr format.

{{ ( as_timestamp(state_attr('sensor.appointmenttomorrow', 'start_time'))) | timestamp_custom('%H:%M') }}

Working:

  - trigger:
      - platform: calendar
        event: start
        entity_id: calendar.appointments
        offset: "-24:00:00"
      - platform: time
        at: '23:59:58' # resets to none
    sensor:
      - name: "Appointmenttomorrow"
        state: |
          {% if 'calendar_event' in trigger and  trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
            {{ trigger.calendar_event.summary }}
          {% else %} none {% endif %}
        attributes:
          start_time: |
            {% if trigger.platform == 'calendar' and trigger.calendar_event.description|default('none') | lower | regex_search('@doctor|annual') %}
               {{ ( as_timestamp(state_attr('sensor.appointmenttomorrow', 'start_time'))) | timestamp_custom('%H:%M') }}
            {% else %} {% endif %}
          filter_key: "tomorrow"