Custom Button Card: Templates vs. Lovelace-Gen

thank you, I will try it

Hi @petro, could you share your code for counting the number of lights? Right now I have a separate automation just for counting the lights on per group …

In jinja or JS?

Preferably JS, but I’m fine with both as long as it updates whenever the count changes.

I’m not sure if my code will help. I built my cards based on the assumption that you provide the button with a list of entities to count from.

      items.forEach(item => {
        var count = 0;
        item.entities.forEach(entity_id => {
          var stateobj = states[entity_id];
          if (stateobj !== undefined)
            if (item.state.includes(stateobj.state))
              count = count + 1
        });
        if (count > 0){
          var units = (count == 1) ? item.units.plural(true) : item.units;
          ret.push(getEntityIcon(count, units, item.icon, item.color));
        }
      });

located in this file

1 Like

@petro I think you are there Man :slight_smile:
I am trying since some days now to have a button with some other sensors embedded in.
The idea is bulb (big) plus triggering / related elements smaller at the corners (motion sensor ,illuminance, switch) dynamic (ie with corresponding state live)
Tried that with custom fields in button card
No luck
Just new one example then I walk alone

By the way. Wonderful ordered config. I will remodel mine on yours

A post was merged into an existing topic: Lovelace: Button card

Please create a new thread or ask this in the custom button card thread. This thread is about the differences between the button card and lovelace gen.

sorry thanks

@petro, I was just looking at your configuration and it’s given me more than a few ideas!

I’ve also been using lovelace_gen and jinja2 macros to generate some portions of my lovelace UI (though still mostly just getting started.) One thing that you might consider when using jinja2 macros to generate the YAML configuration is that JSON-style syntax works just fine inside of YAML files. So you can mostly just ignore the whitespace indentation issue completely, and have the macro emit JSON in the place that it’s invoked.

I made a post about this about a year ago that lays this out. This approach might simplify things a bit.

I knew about that before hand, the problem is if you make mistakes it’s much harder to see the unformatted json. I made the yaml things, so I can quickly copy and paste the entire templates into the template editor to see the results printed in a nice fashion.