Automation if more then X lights are on, send notification

What could be a code?

For example if X is 3 bulbs/switches but I have 10 bulbs/switches (hence I cannot put them in a group since is random WHICH bulbs have to count)?

This web site might be able to help, have a look at the for loop -

http://jinja.pocoo.org/docs/2.9/templates/

thanks, but I do not even know how to start …

@balloob Just posted a script to count number of people that are home. You can modify the same to count the number of lights that are on and trigger an automation.

Possibly similar to this…

     {%- for i in states.light if i.state == "on" -%}
        {{ i.attributes.friendly_name }} is ON.
      {%- endfor -%}

You should be able to change the middle line to trigger a script or maybe a shell command or a notify maybe?

You could try to use the loop.last variable by using an if condition on a for loop in the jinja2 templates. For
instance, you could put something like this in a template trigger

{% for e in states.group.target_lights_group.attributes.entity_id if e.state == ‘on’ %}
{% if loop.last %}
{% if loop.index > X %}
true
{% else %}
false
{% endif %}
{% endif %}
{% endfor %}

or, you could use the new python_script component in a similar way to the example below:
https://community.home-assistant.io/t/count-people-that-are-home/20184

Mmhh I can adapt that, but how does all integrate in hass…

New to coding

I modified the loop by @tiktok7 and it gives the number of lights that are on:

{% for state in states.light if state.state == 'on' %}
{% if loop.last %}
{{loop.index}}
{% endif %}
{% endfor %}

You should be able to use that in a template sensor or a template trigger.

1 Like

Thanks a lot.

Some of my light are light.xxxx (hue smart bulbs and yeelight) and some are switch.xxxx (smart switch connected to a dumb light).

How to add the switches?

You can create another similar sensor for switch for now. May be there is an easy way to loop over both light and switch, but I am not sure how to do that.

you could create a group all.lights and put all lights and switches you want to track into it. this should count the items that are on:

{% for item in states.group.all_lights.attributes.entity_id  if states(item) == "on" %}
{% if loop.last %}
{{loop.index}}
{% endif %}
{% endfor %}
1 Like

This is exactly what you need :thumbsup:

I never did that, hope I can manage looking the instructions

this is how mine looks like. i also created a group with all the switches and lights under entity_id, but without putting them into the template sensor, it won’t update properly.

sensor:
  - platform: template
    sensors:
      counter_lights_on:
        unit_of_measurement: 'Lights'
        friendly_name: 'Lights on'
        entity_id:
          - light.ix_seine_hue
          - light.schreibtischlampe
          - switch.stube_buecherschrank
          - switch.stube_billy
          - light.kugellampe
          - switch.flur_flutlicht_template_switch
          - switch.bad_nachtlicht
          - light.bad
          - light.waschbecken
          - switch.kueche_deckenlicht
          - light.kuechentisch
          - light.kuechenspuele
          - light.schreibtisch
          - light.serverraum
        value_template: >-
          {%- for item in states.group.alle_lichter.attributes.entity_id  if states(item) == "on" %}
            {% if loop.last %}
              {{loop.index}}
            {% endif %}
          {% endfor %}
3 Likes

Mmhh I don’t get it. Where do you put, for example, that 5+lights are on?

And what counts it?

I mean I need to send a notification if 5+ light/switches are on. How to do this?

sensor.counter_lights_on would have the # of lights on.

what would be an automation?

automation:
  trigger:
    platform: numeric_state
    entity_id: sensor.counter_lights_on
     above: 5
 - service: notify.claudio_zanzito
    data_template:
      message: 'Too many lights on'

how to put the number of lights on in the notification?

Also if I want to query with mqtt the number of lights on, how to do it?

also if I want to add the switcvhes, do I need to change this?

states.group.all_lights.attributes.entity_id

I create a group with all lights and switches, and put that?
group.all_lights_and_lightsswitches

have not tested it, but putting the number of lights in your notification text might work like this:

 - service: notify.claudio_zanzito
    data_template:
      message: '{{ trigger.to_state.state }} lights on'

this should definitely work:

 - service: notify.claudio_zanzito
    data_template:
      message: '{{states.sensor.counter_lights_on.state}} lights on'

sorry, i don’t think i understand the question.

just add all entities from which you want to count the on-state into the group.all_lights. if you like the name group.all_lights_and_lightsswitches better, use that and change the template sensor accordingly.

1 Like