meconiotech
(Daniele Bertocci)
September 20, 2022, 10:23am
1
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:
opened 10:06PM - 15 Oct 20 UTC
closed 12:07AM - 30 May 21 UTC
## Context
As you may know, I help tons of people on the forums and discord. … In 0.116 we limited templates using {{ states }} or {{ states.domain }} to only update once a minute. This is great for all those people who were using sensor.time, but it's not so great for people who just wanted to update based on the state changes inside states.domain. Here's the most recent issue that I ran into, a overall light dynamic light entity.
```
light:
- platform: template
lights:
all_lights:
friendly_name: "All"
value_template: >-
{{ state.light | selectattr('state','eq','on') | list | count > 0 }}
turn_on:
service: light.turn_on
entity_id: all
turn_off:
service: light.turn_off
entity_id: all
```
The gentleman in question only had 15 or 20 lights. He simply wants to have a light that turns and off all lights without needing to adjust a configuration whenever a new light is added. The current band aide to a situation like this is to create an automation to watch states and update the light or come up with some inventive templates.
## Proposal
Make expand() work on domains, or add a new function that expands domains.
In the case above, using expand on the light domain would return a generator of light states.
```
light:
- platform: template
lights:
all_lights:
friendly_name: "All"
value_template: >-
{{ expand('light') | selectattr('state','eq','on') | list | count > 0 }}
turn_on:
service: light.turn_on
entity_id: all
turn_off:
service: light.turn_off
entity_id: all
```
This would update whenever any new light is added, also it would update exactly when a light is turned on instead of being rate limited to once a minute like state.light would do.
## Consequences
The only consequence would be that a user expands the sensor domain and they end up bogging the machine down. Regardless, users will be able to rate limit by just using states.domain, but for those special cases they can use expand to get all the states in a domain.
Could it be useful in this case?
Thank you!
meconiotech
(Daniele Bertocci)
September 20, 2022, 11:25am
2
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")’
petro
(Petro)
September 20, 2022, 11:36am
3
{{ 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.
meconiotech
(Daniele Bertocci)
September 20, 2022, 11:51am
4
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.
123
(Taras)
September 20, 2022, 1:41pm
5
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(', ') }}
petro
(Petro)
September 20, 2022, 2:01pm
6
You want to above using == or != with none, it’s a singular object. I altered the solution to account for none instead.
meconiotech
(Daniele Bertocci)
September 20, 2022, 2:39pm
7
In fact, after a while it stopped working, I followed your suggestion and now it works again.
Thanks everyone for the support.
meconiotech
(Daniele Bertocci)
September 26, 2022, 10:50am
8
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(', ') }}
petro
(Petro)
September 26, 2022, 10:52am
9
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.
meconiotech
(Daniele Bertocci)
September 26, 2022, 10:58am
10
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
meconiotech
(Daniele Bertocci)
September 26, 2022, 11:01am
11
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.
meconiotech
(Daniele Bertocci)
September 26, 2022, 11:04am
13
I don’t understand hass restart turns nextevent into string?
meconiotech
(Daniele Bertocci)
September 26, 2022, 11:27am
15
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.