Places: please help with triggers in automation templates

testing @tenly’s Places sensor,

I have this automation , which is taken from the example on the components GitHub, and added a few conditions to prevent it from triggering every 30 seconds.

Trying to prevent it form triggering who I am at home and stay home, it still triggers despite my conditions that usually work when using regulate trigger.to_state != trigger.from_state.

Also, I am not sure about the zones condition. without it it triggers constantly because it is moving. I don’t want that, and only signal when in a known zone.

since this is now using an event, I am somewhat puzzled. Plate have a look with me?

  - alias: Locate M
    initial_state: 'off'
    trigger:
      platform: event
      event_type: places_state_update
    condition:
      - condition: template
        value_template: >
          {{ trigger.event.data.entity == 'M places' }}
      - condition: template
        value_template: >
          {{ trigger.event.data.to_state is not none and
             trigger.event.data.from_state is not none and
             trigger.event.data.to_state != trigger.event.data.from_state }}
      - condition: template
        value_template: >
          {% set zones = states.zone | map(attribute='name')|list %}
          {{trigger.event.data.devicetracker_zone in ['home','not_home'] or
             trigger.event.data.devicetracker_zone in zones }}
      - condition: template
        value_template: >
          {{ state_attr('automation.locate_m','last_triggered') != none and
             as_timestamp(now()) | int - 
             as_timestamp(state_attr('automation.locate_m','last_triggered')) 
                 | default(0) | int > states('input_number.presence_timer')|int }}
      - condition: template
        value_template: >
          {{ is_state('input_boolean.notify_presence', 'on')}}
    action:
    - service: notify.ios_phone
      data_template:
        title: >-
          Locate: {{ trigger.event.data.entity }} ({{ trigger.event.data.devicetracker_zone }}) {{ trigger.event.data.place_name }}
        message: |-
          {{ trigger.event.data.entity }} ({{ trigger.event.data.devicetracker_zone }}) 
          {{ trigger.event.data.place_name }}
          {{ trigger.event.data.distance_from_home }} from home and traveling {{ trigger.event.data.direction }}
          {{ trigger.event.data.to_state }} ({{ trigger.event.data.mtime }})
        data:
          attachment:
            url: >
              {{ trigger.event.data.map }}
            hide_thumbnail: false

I’m not sure why you would want to use this component if you only want it to trigger when a device is within a known zone…

The whole reason for this component is to provide information about the location of a person when they are OUTSIDE of any known zones…in the form of a place name or address.

I did put in code that suppresses updates if the device had travelled less than 10m from the previous update. If you want less frequent updates than that, you might try changing that to 50 or 100 - but i don’t think I exposed it as an option so you’d have to modify the component code. However… I think the best way to slow down the update stream (and preserve your devices battery life) would be to throttle down the frequency of updates that your devices send…the mechanism for doing so will depend on the mechanism you use time to send the updates (iCloud, iOS app, OwnTracks, etc)

If you really only care about knowing where a device is when it is within a defined zone, you don’t need a custom component. You can do that out of the box with Device Tracker.

Thanks,

Id only like notifications when in a zone. And not have them when I stay in a zone (as in work in my on office, or watch a movie and receive a notification I am home every minute) :wink:

I don’t need a notification each minute, about 10 meters moved either, not much use when driving is it.

Hence my efforts to set conditions.
since triggering i based on the event I need this:

   condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.state is not none and
             trigger.from_state.state is not none and
             trigger.to_state.state != trigger.from_state.state }}
     #      - condition: template
    #        value_template: >
    #          {{trigger.to_state.attributes.longitude != 
    #              trigger.from_state.attributes.longitude and 
    #            trigger.to_state.attributes.latitude != 
    #              trigger.from_state.attributes.latitude }}
      - condition: template
        value_template: >
          {% set zones = states.zone | map(attribute='name')|list %}
          {{trigger.to_state.state in ['home','not_home'] or
            trigger.from_state.state in ['home','not_home'] or
            trigger.to_state.state in zones or 
            trigger.from_state.state in zones}}
      - condition: template
        value_template: >
          {{ (now() - trigger.from_state.last_changed | default(0)).total_seconds() > 
                                            states('input_number.presence_timer')|int }}

to be rewritten using the event.

Hi Marius,

You misunderstand. The 10m is not a trigger - it’s a filter. By default, the Places sensor updates every time the lat/long provided by deviceTracker changes. Depending on what device/process feeds deviceTracker, that could generate many, many updates for small distance changes - so the places component calculates the change in distance and if it is less than 10m, it ignores the update and waits for the next one. It doesn’t say “send a notification every 10m”, it says ignore any update that is less than 10m from the last update.

The places component is also supposed to ignore any movement within a defined zone. If that’s not happening for you, I’m not sure what’s wrong. I’d suspect that the co-ordinates provided by your device are popping in and out of your zone and maybe increasing the radius of your zone can help with that.

But basically, the 2 issues you are trying to work around with your conditions shouldn’t be happening in the first place - so I’d rather help you try to resolve the root problem than help you work around the issue…

Perhaps - if you enable the debug messages for the places component and then share a log file snippet from a time period during which the frequent notifications while inside a zone occur - I’ll be able to see where the problem lies. It could be a code problem with the component or it could be a configuration problem on your end.

No, I get what your explaining here, thank you. I might have misexpressed myself. What I should have said is with the default automation, as you provide on the repo, the automation is triggered very often.

Probably on all state changes of the set device_tracker.

Your component isn’t unique… most of the device_trackers with basic automations do so, hence the need for extra conditional value_templates, or, even better in combination, with multiple device trackers setups. A group, or a composite device_tracker.

Still, even using that, your automation would trigger very often, while staying put… and most certainly within the radius of the zone (home in this case). It might very well trigger on the changing battery status…

Ill try the debug setting later, when I enable the component again, and most certainly will let you know, thanks for your assistance! Much appreciated.

this is my handmade composite tracker:

device_tracker:
  - platform: mqtt_json
    devices:
      m_presence: location/m



  - alias: M Presence Update from GPS
    trigger:
      platform: state
      entity_id:
      # - device_tracker.telefoon
        - device_tracker.owntracks
        - device_tracker.life360_m
        - device_tracker.homeassistant_app
    action:
      service: mqtt.publish
      data_template:
        topic: "location/m"
        retain: true
        payload: >-
          {
            "latitude": "{{ trigger.to_state.attributes.latitude }}",
            "longitude": "{{ trigger.to_state.attributes.longitude }}",
            "battery_level": {{ trigger.to_state.attributes.battery | int }} ,
            "gps_accuracy": {{ trigger.to_state.attributes.gps_accuracy | int }}
          }
# https://community.home-assistant.io/t/double-trigger-home-not-home/58912/11

  - alias: M Presence Update from Router or Bluetooth
    trigger:
      platform: state
      entity_id:
        - device_tracker.ping_telefoon
        - device_tracker.telefoon_m_bt
        - device_tracker.m
      to: home
    action:
      service: mqtt.publish
      data_template:
        topic: "location/m"
        retain: true
        payload: >-
          {
            "latitude": "{{ states.zone.home.attributes.latitude }}",
            "longitude": "{{ states.zone.home.attributes.longitude }}",
            "battery_level": {{state_attr('device_tracker.life360_marijn','battery')|int}},
            "gps_accuracy": 0
          }