Active Devices Counter / Lights / Window Sensors

Looking for a simple solution ( Not Yaml since I’ve used only the UI so far and have 0 idea of YAML ) and I understand that YAML is the way to go, but its not for me, at least for the moment.

Im creating my first dashboard with mushroom and on the top im having Chip that shows wether, and I would like to have next to it 3 small icons that shows how many bulbs, windows, doors are opened at the moment.

Im using Home Assistant latest OS Version
Running on M1 Mac mini through UTM VM

2 Likes

I add all my lights into a master group called All Lights - I then use this sensor to show a number…

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

You could create groups for other device types you want to count…
I was going to select all devices by domain but then I got duplicate counts for some of the virtual devices. This way I have control over what is and isn’t considered. I got it from another post but couldn’t find the link for it.

Update: Found it! Here is a domain level which removes groups. Count Lights that ar not a group - #4 by 123

3 Likes

Huge thanks for being active and supportive ! But I kinda have no idea what to do with that code :smiley: like create a new card, then use the YAML option on the bottom instead of UI ?
Or do I have to create it like an automation ? And pleeeeeasse don’t send me a link to two hour long explanation about templates, it just makes things worse…
It’s just super hard to start from 0 especially if You try to learn everything at once, it took me like hours to understand the most basic stuff like cards, and some dashboards and that’s only using UI…
I know it’s hard for some to understand but I’m really starting from 0 and just throwing info at me hoping it sticks, sadly brings no results in my case… baby steps here please :slight_smile:

No worries at all. What I have shared is called a template sensor. By template it means, you have code what the device will display. Unfortunately, if HA is you’re future platform of choice reading links is inevitable. Have a read through this documentation for background: Template - Home Assistant

Using a file editor you will add the necessary “YAML” (manual home assistant configuration) into your configuration file. You’re essentially doing something similar to this: Template - Home Assistant

There is a learning curve with HA. But if you can get your head around the need to configure the easier stuff through the UI and the more complicated stuff using YAML and File Editors - you’ll slowly get used to the more complex stuff and learn where to do what. The community is very supportive.

Let me know if the File Editor and Configuration file is something you know how to get to and edit…this channel is quite good: Access Your Home Assistant YAML Files - YouTube

2 Likes

I wrote almost two A4 page long text about how hard it is and how frustrated I am but no one here should spend time reading all this, long story short.

I red the whole thing 3 times and I’m not lying, in understood only and exactly the first sentence.

The YouTube video I will definitely check it out later today, since I’m not home at the moment.

Nope, I have 0 idea about the configuration thing or file manager.

And again, thanks for taking You’re time :slight_smile: + a simple, part explanation for super dumb, not techy, not native English speaker, ones would be really nice. Like the community is to advanced, the sites with info are way to advanced…

No problem at all, I think the video will be the most helpful. From there hoping it makes a bit more sense. If it doesn’t just ask. It’s difficult to do step by step without you having the background/basic. The video will give you that…

Huge thanks, will get back here after watching ! It’s like I’ve just learned driving a bicycle and now I have to read the manual of a huge airplane and drive it, like everyone says take You’re time explains how the whole hydraulic system works, says to read another section and just fly :smiley: enough of non topic stuff for now :slight_smile: will get back with any progress / regress :smiley:

1 Like

Hi :slight_smile: Had some work and cable management to do so got to it only now :slight_smile:

Before we go any further, just to clarify couple things for me, and im using kinda simple language and simple explanations here, I understand there is more to it :slight_smile:

  • Devices are like physical devices
  • Entity’s are the options or the data that the device provides
  • Template is like a fake, virtual entity
  • YAML configuration is like the whole operating system, instead of buttons, folders, pictures and settings, its just all plain code

so far kinda right ?..

the video was like soo awesome not a word to much !
I got even some questions, but if ill start with that then ill just end in the rabbit hole soon again.

so I’ve done so far:

  • Watched the video 3x times :smiley:
  • Added the file editor
  • pasted You’re template

Questions
#1 Should I paste the templates in some specific place, organise them somehow, what about spacing before and after them ?
#2 Got it running somehow, but it shows the wrong count of lights, I have only light switches, I turned them with helpers into light bulb, for example when I turn one light on, the little bulb icon shows that 3 lights are on…

Got so far ( Picture in attachments ) no progress, from what I’ve understood, a template that was in configuration before ( don’t ask, don’t remember :smiley: ) worked, but worked wrong and shows the wrong count ( If im now wrong it counts each switch that has two zones like two bulbs, so normal one bulb switch works but not the doubled ones… ), the real ones, that You provided, don’t even show up in the list when I try to add them…

Kinda last update I guess :slight_smile:

Firstly and most importantly, I was trying to use a template that didn’t work and I just mixed both of them, after I got clear wich one worked, it started to make a bit more sense, im still not 100% sure about how all this works but I was able to do a count on windows and doors :slight_smile:

Secondly, I had to restart my Home Assistant from UI for the fake things to show up :slight_smile:

And lastly, You’re more than welcome in my profile to help with other 99 issues im having :smiley:

Should I paste the templates in some specific place, organise them somehow, what about spacing before and after them ?

Screenshot 2022-06-29 at 18.23.35

1 Like

Hi @oskarsj94 , well done on pushing through.

The restart is necessary in order for the devices/entities to be created in HA. Any changes would also need a restart. However in some instances, on the Developer/YAML tab, there are different domains towards the bottom which you can reload rather than doing a full restart. Between you me and doorpost, I just restart usually.

There are different strategies for maintaining YAML. For now the simplist is to just maintain the different “integrations” (in your instance you used the sensor integration) within your main Configuration file. As you start to use it more and more, you can have them maintained elsewhere but for now - lets keep it simple. The indenting and “syntax” is really fiddly. I always look for examples in the community posts or reference this HA link on templating: Templating - Home Assistant

If you are interested in “split configuration” or “packages” - here is another decent video: Home Assistant How To - split your configuration files (YAML way) - YouTube BUT if you want to avoid watching a 30min video. Just maintain them in your main configuration.yaml file.

That was some fiddling around but somehow got it to work :slight_smile: and from that point its like gone way worse, I have like around 500 lines of code in YAML in these 24 hours :smiley: of course, together with 500 problems and questions :smiley: Im mostly trying to use Discord but the problem there is that only 3 - 5 people are helpful and active and secondly, they are way to advanced to me, like im still having problems with using the right spacing and stuff… so, this topic is then closed, and I happily invite You to Siemens Smart Home Unavailable

Hi- I’m really struggling to get to a point where I have a card that populates with the total lights on. I’ve added the lines of code to my confi.yaml file but nothing happens- can’t figure out how to “execute” the code. Hopefully that makes sense. Thanks!

The code doesn’t execute per se…what has been setup here is a template device which reacts when lights go into an on state. I’ve actually decided to use groups to help with this use case as I also have incorporated a pop up which shows which lights are on too. Light groups can be created under Helpers - Settings → Devices & Services → Helpers → Create Helper button → Light Group

The only way to then see the count is to add either the entity above to a card on your dashboard or in my case I have some specific code which “ungroups” the light group. For example, I have a condition chip card which appears when the light group is on:

  - type: conditional
    conditions:
      - entity: light.all_lights
        state: 'on'
    chip:
      type: template
      icon: mdi:lightbulb-group
      content: >-
        {{ expand(states.light.all_lights) | selectattr( 'state', 'eq', 'on') |
        list | count }}
      entity: light.all_lights
      icon_color: amber
      tap_action:
        action: fire-dom-event
        browser_mod:
          service: browser_mod.popup
          data:
            title: Lights On
            content:
              type: custom:auto-entities
              filter:
                include:
                  - group: light.all_lights
                    state: 'on'
                    options:
                      type: custom:mushroom-light-card
                      show_brightness_control: true
                      layout: horizontal
                      tap_action:
                        action: toggle
                      use_light_color: true
                      card_mod:
                        style: |
                          ha-card {
                            padding: 4px 12px !important;
                          }
                  - entity_id: light.all_lights
                    state: 'on'
                    options:
                      type: custom:mushroom-light-card
                      layout: horizontal
                      secondary_info: none
                      tap_action:
                        action: toggle
                      card_mod:
                        style: |
                          ha-card {
                            background: rgba(var(--rgb-state-light), 0.1);
                          }
                exclude: []
              card:
                type: custom:layout-card
                cards: []
                layout_type: masonry
              sort:
                method: friendly_name

As you’ll discover there is always more than one way to do something in HA. In my case I started with the code above but then moved to using light groups as I wanted rooms to be grouped rather than a long list of individual lights in the pop up.

Hopefully this helps you.

PS: You will need Browser Mod from HACS installed for the popup to work. But you could remove all the code below the tap_action is this isn’t needed.