Template for lighting control when the first person arrives at home

Hello everyone,

I’ve been trying on the templates for a few days.
I have already managed a lot with it, but I fail because of the following task:
I try to turn on the light automatically when a person arrives at home. However, the condition should be that this only happens with the first person.
As a condition, I wrote the following template:

condition:
  - condition: template
    value_template: |-
      {% set personCount = cycler(0, 1, 2) %}
      {% for person in states.person if person.state == 'home' %}
      {% if personCount.current < 2 %}
      {{ personCount.next() }}
      {% endif %}
      {% endfor %}
      {% if personCount.current <= 1 %}
      {{ true }}
      {% else %}
      {{ false }}
      {% endif %}

Now ‘true’ appears in the template editor, which should consider the condition to be met.
However, this template does not continue in automation and automation ends.

Did I miss something? Or is there a function that I have not seen in the references?
Thank you very much for your help!

A simpler template:

{{ states.person | selectattr("state", "eq", "home") | list | count <= 1}}

See if that works for you. And, if it still doesn’t, it’ll be easier to debug and fix.

1 Like

How about putting all people in a group and using the group status - home or not_home - for this?
I only do it with devices (via device_tracker), lights, and switches but it should work for person as well, i.e. one person’s status changes to ‘home’ means the group status changes to ‘home’ and stays ‘home’, no matter how many other persons come and go, until everybody is ‘not_home’ any more.

Thx! That was exactly what I was looking for!

Another question, because I didn’t find anything about it either… can I only filter for lights in an area that are on or off? Unfortunately, all my template attempts did not succeed.

Sorry, I don’t quite get the question. If you want to open a new topic with a full question + your attempt + etc, then I’m sure you’ll get a good response :slight_smile: and I’ll be sure to take a look at it