Combine 2 actions into 1

Hi All,

Im struggling with Presence. Sometimes geofence or Locative are not sending the URL to turn tracker at ‘home’ or ‘away’.
So I created a input_boolean and a binary_sensor.

Binary_sensor look to state of input_boolean.

Here is the 2 scripts. When geofence tracker state home then input_boolean get state ‘on’ same for locative tracker. Individual they turn on or off the input_boolean. So when 1 of both fails to send the URL then still I have a correct ‘Presence’
Now I want combine them into 1 action based on trigger entity_id. but tried lot of things without success:

- alias: 'System - Peter Left or Arrive GeoFency Manual'
  trigger:
  - entity_id: device_tracker.geofency_peter
    platform: state

  action:
    - service_template: >-
        {% if is_state('device_tracker.geofency_peter', 'home') %} input_boolean.turn_on
        {% else %} input_boolean.turn_off
        {% endif %}
      data:
        entity_id: input_boolean.peter_home_manual

- alias: 'System - Peter Left or Arrive Locative Manual'
  trigger:
  - entity_id: device_tracker.1717fe36_3c70_4599_963c_b70b6ee95eba
    platform: state

  action:
    - service_template: >-
        {% if is_state('device_tracker.1717fe36_3c70_4599_963c_b70b6ee95eba', 'home') %} input_boolean.turn_on
        {% else %} input_boolean.turn_off
        {% endif %}
      data:
        entity_id: input_boolean.peter_home_manual

a OR statement is not the correct one, because it will check then both trackers.

In this example, if either device_tracker changes state, the action will be executed.

- alias: 'System - Peter Left or Arrive Locative or GeoFency Manual'
  trigger:
  - platform: state
    entity_id:
      - device_tracker.1717fe36_3c70_4599_963c_b70b6ee95eba
      - device_tracker.geofency_peter
  action:
  - service_template: >-
      {% if trigger.to_state.state == 'home' %} input_boolean.turn_on
      {% else %} input_boolean.turn_off
      {% endif %}
    data:
      entity_id: input_boolean.peter_home_manual

EDIT
Removed extraneous parenthesis from the template.

Thanks @123, funny I tried to use trigger.to_state.state but didn’t work. Maybe something wrong my side. Let me test this :slight_smile:

edit: Worked!! removed the ) behind the ‘home’

Thanks!

1 Like

I banged that one out quickly and overlooked to remove that parenthesis. I’ve corrected my example above so if others are looking for the solution they won’t get Config Check errors.

Speaking of Solution, if you mark my post as ‘Solution’ (only you, the author of this topic, can do that) it will automatically place a checkmark next to the topic’s title, indicating it contains a solution (instead of what you did, edit the title to include the word ‘SOLVED’).

Never to old to learn every day! Thank you for the off_topic tip

With your script I have a solid solution to have a good presence solution.
Tracker on IP or MAC is also a option but not always good, iPhones go in sleep and disconnect Wifi.