I have this code that counts all my smart bulbs and show number of turned On Bulbs im my home, which works perfectly. It’s only counts bulbs, but i also have couple of smart sockets with normal bulbs
switch.lamp_socket_1
switch.lamp_socket_2
switch.lamp_socket_3
that i would like to count them togather with the bulbs.
This is My Code:
# lights of count
- platform: template
sensors:
lights_of_count:
friendly_name: 'lights of count 1 floor'
unique_id: lights_of_count
value_template: >
{%- set search_state = 'on' %}
{%- set search_areas = ['My Home Area'] %}
{%- set ns = namespace(lights=[]) %}
{%- for light in states.light | selectattr('state','eq', search_state) %}
{%- for area in search_areas %}
{% if area_name(light.entity_id) == area and "1 floor" not in state_attr(light.entity_id, "friendly_name") %}
{%- set ns.lights = ns.lights + [ light.entity_id ] %}
{% endif%}
{%- endfor %}
{%- endfor %}
{{ ns.lights| list | length }}
i’m kinda new to HA, any idea how i can achieve that.
# lights of count
- platform: template
sensors:
lights_of_count:
friendly_name: 'lights of count 1 floor'
unique_id: lights_of_count
value_template: >
0
This template does not listen for any events and will not update automatically.
Ouch! It works fine in the Template Editor but it’s true that, when used in a Template Sensor, it doesn’t contain anything that Home Assistant can monitor for state-changes.
Looks like it’ll need to be based on states.light and states.switch in order to monitor their state-changes.
Both templates I’ve posted so far correctly report the number of lights and switches that are on in my “Kitchen” area. If this latest version still reports a count of zero for you, then I need to know if you are modifying anything in the template to adapt it to your specific needs (such as changing the area’s name or adding more areas to it).
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.