Domain template help

Hi, i am not expert of homeassistant templates, i am trying to do this:

I would obtain entity friendly name of a schedule (new helper) that has ‘next_event’ attribute today

This code not work is for example

{% for state in states.schedule -%}
  {% if as_timestamp(state_attr('state' , 'next_event')  | timestamp_custom('%d') ==  now().strftime('%d'))  %}
  {{ state.friendly_name }}
  {% else %}
  ??
  {% endif %}
{%- endfor %}

In github i have found some changes in the domains groups:

Could it be useful in this case?

Thank you!

I am making several attempts but this too does not work unfortunately:

{% for state in states.schedule | selectattr('attributes.next_event', 'defined') | selectattr( 'attributes.next_event.strftime("%d")'  , 'eq' , now().strftime("%d") ) %}
{{ state.name }} - {{ state.state }}
{% endfor %}

The template tester in developer section say: UndefinedError: ‘datetime.datetime object’ has no attribute ‘strftime("%d")’

{{ states.schedule | rejectattr('attributes.next_event', 'none') 
    | selectattr('attributes.next_event', '>=', now()) 
    | selectattr('attributes.next_event', '<=',  today_at() + timedelta(days=1)) 
    | map(attribute='attributes.friendly_name') | list | join(', ') }}

states.schedule → gets all schedules
rejectattr('attributes.next_event', 'none') → filter out events that do not exist
| selectattr('attributes.next_event', '>=', now()) → filter schedules down to events that are happening after this minute
| selectattr('attributes.next_event', '<=', today_at() + timedelta(days=1)) → filter schedules down to events that are happening before midnight today.
| map(attribute='attributes.friendly_name') | list | join(', ') → Grab the friendly name of all filtered schedules and make a comma separated list of them.

OMG Petro, it works perfectly many many thank you!
I am studying a bit of basics I promise that soon I will not disturb you anymore.

The next_event attribute is always defined even if the schedule has no events.

Therefore this filter will always evaluate to true.

selectattr('attributes.next_event', 'defined')

As a consequence, it will allow the next filter to compare a null value to now() and fail with an error.

I suggest you replace it with the following:

selectattr('attributes.next_event', '!=', none) 

For example:

{{ states.schedule | selectattr('attributes.next_event', '!=', none) 
    | selectattr('attributes.next_event', '>=', now()) 
    | selectattr('attributes.next_event', '<=',  today_at('23:59'))
    | map(attribute='name') | list | join(', ') }}

You want to above using == or != with none, it’s a singular object. I altered the solution to account for none instead.

In fact, after a while it stopped working, I followed your suggestion and now it works again.
Thanks everyone for the support.

Sorry if I still bother you, unfortunately it stopped working, the same code that worked a few days ago now says:
TypeError: ‘> =’ not supported between instances of ‘str’ and ‘datetime.datetime’

What can it be caused by?

Here the code:

{{ states.schedule | rejectattr('attributes.next_event', 'none') 
    | selectattr('attributes.next_event', '>=', now()) 
    | selectattr('attributes.next_event', '<=',  today_at() + timedelta(days=1)) 
    | map(attribute='attributes.friendly_name') | list | join(', ') }}

Assuming you restarted?

{{ states.schedule | rejectattr('attributes.next_event', 'none') | rejectattr('attributes.next_event', 'string')
    | selectattr('attributes.next_event', '>=', now()) 
    | selectattr('attributes.next_event', '<=',  today_at() + timedelta(days=1)) 
    | map(attribute='attributes.friendly_name') | list | join(', ') }}

keep in mind it will reject events that are a string. I.e. any events that are provided after a restart of HA that were restored.

Yes I rebooted, these days it worked even after the reboot, unfortunately not now.
Today I have a sensor that should be seen by this template:
next_event: '2022-09-26T21: 00: 00 + 02: 00

Have tried this code now in template editor:
{{ states.schedule | rejectattr(‘attributes.next_event’, ‘none’) | rejectattr(‘attributes.next_event’, ‘string’)
| selectattr(‘attributes.next_event’, ‘>=’, now())
| selectattr(‘attributes.next_event’, ‘<=’, today_at() + timedelta(days=1))
| map(attribute=‘attributes.friendly_name’) | list | join(’, ') }}

It say result type string but there is no value.

Yes that’s expected

I don’t understand hass restart turns nextevent into string?

Yes

I have just restarted the home assistant and it has started working again, I have the impression that I will still have this problem in the future because I still don’t understand how it works.