Using OR in service template not possible? ...or I lack the knowledge of the correct syntax... ;-)

I was hoping to do something like this to trigger the script when either condition was met, e.g. me OR my wife is coming home. unfortunately it don’t pass the syntax test…

    - service_template: >
        {% if is_state('device_tracker.pal_presence', 'home') OR is_state('device_tracker.eva_presence', 'home') %}
          script.adults_coming_home
        {% endif %}

I ended up doing this (below), although this seems a bit overkill. How can I accomplish triggering the script if either om us is at home?

    - service_template: >
        {% if is_state('device_tracker.pal_presence', 'home') %}
          script.adults_coming_home
        {% endif %}
        {% if is_state('device_tracker.eva_presence', 'home') %}
          script.adults_coming_home
        {% endif %}

The or operator is case sensitive:

    - service_template: >
        {% if is_state('device_tracker.pal_presence', 'home') or is_state('device_tracker.eva_presence', 'home') %}
                                                              ^^
                                                         'Lower case'

However you have no else statement so the service could end up being empty and generate an error. You would be better off using the choose action.

    action:
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ is_state('device_tracker.pal_presence', 'home') or is_state('device_tracker.eva_presence', 'home') }}"
            sequence:
              - service: script.adults_coming_home
      - other actions here if you have them.

This could also be written without templates (but is considerably more verbose):

    action:
      - choose:
          - conditions:
              - condition: or
                conditions:
                  - condition: state
                     entity_id: device_tracker.pal_presence
                     state: home
                  - condition: state
                     entity_id: device_tracker.eva_presence
                     state: home
            sequence:
              - service: script.adults_coming_home
      - other actions here if you have them.

If you don’t have other actions after the call to the script, I think you can simplify it to this, but have seen people have issues with using a condition as the first action:

    action:
      - condition: template
        value_template: "{{ is_state('device_tracker.pal_presence', 'home') or is_state('device_tracker.eva_presence', 'home') }}"
      - service: script.adults_coming_home

If that is the case you could fudge it with:

    action:
      - delay: '00:00:01'
      - condition: template
        value_template: "{{ is_state('device_tracker.pal_presence', 'home') or is_state('device_tracker.eva_presence', 'home') }}"
      - service: script.adults_coming_home

The other option is to use an automation using either a template trigger or two state triggers, and the script (or it’s contents) as the action.

2 Likes

Thank you, @tom_l for a throughoutly answer, as always :wink:

For what it’s worth to others; Here is my entire automation

automation:
- alias: '[House] Family member coming home'
  trigger:
    - platform: state
      entity_id:
        - device_tracker.pal_presence
        - device_tracker.eva_presence
        - device_tracker.sofie_presence
        - device_tracker.andrea_presence
      to: 'home'
  action:
    - service: notify.adults
      data_template:
        title: chezENGH smarthus
        message: "{{ trigger.to_state.attributes.friendly_name }} came home at {{now().strftime('%H:%M')}}"
    - choose:
        - conditions:
            - condition: template
              value_template: "{{ is_state('device_tracker.pal_presence', 'home') or is_state('device_tracker.eva_presence', 'home') }}"
          sequence:
            - service: script.adults_coming_home

Because there are no other actions you can simplify it to this:

automation:
- alias: '[House] Family member coming home'
  trigger:
    - platform: state
      entity_id:
        - device_tracker.pal_presence
        - device_tracker.eva_presence
        - device_tracker.sofie_presence
        - device_tracker.andrea_presence
      to: 'home'
  action:
    - service: notify.adults
      data_template:
        title: chezENGH smarthus
        message: "{{ trigger.to_state.attributes.friendly_name }} came home at {{now().strftime('%H:%M')}}"
    - condition: template
      value_template: "{{ is_state('device_tracker.pal_presence', 'home') or is_state('device_tracker.eva_presence', 'home') }}"
    - service: script.adults_coming_home
1 Like

Oh damn it! I’ve been wrecking my brain with helpers to activate other helpers and “or” works.

Thanks!

Now I can change all my automations again :smiley: