Create sensor for end of school day time

Hi Everyone

i am trying to figure out what would be the best way to create a sensor that tells me what time my kid finishes school today.

his schedule is the same every week but the time varies per day;

does it make sense to use the local calendar or just a simple template with the hardcoded variables?

Thank you

Hi Alex, welcome to the forum!

It all depends on what you would like to achieve.
If you elaborate a little we can help in a better way.

Hi Nick yes sorry posted by accident before i could finish my sentence.

the end goal is to have a dashboard displaying this kind of info and also do morning briefings.

There are many ways. Two that come to mind:

If it is really the same every single day, a schedule helper would be a nice visual way to edit the schedule. You can create schedules in the helper section. The state would be on or off for in school or not, It has an attribute, next_event, to show the next change (so start or end of school).

If you want full control, a local calendar would be a great way, you’d have a calendar to define repeating school appointments. Showing the next event would take a bit more work though.

Both would allow for all kinds of automation fun, the second would be able to account for holidays.

Hi Edwin, thanks

yes i’ve looked at both options, but both seem overkill; ideally i just what a sensor that returns the time
for example if on Mondays he finishes at 2pm
the state of the sensor would be 14:00

edit: Actually Schedule next_event attribute does make a lot of sense. except in the morning before school start i would get the start time instead of end time…

You will need something to set that sensor, which is a calendar or a schedule.
How would you set that time otherwise: every night before?
If the schedule is regular on a weekly basis a calendar/schedule would be the easiest option in the sense: set and forget.

yes i enter the data for each day (recurring every week for 3 months)
Monday: 14:00
tuesday : 16:00
etc…

Actually i found something that works:
I created a local calendar just for his school timings, then i can just use:

{{ state_attr('calendar.school_noah', 'end_time') }}

and to display just the time:

{{ as_datetime(state_attr('calendar.school_noah', 'end_time')).strftime('%H:%M') }}

Actually i was wrong; the problem with

{{ state_attr('calendar.school_noah', 'end_time') }}

is that it returns the end date for the next available event, therfore on a day without a school event it’ll still return the. next end of school time;

So you mean on a Sunday it gives you Mondays schedule?

You could fix that with:

{% if now().weekday() <=4 %}
{{ state_attr('calendar.school_noah', 'end_time') }}
{% endif %}

But what are you going to do with days when the school is closed?

yes thats correct;

i want another route using sensors:

- trigger:
    - platform: homeassistant
      event: start

    - platform: event
      event_type: event_template_reloaded

    - platform: state
      entity_id: calendar.school_noah
  action:
    - service: calendar.get_events
      target:
        entity_id: calendar.school_noah
      data:
        start_date_time: "{{ now().date() }} 00:00:00"
        end_date_time: "{{ now().date() }} 23:59:59"
      response_variable: agenda

    - variables:
        events: "{{ agenda['calendar.school_noah']['events'] }}"
  sensor:
    - name: "Noah School"
      unique_id: noah_school
      icon: mdi:calendar-star
      state: "{{ events | count }}"
      attributes:
        events: >
          {%- if events | count > 0 %}
            {%- for event in events %}
              {%- if (event.end | as_datetime).day == now().day %}
                {{ event.end | as_timestamp | timestamp_custom('%-I:%M %p') }}
              {%- endif %}
            {%- endfor %}.
          {%- endif -%}

- trigger:
    - platform: homeassistant
      event: start

    - platform: event
      event_type: event_template_reloaded

    - platform: state
      entity_id: calendar.school_arthur
  action:
    - service: calendar.get_events
      target:
        entity_id: calendar.school_arthur
      data:
        start_date_time: "{{ now().date() }} 00:00:00"
        end_date_time: "{{ now().date() }} 23:59:59"
      response_variable: agenda

    - variables:
        events: "{{ agenda['calendar.school_arthur']['events'] }}"
  sensor:
    - name: "Arthur School"
      unique_id: arthur_school
      icon: mdi:calendar-star
      state: "{{ events | count }}"
      attributes:
        events: >
          {%- if events | count > 0 %}
            {%- for event in events %}
              {%- if (event.end | as_datetime).day == now().day %}
                {{ event.end | as_timestamp | timestamp_custom('%-I:%M %p') }}
              {%- endif %}
            {%- endfor %}.
          {%- endif -%}

i then use them like this:

          {% set noah_school_day = is_state('sensor.noah_school', '1') %}
          {% set arthur_school_day = is_state('sensor.arthur_school', '1') %}
          {% set noah_school_time = state_attr('sensor.noah_school', 'events') %}
          {% set arthur_school_time = state_attr('sensor.arthur_school', 'events') %}

          {% if noah_school_day and arthur_school_day %}
          Noah finishes school at {{ noah_school_time }} and Arthur at {{ arthur_school_time }}.
          {% elif noah_school_day and not arthur_school_day %}
          Noah finishes school at {{ noah_school_time }} and Arthur doesn't have school.
          {% elif not noah_school_day and arthur_school_day %}
          Arthur finishes school at {{ arthur_school_time }} and Noah doesn't have school.
          {% else %}
          Oh no.. both kids are on holiday.
          {% endif %}

it seems to be working fine, but im not sure if its the more elegant solution