Trying to show how many lights are on

sorry i’m new at this. Is this right ?

I believe the “entity” they are referencing is actually the automation. Which is ‘on’

they need to create a template sensor based on the template they posted above. not an automation.

then use the sensor state in the card config.

Yes, that helps. The screenshot shows that you are actually refencing the automation. So when “On” is displayed on your dashboard, it means that the automation itself is on/active/not disabled. Automations do not return a value.

There are multiple ways you could achieve what you actually want to do. In my opinion the most usefull solution would be to create a template sensor and select that sensor in your exiting lovelace config. You already have the template ready so it should be easy to setup. Add the following to your configuration.yaml

template:
  - sensor:
      - name: "Total Lights Count Template"
        state: {{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}       

(Obviously check for spelling errors, since I typed this from your screenshot :wink: )

are trying but are new at this. did find the configuration.yaml and added the infor but getting this. i don’t understand where it’s wrong Screenshot 2023-01-30 114253

template:

  • sensor:
    • name: “Total Lights Count Template”
      state: {{ states.light | rejectattr(‘attributes.entity_id’, ‘defined’) | selectattr(‘state’, ‘eq’, ‘on’) | list | count }}

My bad. Add " around the template part.

template:
 - sensor:
     - name: "Total Lights Count Template"
       state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"

Thanks for the help, now i get a number, not only “ON”

thanks :slight_smile: :slight_smile:

Is there anyone how can give me a step by step instruction on how to do this

I’m new to HA after transferring from sharp tools

I sort of understand the process, but am getting confused when you say add to yaml config etc, I have the editor but don’t know where to input it etc.

Appologies of this is “jannet and John”

Finding the whole thing rather confusing.

I think If I had a step by step instruction once I’d followed that I’d be able to do a bit more…

Ensure you have an editor installed in your add-ons

image

Editor

find this file
image

add this to the bottom of the file to count all lights and save the file. Restart HA to create the sensor.

template:
 - sensor:
     - name: "Total Lights Count Template"
       state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"

add this with a manual list of lights that you want to count if you don’t want all the lights counted. You don’t need both added, the second can be helpful if you group lights as a single entity.

sensor:
  - platform: template
    sensors:
     lights_on:
       friendly_name: 'Lights ON'
       value_template: >
       
          {% set lights = [
          states.light.pc_lights,
          states.light.bed_lights,
          states.light.tv_lights,
          states.light.night_stands,
          states.light.bathroom_lights,
          states.light.server_light,
          states.light.closet,
          states.light.hallway_lights,
          ] %}
          {{ lights | selectattr('state','eq','on') | list | count }}          

There are multiple ways so you may receive additional advice, but this works. You do have to define the lights to get a real-time count. Both of these methods will do that.

4 Likes

Hi many thanks for the help - i have followed your instructions and I have ended up with this

image

is that right? how can i get it to hid the platform: template sensor text?

thanks in advance

Can you post the sensor portion of your configration.yaml You have an alignment issue that is causing this issue.

thanks for the quick response on, everyone has been so helpful here - i deleted and re did what you listed above and its worked perfect now

1 Like

Im getting cheeky now - is it possible to make the number of lights on bigger and centered?

Can you post the card code please?

see #11 here

Sorry I’m really struggling here - ive read the post but how do i post the code like you did?

Use three back tick at the top and bottom of your code

image

image

code here
 - sensor:
     - name: "Total Lights Count Template"
       state: "{{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}"      ```

I think I've got it -thank you so much

I was trying to do was to see if i could the number of lights bigger, any tips?
binary_sensor:
  - name: Ein Licht an
    device_class: light
    unique_id: einlichtan_status
    state: "{{ is_state('switch.00161a499cb9a2_2', 'on') or
      is_state('switch.00161a499cb9a2_18', 'on') or
      is_state('light.00169be9a0f15a_10', 'on') or
      is_state('switch.00161a499cb98e_26', 'on') or
      is_state('light.led_wohnzimmer', 'on') or
      is_state('switch.00161a499cb98e_6', 'on') or
      is_state('light.ambilight', 'on') or
      is_state('switch.00161a499cb98e_2', 'on') or
      is_state('light.00169be992f89f_10', 'on') or
      is_state('switch.00161a499cb9a2_10', 'on') or
      is_state('switch.00161a499cb98e_10', 'on') or
      is_state('light.ambilight_arbeitszimmer', 'on') or
      is_state('input_boolean.helfer_spiegelschrank', 'on') or
      is_state('light.led_badezimmer_oben', 'on') or
      is_state('light.led_badezimmer_unten', 'on') or
      is_state('switch.00161a499cb9a2_30', 'on') or
      is_state('light.led_schlafzimmer', 'on') or
      is_state('light.deckenventilator', 'on') or
      is_state('light.00169be992f89f_2', 'on') or
      is_state('light.00169be992f89f_6', 'on') or
      is_state('light.00169be9a0f15a_6', 'on') or
      is_state('light.00169be9a0f15a_2', 'on') or
      is_state('switch.00161a499cb9a2_14', 'on') }}"
    attributes:
      gesamte_lichter: >-
        {{ [
          is_state('switch.00161a499cb9a2_2', 'on'),
          is_state('switch.00161a499cb9a2_18', 'on'),
          is_state('light.00169be9a0f15a_10', 'on'),
          is_state('switch.00161a499cb98e_26', 'on'),
          is_state('light.led_wohnzimmer', 'on'),
          is_state('switch.00161a499cb98e_6', 'on'),
          is_state('light.ambilight', 'on'),
          is_state('switch.00161a499cb98e_2', 'on'),
          is_state('light.00169be992f89f_10', 'on'),
          is_state('switch.00161a499cb9a2_10', 'on'),
          is_state('switch.00161a499cb98e_10', 'on'),
          is_state('light.ambilight_arbeitszimmer', 'on'),
          is_state('input_boolean.helfer_spiegelschrank', 'on'),
          is_state('light.led_badezimmer_oben', 'on'),
          is_state('light.led_badezimmer_unten', 'on'),
          is_state('switch.00161a499cb9a2_30', 'on'),
          is_state('light.led_schlafzimmer', 'on'),
          is_state('light.deckenventilator', 'on'),
          is_state('light.00169be992f89f_2', 'on'),
          is_state('light.00169be992f89f_6', 'on'),
          is_state('light.00169be9a0f15a_6', 'on'),
          is_state('light.00169be9a0f15a_2', 'on'),
          is_state('switch.00161a499cb9a2_14', 'on')
        ] | count }}
      aktive_lichter: >-
        {{ [
          is_state('switch.00161a499cb9a2_2', 'on'),
          is_state('switch.00161a499cb9a2_18', 'on'),
          is_state('light.00169be9a0f15a_10', 'on'),
          is_state('switch.00161a499cb98e_26', 'on'),
          is_state('light.led_wohnzimmer', 'on'),
          is_state('switch.00161a499cb98e_6', 'on'),
          is_state('light.ambilight', 'on'),
          is_state('switch.00161a499cb98e_2', 'on'),
          is_state('light.00169be992f89f_10', 'on'),
          is_state('switch.00161a499cb9a2_10', 'on'),
          is_state('switch.00161a499cb98e_10', 'on'),
          is_state('light.ambilight_arbeitszimmer', 'on'),
          is_state('input_boolean.helfer_spiegelschrank', 'on'),
          is_state('light.led_badezimmer_oben', 'on'),
          is_state('light.led_badezimmer_unten', 'on'),
          is_state('switch.00161a499cb9a2_30', 'on'),
          is_state('light.led_schlafzimmer', 'on'),
          is_state('light.deckenventilator', 'on'),
          is_state('light.00169be992f89f_2', 'on'),
          is_state('light.00169be992f89f_6', 'on'),
          is_state('light.00169be9a0f15a_6', 'on'),
          is_state('light.00169be9a0f15a_2', 'on'),
          is_state('switch.00161a499cb9a2_14', 'on')
        ] | select('equalto', true) | list | count }}

Is there a question or advice associated with all this code?

Its Working fine, just a Solution/Sample :slight_smile:

There is something similar further up this post

sensor:
  - platform: template
    sensors:
     lights_on:
       friendly_name: 'Lights ON'
       value_template: >
       
          {% set lights = [
          states.light.pc_lights,
          states.light.bed_lights,
          states.light.tv_lights,
          states.light.night_stands,
          states.light.bathroom_lights,
          states.light.server_light,
          states.light.closet,
          states.light.hallway_lights,
          ] %}
          {{ lights | selectattr('state','eq','on') | list | count }}