Loop device_trackers to check for presence

Hi all,

I’m new to the HA business and i can’t wrap my haed around an issue. hope anyone of you can help!
i have a ventilation automation that goes on low when we are not home. (defined 2 fixed device_trackers)
Now i want to add switches (based on device_trackers) so i can control who can trigger the ventilation automation. (for example when we have a babysitter, house cleaning,…)

when running this in the template editor it comes out great

switch:
{% for entity in states.device_tracker %}
  - platform: template
    switches:
      enable_tracking_{{ entity.object_id }}:
        friendly_name: "Enable Tracking {{ entity.name }}"
        value_template: "{{ is_state('device_tracker.' ~ entity.object_id, 'home') }}"
        turn_on:
          service: device_tracker.see
          data:
            dev_id: "{{ entity.object_id }}"
            location_name: "home"
        turn_off:
          service: device_tracker.see
          data:
            dev_id: "{{ entity.object_id }}"
            location_name: "not_home"
{% endfor %}

but then i’m stuck…
added a packages folder and then pasted the code into the yaml file
resulting in

i don’t see the problem
i really hope this can work and someone can point me in the right direction!
thanks!

The Template editor tool does not understand YAML. You cannot use Jinja templating to live “build” YAML configuration.

Share your ventilation automation and explain your goals so we can point you towards a workable solution.

alias: Ventilatie afwezig test
description: ""
trigger:
  - platform: state
    entity_id:
      - group.family_presence
    to: not_home
    id: Empty house
  - platform: state
    entity_id:
      - group.family_presence
    to: home
    id: Full house
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Empty house
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id:
                - switch.sonoff_1001ec9d79_1
      - conditions:
          - condition: trigger
            id:
              - Full house
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.sonoff_1001ec9d79_2
mode: single

the issue right now is that i need to hardcode every member in the presence group

in the groups.yaml

family_presence:
  name: "Family Presence"
  entities:
    - device_tracker.x
    - device_tracker.y

giving it some thoughts it’s maybe not the worst thing to do but the project is all about automating so i wanted to reach the same goal in the code :slight_smile:

how should jinja be used then?

Unfortunately, I don’t think there’s currently an easy way to completely automate this. Maybe if you kept all guests’ devices on a specific network you could use a template sensor to combine that with your regular family members’ presence to get a dynamic house occupancy sensor.

With updates to person entities and zones over the past couple years, the legacy group presence method isn’t the only, or best, method to use. The state of a zone reflects the number of person entities that are in that zone, so you can use a basic Numeric state trigger instead. It would still require some manual maintenance in the UI, but you could set up a Guest person entity and just add all your guests’ device trackers to a single person. Your trigger would then look like:

  - platform: numeric_state
    entity_id: zone.home
    below: 1
    id: Empty house
  - platform: numeric_state
    entity_id: zone.home
    above: 0
    id: Full house

In Home Assistant, configuration consists mostly of key-value pairs. Where supported (as indicated in the documentation by a red “template” as the accepted data type), Jinja templates can be used to provide the values, but not the keys.

Additionally, there are YAML methods like anchors and merges to reduce code repetition if that is your goal.

If I’m reading this right and understanding what you’re trying to do, you want to use device trackers to figure out if there are guests present so that you can prevent (or trigger) certain automations such as shutting down HVAC and similar, do I have that right?

If so, my next question is - do you have a dedicated guest wifi network that guests connect to? If you do, you could create a sensor to query the device trackers for that ssid and create a binary sensor that would show if there are guests present and use that as a condition in your automations.

Let me know if you’d like more info, my next video will actually be on this very topic. Copy is already written & tech bits recorded, planning to shoot the talking head bits tomorrow.

Hi all, thanks for thinking with me. unfortunatly i don’t use a guestnetwork but it’s a good thing to keep in mind. as it is clear to me that making the list, the way i want, is not that common + i don’t have that many guests that will be in house without any of us being there also i’ll stick (for now) hard coding the guest into a group.

thanks for the info!

Rather than trying to hard-code the guest into the group, why not just put a toggle on the dashboard that allows you to manually enable/disable guest mode?

Also, here’s the guest-mode video I mentioned above. Apologies for not coming back earlier to provide this.