I am relatively new to Home assistant. I am trying to create an automation who does the following:
I have a few sensors who read certain youtube channels. If there is a video posted today, the sensor state is ‘true’. I want a TTS message who checks the first sensor. If it’s ‘true’ say something. Then go to the next sensor, etc. So I want all sensors checked. If all sensors are ‘false’ it has to say ‘there is no new video’.
I tried it like this:
But this doesn’t work, because when a sensor is ‘true’ it wouldn’t check the next sensors.
I can do it like this for every sensor:
{% if is_state('sensor.oceancam_new_video', 'True') %}
Ocean cam heeft de volgende video geplaatst. {{ states('sensor.ocean_cam') }}.
{% else %}
ocean kem heeft geen nieuwe video
{% endif %}
That works, but it says with every sensor that is ‘false’ it hasn’t a new video.
That isn’t wat I want. I want it to skip, and go to the next one.
You can initialize a variable (at 0) as the first step.
and set variable to 1 each time a sensor is true (just test if there is a new video …) If the variable is still 0 at the end, you do not have any new video … Never tried but it seems pretty OK using development tools…
{% set var = 0 %}
{% if is_state('sensor.oceancam_new_video', 'True') %}
De volgende reminder staat voor vandaag ingesteld. {{ states('sensor.ocean_cam') }}.
{% set var = 1 %}
{% endif %}
................ test the others.....
{% if var == 0 %}
ocean kem heeft geen nieuwe video
{% endif %}
If you’re interested in trying a different technique, paste the following into the Template Editor and experiment with it:
{% set ns = namespace(videos = []) %}
{% for s in expand( 'sensor.oceancam_new_video', 'sensor.what_about_it_new_video',
'sensor.mark_jones_new_video', 'sensor.harry_mack_new_video',
'sensor.miss_monique_new_video', 'sensor.kuylendam_new_video',
'sensor.fantaisyland_new_video' )
| selectattr('state', 'eq', 'True') | list %}
{% set id = s.entity_id[:-10] %}
{% set ns.videos = ns.videos + ['{}: {}'.format(state_attr(id, 'friendly_name'), states(id))] %}
{% endfor %}
{% set total = ns.videos | count %}
{% if total > 0 %}
There are {{ total }} new videos.
{{ ns.videos | join(', \n') }}
{% else %}
There are no new videos.
{% endif %}
That’s great… I’ve been using a dictionary to get a combined read out of overdue chores or room-specific temperatures: LUTS for TTS Examples · GitHub, but your technique could save some time setting up and adding new sensors to an existing template.
Simplified maintenance is one of the primary advantages of separating the data (in this case, the many sensors providing values) from the code that manipulates the data.
In the example I posted above, if all of the sensors were part of a group, the first line of the for statement can be reduced to:
{% for s in expand('group.videos') | selectattr('state', 'eq', 'True') | list %}
Wow, thank you! That is a clever piece of code. As I know nothing about coding, I would never come up with this. But when watching your code, I do understand how it works. I tested it and after renaming some sensors, it works like a charm.