Trying to use iPhone Activity as a condition, but automation states false

The automation should determine if my wife is walking (not_automotive) when entering the home zone, but the automation stops every time when it hits the condition related to state of the iPhone activity sensor. If she goes walking, the goal is to turn off the alarm, unlock the door and turn lights on for 15 minutes (then turn off). I can see the activity tracker is in a not_automotive state when the automation triggers. I also tried walking state to see if that would help, but no luck. I’ve included the yaml. I am new to HA, so trying to find my way so appreciate the help of others!

alias: Wife Arrive - Night Time - Walking
description: Open garage, turn lights on, turn off alarm, unlock door when wife gets home
trigger:
  - platform: state
    entity_id: person.wife
    to: home
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: state
        entity_id: sensor.wife_iphone_activity
        state: not_automotive
action:
  - service: input_boolean.turn_on
    target:
      entity_id:
        - input_boolean.lock_back_door    (this actually unlocks door via comm to homekit)
  - service: alarm_control_panel.alarm_disarm
    target:
      entity_id: alarm_control_panel.location_alarm
  - service: light.turn_on
    target:
      entity_id: light.back_porch_light
    data:
      brightness_pct: 100
      color_name: white
  - service: light.turn_on
    target:
      entity_id: light.movie_light_fridge
    data:
      brightness_pct: 50
  - delay:
      hours: 0
      minutes: 15
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id:
        - light.movie_light_fridge
        - light.back_porch_light
mode: single

Thanks in advance!

The iOS activity sensor returns a list, so you need to check whether your state is or isn’t in the list. Also, I don’t think you can use not_automotive — there are no negative states. The states will be Automotive or Walking, etc.

Yeah, I’ve tried Walking in this automation and Automotive in the other automation for when she is driving but the automation stops because the activity sensor state condition resolves as false even though the state is Walking in the history log. This is what has me stumped and probably shaking my head once I identify how this should work properly.

Because, what you’re getting is a list.

For example:


value_template: "{{ state_attr('sensor.ceres_activity', 'Types') | first }}"
1 Like

Thanks Pieter,

I have novice knowledge as it relates to templates. I’ve been using HA for about 4 months now, but doing some additional research I believe I understand what you are saying but not sure if this should be incorporated as a condition, a trigger, or another way.

If my understanding is accurate, I believe this should be a condition because there is no True / False result from the value template so it cannot be a trigger. So would I add it to the code as a separate condition or as part of the sensor condition like:

condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: not
        conditions:
          - condition: state
            value_template: "{{ state_attr('sensor.wife_iphone_activity', 'Types') | first }}"
            entity_id: sensor.trudy_iphone_activity
            state: automotive
            attribute: Types

If the template returns automotive, the automation will continue, if not (e.g. Walking, Stationary, Unavailable), the condition will fail and stop the automation.

I appreciate your help as I continue to learn how to use HA.

Edited:

I may have stumbled on a different option that would be treated as a condition using the following:

value_template: "{{ is_state('sensor.wife_iphone_activity', 'Automotive') }}"

Using template in Developer Tools, this template returns a True/False result, tested it using the various types available for the sensor (Walking, Running, Stationary, Unavailable) and it gave a False result, changing to Automotive returned a True result.

Does this make sense?

You’re mixing things a little – and I think I also led you a little astray.

Let’s work through it from one side: You want to trigger on the person state, as you’ve been doing, and then use a condition on the activity (assuming the activity state won’t change as fast as the person state which is usually the case, but just remember the assumption).

In your latest post, you need to use a template condition – you’re mixing a state condition with a template condition. It should be (that comparison at the end leads to a binary value):

      - condition: not
        conditions:
          - condition: template
            value_template: "{{ state_attr('sensor.wife_iphone_activity', 'Types') | first == 'Automotive'}}"

But you can also do:

      - condition: not
        conditions:
          - condition: template
            value_template: "{{ states('sensor.wife_iphone_activity') == 'Automotive'}}"

This is where I probably confused you, and I unfortunately can’t remember why I went for the one over the other. The sensor’s state will be only one value, but it is possible for iOS to think there is more than one activity (and hence there are also confidence levels). In that case, there will be a list of activities under the Types attribute.

The most robust check is probably to do this, checking that the list contains Automotive as an activity:

      - condition: not
        conditions:
          - condition: template
            value_template: "{{ 'Automotive' in state_attr('sensor.wife_iphone_activity', 'Types') }}"

See the Apple developer docs:

For example, if the user was driving in a car and the car stopped at a red light, the update event associated with that change in motion would have both the automotive and stationary properties set to true.

Note that you need to check for Automotive and not automotive, because that is what the Home Assistant values would be – capitalised. Always best to check under HA’s developer tools what the actual state values are. I know this one is counter-intuitive, because normally that actual state will be lower case and when presented on the UI it will be capitalised – that is not the case here.

In my case, because I think it helps with readability and reuse, I define sensors and binary sensors to use in my automations.

Two of my template sensors:

    pieter_activity_type:
      value_template: "{{ state_attr('sensor.ceres_activity', 'Types') | first }}"
      icon_template: "{{ state_attr('sensor.ceres_activity', 'icon') }}"
    pieter_activity_confidence:
      value_template: "{{ state_attr('sensor.ceres_activity', 'Confidence') }}"
      icon_template: >-
        {% if state_attr('sensor.ceres_activity', 'Confidence') == 'High' %}
          mdi:speedometer
        {% elif state_attr('sensor.ceres_activity', 'Confidence') == 'Medium' %}
          mdi:speedometer-medium
        {% elif state_attr('sensor.ceres_activity', 'Confidence') == 'Low' %}
          mdi:speedometer-slow
        {% else %}
          unknown
        {% endif %}

And a template binary sensor for driving:

    pieter_driving:
      friendly_name: "Pieter Driving"
      value_template: >-
        {{ is_state("input_boolean.pieter_driving", "on") }}

Used like this in an automation:

- alias: "Open Garage Door When Arriving By Car"
  initial_state: true
  trigger:
    - platform: state
      entity_id: binary_sensor.pieter_present
      to: "on"
  condition:
    - condition: and
      conditions:
        # prevent a glitch in case the cover was recently closed
        - condition: state
          entity_id: cover.rhs_garage_door
          state: "closed"
          for:
            minutes: 5
        - condition: time
          after: "07:00:00"
          before: "23:00:00"
        # i must be arriving by car
        - condition: state
          entity_id: binary_sensor.pieter_driving
          state: "on"
        # my cover for my car
        - condition: state
          entity_id: binary_sensor.rhs_garage_occupied
          state: "off"
        # proxy for the alarm being off
        - condition: state
          entity_id: binary_sensor.rouve_present
          state: "on"
          for:
            minutes: 5
  action:
    - service: cover.open_cover
      entity_id: cover.rhs_garage_door
2 Likes

Thank you for the detailed response! Very informative and helped me learn more about templates and how I can use them for status and in HA automations!

1 Like

@parautenbach thanks for the explanation. I am also trying to create an automation to open the garage based on entering the home zone and and driving.

I am trying to follow your logic, however, I am a bit lost.You created some condition templates. Where do they go?

I have a file called sensors.yaml where I have created various template sensors.

You can use them in scripts or automations. Refer to the OP’s first post: they’re using an automation.

You can also use the templates to make sensors.

You could make a template binary sensor that’s on when driving. You can then check that in a condition in an automation that triggers when you get home in order to decide whether to open the garage door.

Thanks @parautenbach, please have patience with me as i am still learning all this.

So I created a helper - to create a template binary sensor:

{{ is_state(“input_boolean.wg_driving”, “on”) }}

I set this up to show as “Moving”. Currently as I am stationery it shows “Not Moving”

However I have not created “input_boolean.wg_driving”. What do I need to do there?

As this is very useful maybe a step by step for beginner (in this area) like me would be very helpful.

So what am I trying to achieve? I have an Iphone, and it connects to the car when I drive and the phone focus turns to driving. I would like that when I enter the home zone, and I am driving, it runs my “coming home script”

Would it be too much to ask for a step by step guide?

Sure thing. I’m a little tied up, so need to wait until I can sit down to type longer responses.

Just need to point out that our question is somewhat different from OP’s, so it’s generally better to start a new topic.

Right, so you have a binary sensor like binary_sensor.xxx_focus. You don’t need any templates, I think. You might not even need to use the iOS activity sensor and can just use the focus sensor.

automation:
  - alias: "Open Garage Door When Arriving"
    trigger:
      # https://www.home-assistant.io/docs/automation/trigger/#zone-trigger
      - platform: zone
        entity_id: person.you
        zone: zone.home
        event: enter
    condition:
      - condition: state
        entity_id: binary_sensor.xxx_focus
        state: "on"
    action:
      - service: cover.open_cover
        entity_id: cover.garage

You can also use this as your condition instead:

      - condition: template
        value_template: "{{ 'Automotive' in state_attr('sensor.xxx_activity', 'Types') }}"

Since the focus sensor is binary, it doesn’t explicitly tell you what focus profile is in use, but it might be safe to assume that when the person comes home and focus is on, that it must be the driving mode.

1 Like

@parautenbach I just want to say thanks, I have the automation working as intended.

Now to figure out how to do it in reverse :joy:

Yeah, not sure about that one… :slight_smile: I mean, technically you can do it, yes, but personally, for safety reasons, I won’t drive off until the doors and gates are closed – and if you don’t drive off, you can’t trigger it automatically this way.

1 Like