Compact code to check state of a member within a group

Hello everyone!
Here’s my 1st try to get active help from all your HA experts :wink:
I did read hundreds of postings and learned a lot but now I’, stucked…

What I have (or plan to build): a group of input.boolean helper (“Mo”, “Di”, “Mi”, “Do”, “Fr”, “Sa”, “So”) represented as a button on my dashboard.
I’m looking for a nice idea to get “true” or “false” of the corresponding weekday-item by using '{{ now().weekday() }} - or anything else - as an index to access the group (or array).
A ‘brute force’ way would result in endles cascading IF THEN ELSE code.

I hope that my desription could be understood, any hint to an already documented solution would be fine too.
Thank you so much and have a nice day!
Wilfrid

Do you want to be able to change them (which is what an Input Boolean helper does) or just display them?

If these are just for display, create a Template Binary Sensor helper instead for each day, with state template (for Monday):

{{ now().weekday() == 0 }}

Thanks! These input.booleans serves as switches to control my (first) irrigation automation:


I was looking for a template to check ie the 3rd item in my group - when current weekday is “3” and so on. I succeed with a template that results in a list with all entities with state ‘on’. Unfortunately not helpful for me. My idea was - maybe I’m wrong here - that a group is the same as an array where I can access elements by an index.
So I have to try and error more…

Have you looked at the Schedule helper? You can set up a weekly schedule, and trigger an automation from that very simply.

thanks, might be useful for other stuff. I want to tell my automation only to run on dedicates weekdays (as defines by these boolean input helper). But weekday toggles are not used as trigger, only as state.
But maybe I found another solution - and another problem to solve: place a parameter / field value into a template expression of a script. Seems not to be allowed :neutral_face:

In the following example, group.days is a legacy Group containing seven Input Booleans. Its entity_id attribute contains an ordered list of seven input_boolean entities (starting with the Input Boolean representing Monday and ending with the one for Sunday).

The second line checks if the state of the input_boolean corresponding to the current day is on.

{% set days = state_attr('group.days', 'entity_id') %}
{{ is_state(days[now().weekday()], 'on') }}

I tested it on my system and it works correctly.

1 Like

Thank you @123, I will try tomorrow and will come back :slightly_smiling_face:

Another option not requiring a group would be to name your Input Booleans with the weekday number at the end:

input_boolean.tag0  # Monday
⋮
input_boolean.tag6  # Sunday

Then:

{{ is_state('input_boolean.tag%d' % now().weekday(), 'on') }}
1 Like

Thanks a lot! That is what I was looking for! Working well and will save tons of - mostly redundant - code. As an “computer science veteran” (startet programming with fortran77, PL/1, Cobol, Pascal…) it seems to be nessecary to dive into jinja :innocent:

thanks! This is even much more compact - wow!

FWIW, Troon’s example employs python’s modulo % operator to format the string.

Jinja2 borrows from python, so it’s useful to develop familiarity with python.


BTW, did my example work with your existing group entity?

1 Like

thanks @123, yes your template works well to!
My 1st reply should have inserted after your post, but was added at the end, after example from @Troon. Seems that “reply” to any post will put at the end :thinking:

I want to understand your (both) examples so I have to learn basics (hard in my age :smiley:

1 Like

Copy-paste the following template into the Template Editor and examine the output. Replace group.days with the entity_id of your group entity.

{% set days = state_attr('group.days', 'entity_id') %}
{{ days }}

{{ days[0] }}, {{ is_state(days[0], 'on') }}
{{ days[1] }}, {{ is_state(days[1], 'on') }}
{{ days[2] }}, {{ is_state(days[2], 'on') }}
{{ days[3] }}, {{ is_state(days[3], 'on') }}
{{ days[4] }}, {{ is_state(days[4], 'on') }}
{{ days[5] }}, {{ is_state(days[5], 'on') }}
{{ days[6] }}, {{ is_state(days[6], 'on') }}

Today: {{ now().weekday() }}
{{ days[now().weekday() }}, {{ is_state(days[now().weekday()], 'on') }}