I have an issue when I try to use a ‘for each’ loop, I can’t use variables as filters in the value_template and in the message, and an error occurs during execution. I followed the instructions on this page and tried to find a solution on the forum but I couldn’t understand how to solve it.
As you can see from the screenshot, the value ‘2’ is correctly populated in the title.
For example, if I manually replace ‘repeat.time.days’ in value_template with the value “2”, everything works correctly.
Remove the quotes from around 'repeat.item.giorni' in the selectattr() filters. With the quotes you are not rendering the variable but instead selecting for states with the value of the string “repeat.item.giorni”. You will need to either set the values in the for_each to strings by adding quotes or cast the variable to the string() filter.
FWIW, you can also remove the expand() functions and use select().
The issue is likely in the condition of the If action… post the current script you are using there.
Just to be clear, I did not remove the selectattr(), it was switched to a select() filter with an is_state test. The combination is a relatively new addition to HA’s templating, but it is more efficient than using expand(). Your use of selectattr() was correct for the way you had constructed the original template.
I believe I found the problem. The for_each sequence needs to be added at the end, not at the beginning. I rewrote the sequence from scratch, also making a small modification to the message, and now it works. Here’s the working code in case anyone needs it: