you copied and pasted unformatted code, as a result you have “fancy” quotes. Those do not work with templates.
Many thanks. Indeed, I just changed it.
However the error is still present.
Is this just a copy error or is it wrong indented?
Hi, it’s just a copy error on the forum … in the yaml config it’s right. There were many light included in the light groups, so i deleted most of it for posting it here . Many thanks for your remark!
I’ll look a little bit further
Tried and couldn’t get your code to work. This variant works:
- sensor:
- name: light group
state: >-
{{ expand(state_attr('light.your_light_group', 'entity_id') )
| selectattr('state', 'eq', 'on')
| list
| count
}}
You are a hero! All is working well now!
Many thanks!!!
Hi Guys.
I am totally new to HA and trying to implement this idea in my ecosystem.
Unsuccesfuly.
Can someone direct me step by step where to put those codes to create this sensor counting lights on?
I did put it in template editor and it counts lights but cannot find the sensor to use.
Thanks in advance
Robert, are you comfortable with yaml? if so, type this in at the bottom of configuration.yaml
and restart:
sensors:
- platform: template
count_lights_on:
friendly_name: "# Lights on"
unit_of_measurement: 'on'
value_template: "{{ states.light |selectattr('state','equalto','on') |list |count }}"
You should then have a sensor called sensor.count_lights_on
Done it, went to check configuration before start and got this message: Configuration invalid! Integration error: sensors - Integration ‘sensors’ not found.
I can paste pic of this part of my config file, if that helps
What he posted is not correct.
sensor:
- platform: template
sensors:
count_lights_on:
friendly_name: "# Lights on"
unit_of_measurement: 'on'
value_template: "{{ states.light | selectattr('state', 'eq', 'on') | list | count }}"
Apologies - I have it in a separate .yaml and messed up with a typo and incorrect tabbing when copy/pasting to be be in the configuration.yaml
Thank you so much, you are the stars, guys. All works perfectly.
Just one more thing, got some switches also controlling lights, them entities are coming like: switch.relay_switch_1x3kw. How I could add them to total count of lights? I don’t see any issue to add entities to the list of lights in config.yaml, if that would be the easiest way. This is because, some switches are controling lights and some of them, different devices (not lights), so I believe list of entities would be easiest way.
Thank you
You need to create a “new” light; something like* this in configuration.yaml
light:
- platform: switch
name: "your name here"
entity_id: switch.relay_switch_1x3kw
“your name here” is a light that will reflect the off/on state of switch.relay_switch_1x3kw
and will be counted
[edit: typo*]
Hi petro,
you helped me few weeks ago!
I am now trying to use this in a custom card (ui-lovelace-minimalist) I know this in not the topic for it but
I thought boy could point me to some direction how to achieve this?!!
what I am doing;
I am trying to use what worked in the markdown in the custom card.
label: |-
[[[
if (variables.room_card_label) {
return '<ha-icon icon="mdi:thermometer" style="width: 16px; height: 16px;"></ha-icon>' + (entity.attributes.current_temperature || entity.attributes.temperature || entity.state || '-') + '°C' + ' | '
+ '<ha-icon icon="mdi:water-percent" style="width: 18px; height: 18px;"></ha-icon>' + (entity.attributes.current_humidity || entity.attributes.humidity || entity.state || '-') + '%' + ' | '
+ '<ha-icon icon="mdi:lightbulb-on" style="width: 16px; height: 16px;"></ha-icon>' + ' count ' + ( (variables.room_card_area_name) | selectattr('domain','eq','light') | selectattr('state','eq','on') | list | count );
} else if (entity.state == "on") {
return variables.ulm_on
} else if (entity.state == "off") {
return variables.ulm_off
} else {
return entity.state
}
]]]
it seems I cannot copy paste what worked in markdown in the custom_card.
how do I implement this?
Inspired by this post, I came up with the following code in my configuration.yaml
in order to NOT count multipe times lights that are in one or more groups (the items in the reject list are all light / - platform: group
in configuration.yaml as well) :
sensor:
- platform: template
sensors:
count_lights_on:
friendly_name: "# Lights on"
unit_of_measurement: "on"
value_template: >
{% set reject = ['light.home_lights', 'light.downstairs_lights', 'light.upstairs_lights', 'light.office_lights', 'light.study', 'light.living_room_lights', 'light.dining_room_lights', 'light.garage_lights'] %}
{{ states.light | rejectattr('entity_id', 'in', reject) | selectattr('state','equalto','on')| list | length }}
Hello, I have added the follwing code in the yaml file:
template:
- sensor:
- name: “number_of_lights_on”
unit_of_measurement: ‘on’
value: “{{ lights | selectattr(‘state’,‘eq’,‘on’) | list | count }}”
but I am getting an invalid config error even if the “check configuration” is OK.
- name: “number_of_lights_on”
Invalid config for [template]: [value] is an invalid option for [template]. Check: template->sensor->0->value. (See /config/configuration.yaml, line 140).
Any idea what could be wrong?
You have to use “state” instead of “value”.
If i wanted to add in switches, like smart plugs and to count along with lights on, how would I do it?
I’ve tried listing individual entities but seems to be adding one extra when i do it that way.
isn’t that:
value_template: "{{ expand('group.luces', 'light.whatever') | selectattr('state','eq','on') | list | count }}"
Don’t need that closing brace after count.
Correct. That final closing parenthesis is a typo and is unecessary. All subsequent examples I posted (three or four) don’t have it.
I’ve corrected the example. Thanks for bringing it to my attention.