Custom Button Card: Templates vs. Lovelace-Gen

Close…
It seems that ‘toggle’ has to be explicitly returned because this,

action: "[[[ if ('{{ radio_button }}' == 'true' && entity.state == 'on') return 'none'; else return 'toggle' ]]]"

will always toggle, which is an improvement on it always doing nothing.

So, the condition syntax doesn’t seem to be working. It doesn’t seem to like the

  '{{ radio_button }}' == 'true'

(or any variation of it that I have tried) because if I do this:

action: "[[[ if (entity.state == 'on') return 'none'; else return 'toggle' ]]]"

It ‘works’, albeit without the flexibility of having that behaviour optional.
(And yes, I use that template elsewhere for buttons that are not ‘radio buttons’ so I’d really like it!)


EDIT: I think I have it…

  action: "[[[ if (('{{ radio_button }}' == 'true') && (entity.state == 'on')) return 'none'; else return 'toggle' ]]]"

Your quotes around {{ radio_button }} and brackets around both conditions…

EDIT 2 I found a better solution allowing it to default to off:

  action: "[[[ var radio_button = '{{ radio_button }}'; if (radio_button && (entity.state == 'on')) return 'none'; else return 'toggle' ]]]"

Thanks for all your help getting there…

radio button was probably already a string so you’d just need to remove the qutoes from my original comment.

"[[[ if ({{ radio_button }} == 'true' && entity.state == 'on') return 'none'; else return '' ]]]"

I’m afraid that doesn’t work with or without ‘toggle’ explicitly defined and neither does,

  action: "[[[ if ({{ radio_button }} && (entity.state == 'on')) return 'none'; else return 'toggle' ]]]"

The button in all cases shows the shadow indicating it was pressed but doesn’t toggle.

Is this your own card? Or is this something available? It looks really good, I’m not sure I would trust HA to run my sprinkler system (what if it crashes while a zone is on?) but I do have it linked to my rachio account so that I can mess with the zones, which worked well for when I installed sod last year.

That card is a piece meal card. He’s using a series of cards to configure that. It’s not a single custom card that you can install, if that’s what you’re asking.

Indeed it is a bit of a Frankenstein.

As for letting HA run your irrigation, I have been doing it for two years (summers only :slight_smile: ) with no problem. In fact it is probably why I found HA, and stayed.

Search for garden irrigation on here and you’ll find plenty of people doing it. You might even find my effort which I’m currently re doing. Hence the new UI.

And there are ways to mitigate for disaster :wink:

Could you share how you created this card ? I would like to use something similar to schedule the blinds and maybe also for garden irrigation too.

It doesn’t lend itself to reuse by copy and pasting but by all means take it and Frankenstein it even more.
Look here

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.