Code Question For The Guru's

This code is pasted into a custom template card, and it shows how many lights are on. How would I make this not show if the result is zero lights on?

{{ states.light|selectattr('state','equalto','on')|rejectattr('entity_id', 'search', 'lights')|list|length }}

perhaps::

{% set lights=states.light|selectattr('state','equalto','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}
{% if  lights>0 %}
{{  lights }}
{% else %}
{{''}}
{% endif %}

Probably the person may want to show the WHOLE ROW only if there are some “ON” lights?

I’ll test what you have there…

Basically this is what the card shows right now. There are four total lights on in the house.

Overview

But if there are no lights on, it just shows the same thing with no “4”, so it looks as though it’s an indicator that there are “lights on”, when there aren’t any on.

You should have started with a naming a card what you are going to use.
What is that “custom template card” exactly?
What is a desired look in case of no lights?
Post a picture with a draft (MS Paint or whatever).

That card is a Mushroom Template Card.

Ideally, I want the card to disappear when the value is zero. But I’d be happy if it just said “0” or “None”. Like a conditional card, but because it’s a template, I’m not sure how to do the conditional part. I’m experimenting at the moment with different ideas.

Know nothing about mushroom cards.
But if you need to have this card disappeared - then there are at least 2 options:

  1. The mushroom card is placed inside a conditional card
    So far, a standard conditional card does not support templates to decide whether to show an internal card or not. So, you may use a custom:state-switch card which does support templated conditions like “if number of lights >= 1 - then condition is TRUE”.
  2. Apply a card-mod style for the mushroom card like “if number of lights >= 1 - then display: none”.

Both ways may have some pitfalls…

1 Like

You have to template the icon_color field and the primary field, too. Use a variable as @Heinz has shown you.


icon_color: |
  {% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}
{% if  lights>0 %}amber
{{  lights }}
{% else %}disabled
{% endif %}

Or shorter:


icon_color: |
  {% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}
  {{ 'amber' if lights > 0 else 'disabled' }}

Same with primary field.

Thanks, but this functions pretty much the same as my original card.

I think I’d be happy if there was a way to take my original code, and modify it so it displayed “None” if the value was “0”. It’s probably the simplest solution to this problem. My main concern is not confusing the wife by seeing a button that just says “Lights On” when the value is “0”, because she may think it’s telling her there are lights on.

If it said “None”, it would be clearer to anyone looking at it.

Any thoughts how to accomplish that?

Thanks,
Steven

That’s just showing the code, not the result? Maybe because it’s embedded in a card, not a standalone template sensor?

Screenshot at 2022-12-03 11-03-07

type: custom:mushroom-template-card
primary: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %}
  {% if lights>0 %}
    {{'Lamps on'}}
  {% else %}
    {{'Lamps off'}}
  {% endif %}
secondary: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %}
  {% if lights>0 %}{{ lights }}{% else %}{{ '' }}{% endif %}
icon: mdi:home

I made a card to test. This works but you can not choose “None” since it shows nothing I used “Lamps off”

Which means
Primary:

  {% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id','search', 'lights')|list|length %}
  {% if lights>0 %}{{'Lamps on'}}{% else %}{{'Lamps off'}}{% endif %}

and Secondary:

  {% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}
  {% if lights>0 %}{{ lights }}{% else %}{{ '' }}{% endif %}
1 Like

I tested this out and it works flawlessly, thank you!

I tried to implement some form of the reply from @pedolsky above to change the icon color depending on the state, but I can’t get it to work. If I paste…

icon_color: |
  {% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}
  {{ 'amber' if lights > 0 else 'disabled' }}

into the icon color field it doesn’t change appropriately. I imagine I have to paste some form of code into the “primary” and “secondary” sections to set the icon and color?

It’s fine as it is, but that’s the only thing keeping it from being perfect. Ideally, I’d like it to change between this…

Screenshot at 2022-12-03 12-59-39

So far, the code for anyone with Mushroom installed is this…

type: custom:mushroom-template-card
primary: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %} {% if lights>0 %}
    {{'Lights On'}}
  {% else %}
    {{'Lights Off'}}
  {% endif %}
secondary: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %} {% if lights>0 %}{{ lights }}{% else %}{{
  '' }}{% endif %}
icon: mdi:home-lightbulb
layout: vertical
badge_color: ''
tap_action:
  action: none
hold_action:
  action: none
double_tap_action:
  action: none
icon_color: ''

This creates a card that counts the number of current lights on in the house while excluding light groups. The only caveat is to make sure your entities for light groups are named with “lights”, and individual lights are named “light” without the “s”. This matters, for example, if you have “vanity lights” of a set of multiple bulbs that you want treated as a single light. If you name them “vanity lights” they will be excluded because of the “s”. Just stick to some naming conventions, and this works beautifully to let you know if you left any lights on somewhere…

type: custom:mushroom-template-card
primary: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %} {% if lights>0 %}
    {{'Lights On'}}
  {% else %}
    {{'Lights Off'}}
  {% endif %}
secondary: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %} {% if lights>0 %}{{ lights }}{% else %}{{
  '' }}{% endif %}
icon: mdi:home-lightbulb
layout: vertical
badge_color: ''
tap_action:
  action: none
hold_action:
  action: none
double_tap_action:
  action: none
icon_color: >-
  {% set
  lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %}{{ 'amber' if lights < 0 else 'green' }}

Icon color:

{% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id',
  'search', 'lights')|list|length %}{{ 'amber' if lights < 0 else 'green' }}

Change the icon colors to whatever you want

Yeah, I tried similar on my own. I thought I was doing something wrong. In your example, the icon is green no matter what state the lights are in.

Works for me

In the icon color field:

{% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}{{ 'amber' if lights < 0 else 'green' }}

Are you sure all lamps are off or on?

It should be greater than 0.

2 Likes

That’s strange… I’ll tinker with it, because it stays green for me. I appreciate all your help.

:rofl: I used it for testing … :stuck_out_tongue:
Adjusted:

{% set lights=states.light|selectattr('state','eq','on')|rejectattr('entity_id', 'search', 'lights')|list|length %}{{ 'amber' if lights > 0 else 'green' }}

“Testing” me, no doubt? LOL

That works. Believe it or not I tried “= 0”, but never tied that together. Thanks for everything!

1 Like