How to send a notification if certain number of lights is on

Hi,
how to trigger an automation if number of lights on exceeds certain number. My code is like this, but its not starting automation. What is wrong?

alias: "Notify: Too many lights on"
description: ""
trigger:
  - platform: template
    value_template: >-
      {% if expand('light.all_lights')|selectattr('state','eq','on')|list|count
      %}>4{% endif %}
condition: []
action:
  - service: notify.mobile_app_martinov_iphone_13_pro
    data:
      message: too many lights on
mode: single

Template triggers need to evaluate to true or false…

alias: "Notify: Too many lights on"
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ expand('light.all_lights') 
      | selectattr('state', 'eq','on') | list | count > 4 }}
condition: []
action:
  - service: notify.mobile_app_martinov_iphone_13_pro
    data:
      message: too many lights on
mode: single

You can also do it without the expand(), by using the is_state test in a select() instead:

{{ state_attr('light.all_lights', 'entity_id') 
| select('is_state', 'on') | list | count > 4}}

Excellent, thank you.

What would be a proper syntax to compare number of lights against number of people at home?
I am counting people using this {{ states('zone.home') | int }}

would this work?

value_template: >-
      {{ expand('light.all_lights') 
      | selectattr('state', 'eq','on') | list | count >= states('zone.home') | int  }}

It works, fine, but I have another question.

Is it possible to add time condition like with normal automation, e.g. if template condition is true for 5 minutes, only than trigger the action?

The method I usually use for that is to transition the template trigger to a template binary sensor. This will create an entity that has its own last_changed property, allowing you to use the for variable in a state trigger.

The binary sensor configuration would look like:

template:
  - binary_sensor:
      - name: More lights than people
        state: >
          {{ state_attr('light.all_lights', 'entity_id') | select('is_state', 'on') 
          | list | count >= states('zone.home') | int  }}
        availability: "{{ states('light.all_lights') not in ['unknown', 'unavailable', none] }}

And the automation would change to:

alias: "Notify: Too many lights on"
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.more_lights_than_people
    to: 'on'
    from: 'off'
    for: "00:05:00"
condition: []
action:
  - service: notify.mobile_app_martinov_iphone_13_pro
    data:
      message: too many lights on
mode: single

Thanks. In the meantime I tried to add code like this directly into template triggered automation and it seems to work as well

platform: template
value_template: >-
  {{ expand('light.all_lights')  | selectattr('state', 'eq','on') | list | count
  >= states('zone.home') | int  }}
for:
  hours: 0
  minutes: 1
  seconds: 0

:man_facepalming:

Now that you mention it, I remember someone showed me that a while ago. It’d be nice if my brain would remember these thing when they could be helpful…