Schedule helper and next_event attribute

Hi everyone,

The Schedule helper attribute “next_event” gives you an output like this:
2023-01-23T18:30:00+01:00

I’ve created the following template sensor:

  - name: prossimo_orario_cibo_gatti
    state: "{{ state_attr('schedule.orario_cibo_gatti', 'next_event').time() }}"
    icon: mdi:calendar

The output of this sensor is 18:30:00.
I would like to get 18:00 instead.
In short, how can I remove seconds? I’ve googled and read the documentation but I can’t figure this out.

On another note, I noticed that the output of this attribute is the next time there is a change of state.
I.e. next slot is at 18:30:00 for half an hour → up until 18:29 the output is 18:30:00, after that the output is 19:00:00.
I would like to catch only the start of the event. So, in my example, after 18:30 I would like to get the starting time of the next event, instead of the end time of the active event. Is this possible to get in some way?
I hope I explained myself :slight_smile:

Thank you!

One option is to use strftime()

  - name: prossimo_orario_cibo_gatti
    state: >
      {{ state_attr('schedule.orario_cibo_gatti', 'next_event').strftime('%H:%M') }}
    icon: mdi:calendar

It works, thank you so much!
I’m quite new and I need to improve my skills in understanding documentation :slight_smile:

Do you know a solution to my second question by any chance?

As far as I know, there is not a way to accomplish what you asked about in your second question with the schedule helper, because there’s no way to “skip” to the event after next. The closest approximation I can think of would be to have the sensor’s state return something like “active” while the schedule is on, and only give the time of next event when the schedule is off.

I guess you are right, maybe the Schedule helper wasn’t the best choice in the first place, but I couldn’t think of a better solution.
I’ll try and find a solution, otherwise I will implement yours.

Thank you very much!
Andrea

I’m trying a different approach to solve my second question.
Instead of using a scheduler helper I’m using four input_datetime helpers.
Here’s the new template sensor:

  - name: prossimo_orario_cibo_gatti_new
    state: >
        {% if now().timestamp() < (state_attr('input_datetime.pasto_gatti_1', 'timestamp')) %}
          Pasto 1
        {% elif now().timestamp() > (state_attr('input_datetime.pasto_gatti_1', 'timestamp'))
        and now().timestamp() < (state_attr('input_datetime.pasto_gatti_2', 'timestamp')) %}
          Pasto 2
        {% elif now().timestamp() > (state_attr('input_datetime.pasto_gatti_2', 'timestamp'))
        and now().timestamp() < (state_attr('input_datetime.pasto_gatti_3', 'timestamp')) %}
          Pasto 3
        {% else %}
          Pasto 4
        {% endif %}

This actually works only when the first condition is true, otherwise it always jump to the final else.
Is there something wrong with the “and” boolean inside template sensors?