Device tracker issues

Trying to figure out why the script script.at_home are activated even though none of us are at home

home_or_not:
    sequence:
      - service: script.turn_on
        data_template:
          entity_id: >
            {% if is_state('device_tracker.cg', 'not_home') and is_state('device_tracker.lt', 'not_home') and is_state('device_tracker.lt2', 'not_home') and is_state('device_tracker.cg2', 'not_home') and is_state('input_boolean.guests_present', 'off')  %}
              script.not_home
            {% elif is_state('device_tracker.cg', 'home') and is_state('device_tracker.cg2', 'home') and is_state('device_tracker.lt2', 'not_home') and is_state('device_tracker.lt', 'not_home') and is_state('input_boolean.guests_present', 'off') %}
              script.cg_home
            {% elif is_state('device_tracker.lt', 'home') and is_state('device_tracker.lt2', 'home') and is_state('device_tracker.cg2', 'not_home') and is_state('device_tracker.cg', 'not_home') and is_state('input_boolean.guests_present', 'off') %}
              script.lt_home
            {% else %}
              script.at_home
            {% endif %}

Assuming input_boolean.guests_present is off, you have 4 trackers, that’s 16 possible combinations of tracker states. You are testing for 3 combinations.

I’m guessing your catch-all:

 {% else %}
   script.at_home

Is catching one of the 13 untested state combinations.

What are the sates of the 4 trackers? Are cg, cg1, lt and lt1 all ‘not home’?

1 Like

The problem with your code is that it doesn’t account for 1 device tracker being home and 1 not. I see this all the time with my stuff. It’s best to combine them. You can do that with a group.

If you don’t want groups, this template will reduce your trackers. The first 2 lines reduce your double device trackers into a single result, either being True if home or False if not (for CG and LT). The 3rd line is just to shorten the if statements length.

{% set cg_home = [ states.device_tracker.cg, states.device_tracker.cg2 ] | selectattr('state','eq','home') | length > 0 %}
{% set lt_home = [ states.device_tracker.lt, states.device_tracker.lt2 ] | selectattr('state','eq','home') | length > 0 %}
{% set guests_home = is_state('input_boolean.guests_present', 'on') %}
{% if not cg_home and not lt_home and not guests_home %}
  script.not_home
{% elif cg_home and not lt_home and not guests_home %}
  script.cg_home
{% elif lt_home and not cg_home and not guests_home %}
  script.lt_home
{% else %}
  script.at_home
{% endif %}
1 Like

Yes all was state not_home at the time