I’m trying to get more into templating and jinja and am a little stuck on a topic. I have dynamic button-card that will show icons for room when changes occur. (if a motion or door is active, display the icon in a portion of the card). for checking a single entity, i worked that out. but i’m having issues trying to figure out how to do this for lights in a room.
what i want to write up is:
" if any of these lights in the lights[“entities”] list are active, display this icon mdi:lightbulb, else display mdi:blank"
if anyone has accomplished something like this i would greatly appreciate some recommendations
and i know i can achieve the same outcome by adding those lights to a group, and just listing the group in a key:value pair in the config, so i could use the same code like i do for a motion or door, but really wondering if there is way to check if any of the entities in that list in my config file are on.
otherwise, if i make a change to what devices are in the room (kitchen in this example) i would have to make updates to two different files, vs just one.
also took me a few to figure out why i was only getting icons showing when i had more than one device, but that was a simple fix of just updating the var icon = (cnt > 1) to var icon = (cnt >= 1)
thank you very much. i’m really loving the combo of lovelace_gen, button cards, and now also layout cards and state-switch cards. i’m learning a ton.
in your original code snippet, could you please explain the second use/ need of entity.state in the if condition? i get confused hopping between jinja/templates and when /where to use each but perhaps this isn’t the place for that
if (entity && entity.state && entity.state === 'on')
and your config is intense! i’ll need to spend some nights digging into how your doing things there. much more advanced than my usage of these things, but great inspiration
first check is against entity, making sure it exists. Second check makes sure the property ‘state’ exists on the entity object. 3rd checks to see if the value of state is equal to on. It’s for safety. If the first condition fails JS will not check the remainder.
I tried to make my configuration expandable. Meaning I only have to add to the configuration and it will auto place my cards in the correct spot. Ultimate laziness.
and i totally get you here. that is my reason for redoing my whole system to utilize these components as well. after 7 years of tinkering and manually making changes, it’s time to better automate the config and have a single place to just define the devices