Light on when someone arrives at home

Learning every day …

in https://community.home-assistant.io/t/how-to-create-when-last-person-leaves-when-first-person-arrives-automation/393860
I found a template sensor to track the number of persons in my home:
This works like a charm:

template:
  - sensor:
      - name: persons_at_home
        icon: mdi:account-supervisor-circle
        state: "{{ states.device_tracker | selectattr('state', 'eq', 'home') | list | count }}"

Now about using it. When someone arrives at home I want to turn on a light.
I guess what I have here won’t be very far off but not working yet:

alias: someone arrives at home
description: ""
triggers:
  - entity_id:
      - sensor.persons_at_home
    trigger: state
conditions:
  - condition: template
    value_template: "{{ (trigger.from_state.state|int(0)) < (trigger.to_state.state|int(0)) }}"
actions:
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: light.kitchen

The trigger is started but the condition evaluated to false when when someone arrived so the poor thing was without a light in the kitchen.
(ah well, it was about noon so not really a problem :wink: )

I hope someone can explain me my problem with this:
Why is my value_template evaluated to false when the count changes from “3” to “4” ?
I can imagine many reasons like: the from_state does not exist …
Is there a way to debug this? (like finding the from_state)

What should I change to get it working? (preferably with a little explanation or a pointer to some doc that explains this a bit)

thanks for your time.

Much easier to use numeric state trigger

trigger: numeric_state
entity_id:
  - zone.home
above: 0

Is that the same as zone.home state?

triggers:
  - entity_id:
      - sensor.persons_at_home
    trigger: state
conditions:
  - condition: template
    value_template: "{{ (trigger.from_state.state|int(0)) < (trigger.to_state.state|int(0)) }}"

You want to trigger when it increases from zero, right?

  - trigger: numeric_state
    entity_id:
    - zone.home
    above: 0

thanks for your reply.
Only thing is that I think that only fires when changing from 0 to anything above 0. I also want it to fire when someone already is at home.

thanks for your replay @busman,
I want it to fire every time someone arrives at home. Not only for the first one but also for any additional person that arrives.

Just set up a trigger for every person arriving home.

automation:
  triggers:
    - trigger: zone
      entity_id: person.paulus
      zone: zone.home
      # Event is either enter or leave
      event: enter # or "leave"

thanks again for your reply @Arh
but given the dynamics in my household that would require adding and deleting triggers over and over again. The idea I found using that template can’t be very wrong. So I was hoping to correct what I did wrong, keeping the trigger as intended. Fire when anyone arrives at home. The persons_at_home helper template counts the number of persons (well, actually their devices that are tracked) pretty good. When that value increases I want an action. I just don’t understand my error there. Is there a way to debug this with more detail?

Ah, sorry. I have the same automation but I assume if someone is already home no need to turn on the light.

Interesting. Is there a risk that your template sensor might pick up states you don’t want to track?

Your approach seems kind of how I’d do it. Or if it is something you want to use in a number of automations maybe have one automation that has the specific people to track and then fire an event that other automations can trigger on?

Why would you need to add and remove triggers all the time? You can add as many triggers as you want to.

I didn’t test it but I believe you need to use float instead of int:

`value_template: "{{ (trigger.from_state.state|float < (trigger.to_state.state|float }}"`

No idea about picking up other events. How can I verify that? When I look at the sensor values I see a history with the expected counts. It also shows who triggered the increment or decrement of the value.
It’s just the template trigger that - in my opinion - should trigger true when an increment is done because someone arrived.
It must be something very simple that I overlooked.

Thist returns 16 for me:

{{ states.device_tracker|selectattr('state', 'eq', 'home')|list|count }}

Doesn’t the automation trace show why it is not running as expected?

Your template counts the number of device_trackers that are home, right? That what zone.home does, so you don’t really need that. (And your template might need to deal with unavailable, too.)

I don’t know, this works for me using a helper:

alias: Person increased
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_number.person_count
conditions:
  - condition: template
    value_template: "{{ trigger.from_state.state|int(0) < trigger.to_state.state|int(0) }}"
actions:
  - action: persistent_notification.create
    metadata: {}
    data:
      message: Home
mode: single

It looks very similar. Your helper is input_number.person_count?
Can you show the yaml code?
When I try to create a template helper with zone.home, I get ‘zone’ undefined.

No, I just created a numeric helper to test the increase condition.

What does this show in dev tools?

{{ states('zone.home') }}

Shows the number of persons instead of the number of trackers. Now I defined the helper as a numeric template helper. What I had showed as character type and not as numbers.
Testing in progress.

I’m not sure I’m adding much here.

Isn’t that what you want? You assign device trackers to people (typically a Companion App phone) and set a home zone and zone.home tells you how many are in that zone.

Not following that. All states are strings, which is why you are converting to int(0) when doing a numeric comparison, as I’m sure you know.

The problem seems to be just the condition is always returning false?

In this case it doesn’t really matter if I count trackers or people. If the number of either increases it will be because someone arrives (assuming the trackers don’t travel alone)

So yes, to false that is returned is my problem. When the count goes from ‘3’ to ‘4’ I hope that the ‘3’ is in the trigger from state and the ‘4’ in the trigger to state. If so, than I also hope that 4 > 3 returns true.

I live with someone who thinks home assistant is watching is their every location so i must determine their presence by their electric use lol

Well, they kind of do… You are checking for them to be “home” but they can become “unavailable” at times, so that would change the count.

That would be expected. You can see those states in the trace, of course.

For a sanity check did you try using a helper like I did?

so much hassle for such a basic thing.
If you use NodeRed or you are willing to look at it, it’s a matter of a single single node. I can provide you with an example.