#Mushroom chip card - template count lights on

1 messages · Page 1 of 1 (latest)

vital frigate
#

@fresh frost This is my version of what you are trying to create. In my case, I'm counting just lights that are on in the Living Room area.```yaml
type: custom:mushroom-chips-card
chips:

  • type: template
    content: |-
    {% if states.light | selectattr('state', 'eq', 'on')
    | map(attribute='entity_id') | map('area_name')
    | select('in', ['Living Room'])
    | list | count == 0 %}
    {0}
    {%else %}
    {{states.light | selectattr('state', 'eq', 'on')
    | map(attribute='entity_id') | map('area_name')
    | select('in', ['Living Room'])
    | list | count}}
    {%- endif %}
    icon: mdi:lightbulb
    icon_color: |-
    {% if (states.light | selectattr('state', 'eq', 'on')
    | map(attribute='entity_id') | map('area_name')
    | select('in', ['Living Room'])
    | list | count) >= 1 -%}
    yellow
    {%- else -%}
    white
    {%- endif %}
    hold_action:
    action: call-service
    service: light.turn_off
    service_data: {}
    target:
    area_id: living_room```
#

And, now that I'm looking at it, I need to add an exclude to not count the light.living_room_lights group. And, I should also not count the FKB tablet screen since it is a light entity, too. (There's only two lights in the living room.)

fresh frost
#

Thanks for this. So you can define templates directly in the mushroom chips card? Can templates be referenced just like entities? I feel like I have no problem creating a group in groups.yaml and then I can refer to it. Is this how templates work too?

vital frigate
#

The catch to being able to use templates in the Mushroom cards is you have to use the template version. Most other cards will not accept templates or Jinja code. The downside of using a template card, however, is must define every, single thing. It is basically a blank card.

#

I haven't started to use it yet but was playing around yesterday and looking over these docs.

fresh frost
#

Thanks. If we go back to my original code, I have the following:

all_lights specified in groups.yaml

The following code specified under sensor.yaml:

- platform: template
  sensors:
    all_lights_count:
      friendly_name: "all_lights_count"
      icon_template: mdi:lightbulb-group
      value_template: "{{ expand('group.all_lights') | selectattr('state','eq','on') | list | count }}"

However I don't have a sensor named sensor.all_lights_count. When developing this under dev tools, the logic does show the number count. Is this due to the mushroom cards as you mentioned and why I have to use template?

type: custom:mushroom-chips-card
chips:
  - type: template
vital frigate
#

Did you reload/restart after adding the sensor to configuration.yaml?

fresh frost
#

Just trying to understand this at the base level, as I found myself going in circles on the documentation I read this AM.

For example, if you wanted to reference the living room light count in an automation, thats covered under the reusing templates link you provideD?

#

Yeah

vital frigate
#

Bascially, my plan is to incorporate a variable for the area in the template that I posted above so then I can pass a different area. So I could make 3 chips that each show a different area without having to reproduce the code inline with the card. (Based on the links above.)

#

But let's take a step back. Do you have the group.all_lights entity already?

fresh frost
#

Yeah, I have group.all_lights.

type: custom:mushroom-chips-card
chips:
  - type: weather
    entity: weather.forecast_home
    show_conditions: true
    show_temperature: true
  - type: entity
    entity: lock.lock_frontdoor
  - type: entity
    entity: group.all_lights
alignment: justify
vital frigate
#

Okay. So that is at least working. However, like RobC was mentioning, you should create a light group instead. This will give you more options in the long-run. (You can't set brightness, for example, on a regular group IIRC.)

fresh frost
#

Yeah so i defined that in the helpers UI

vital frigate
#

Which type of group did you select?

fresh frost
#

light group

vital frigate
#

In your other post you said I defined the group all_lights in groups.yaml.. Those aren't the same thing.

#

then you should have ended up with a light.all_lights entity.

fresh frost
#

Yeah, so I have both now, I added it after it was mentioned

#

I adjust the sensor then for light.all_lights and it should be able to be refereced?

vital frigate
#

Gotcha. Okay. Change your chips card to reference the light entity instead of the group. (And then at some point get rid of the group entry as you don't need it.)

fresh frost
#

QQ: Are these two equal?

#

Also, I changed to light.all_lights in the mushroom card and ultimately:

vital frigate
#

Honestly, I'm not entirely sure. It seems they do have some different functions. Try the YAML configuration reloading first. If it doesn't load what you're expecting, then try the Restart.

#

Okay. So now you at least have the state of the light.all_lights entity.

#
type: custom:mushroom-chips-card
chips:
  - type: template
    content: |-
      {{ expand('light.all_lights') | 
      selectattr('state','eq','on') | 
      list | count }}```
fresh frost
#

Got it, very helpful, thanks! I was breaking down your original block of code you provided for the living room.

#

is the light group helper in the file editor / vsc anywhere?

vital frigate
#

Exactly. I forget why I added the first part of the IF where if the count is 0 it shows 0. Your code works just fine with no lights on.

#

Not in code form but you can find it in Dev Tools > States and Settings > Devices & Services > Entities or Helpers tabs. I'm sure it is tucked away in a database somewhere.

#

If you want to add/remove lights to the group, click the entity > Settings cog > Group options.

fresh frost
#

Just what I was looking for, thank you!!