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
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 }}
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
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…