Automation with OR condition returning error 500

I created an automation that turns on a light if my wife or I am at home (by determining the location of the mobiles).

Although I used the trace tool, I still cannot make it work. I used the following example form Conditions - Home Assistant

condition:
  alias: "Paulus home OR temperature below 20"
  condition: or
  conditions:
    - condition: state
      entity_id: "device_tracker.paulus"
      state: "home"
    - condition: numeric_state
      entity_id: "sensor.temperature"
      below: 20

And hereā€™s my code:

condition: 
  alias: "Gime o Jose en casa"
  condition: or
  conditions:
    - condition: device
      device_id: TheId
      domain: device_tracker
      entity_id: device_tracker.samsung_s20fe
      type: is_home
    - condition: device
      device_id: TheId
      domain: device_tracker
      entity_id: device_tracker.moto_g_8
      type: is_home

Any ideas?

The examples in the documentation are indented differently to yours (the leading dash ā€˜-ā€™ counts)
Docs : -

    condition:
      - condition: or
        conditions:
          - condition: numeric_state
            entity_id: sun.sun
            attribute: elevation
            below: 4
          - condition: numeric_state
            entity_id: sensor.sensorluz_7_0
            below: 10

Yours : -

condition: 
  alias: "Gime o Jose en casa"
  condition: or
  conditions:
    - condition: device
      device_id: TheId
      domain: device_tracker
      entity_id: device_tracker.samsung_s20fe
      type: is_home
    - condition: device
      device_id: TheId
      domain: device_tracker
      entity_id: device_tracker.moto_g_8
      type: is_home

Note quite sure why you have ā€œcondition:ā€ right at the top either - is that just a title ? As otherwise itā€™ll screw up your yaml

Iā€™d try : -

    condition:
    - condition: or
      conditions:
        - condition: device
          device_id: TheId
          domain: device_tracker
          entity_id: device_tracker.samsung_s20fe
          type: is_home
        - condition: device
          device_id: TheId
          domain: device_tracker
          entity_id: device_tracker.moto_g_8
          type: is_home

But I canā€™t be certain a) as you are using device_idā€™s (why ?) b) you seem to be mixing both device_idā€™s and entity_idā€™s (never seen that before.
You should really post the whole automation as itā€™s difficult to check spacing with just a snippet

I will try your suggestion and reply back.

Youā€™re right, but it was created that way when I used the UI the first time I created it :man_shrugging:

This worked:

alias: Luces fondo
description: ''
trigger:
  - platform: sun
    event: sunset
condition:
  alias: "Gime o Jose en casa"
  condition: or
  conditions:
  - condition: device
    device_id: 07e59e90c291b9f61e0cc7853918864a
    domain: device_tracker
    entity_id: device_tracker.samsung_s20fe
    type: is_home
  - condition: device
    device_id: 3ad1053fe9ed4110928ca7fe2f667468
    domain: device_tracker
    entity_id: device_tracker.moto_g_8
    type: is_home  
action:
  - type: turn_on
    device_id: efb34a4ada843c69d7e9aa1f7c400a78
    entity_id: switch.luces_fondo
    domain: switch
  - delay:
      hours: 3
      minutes: 0
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: efb34a4ada843c69d7e9aa1f7c400a78
    entity_id: switch.luces_fondo
    domain: switch
mode: single

Not sure why, but ā€œdevice_idā€ is required

Just for kicks try this : -

alias: Luces fondo
mode: single
max_exceeded: silent
trigger:
  - platform: sun
    event: sunset
condition:
  alias: "Gime o Jose en casa"
  condition: or
  conditions:
  - condition: state
    entity_id: device_tracker.samsung_s20fe
    state: 'home'
  - condition: state
    entity_id: device_tracker.moto_g_8
    state: 'home'
action:
  - service: switch.turn_on
    entity_id: switch.luces_fondo
  - delay: '03:00:00'
  - service: switch.turn_off
    entity_id: switch.luces_fondo

This is much shorter, easier to read and should do exactly the same thing

I would point out that (say) the sun sets at 19:03 and your wife comes home at 19:10 and you come home at 19:15 then the (background) lights will not come on, is that what you really want ?

Edit: AND (more importantly) wonā€™t go off when you expect them to (or at all if you put them on manually).

Will give it a try, thanks!

Yes, because I donā€™t want the lights to be turned on when weā€™re on vacation for instance.

Yeah, but in that case youā€™d just have an input_boolean (Vacation) that youā€™d use throughout your Automations (another condition) to stop the ones you donā€™t want in ā€œthat circumstanceā€. (As opposed to groping around in the dark)

Edit: No, Iā€™m being stupid. The whole point of this automation would be ā€œif you were homeā€.
Hence the : -

  condition: or
  conditions:
  - condition: state
    entity_id: device_tracker.samsung_s20fe
    state: 'home'
  - condition: state
    entity_id: device_tracker.moto_g_8
    state: 'home'
1 Like

This could be improved further : -

binary_sensor:
  - platform: template
    sensors:
      ## Luces Fondo
      bs_luces_fondo:
        value_template: >
          {% set offtime = as_timestamp(state_attr('sun.sun', 'next_setting')) + timedelta(hours = 3) | timestamp_custom('%H:%M') %}
          {% set time = states('sensor.time') %}
          {% set pm = time >= '12:00' %}
          {% set gimeencasa = is_state('device_tracker.samsung_s20fe', 'home') %}
          {% set joseencasa = is_state('device_tracker.moto_g_8', 'home') %}
          {% set sunbelow = is_state('sun.sun', 'below_horizon') %}
          {{ pm and (time <= offtime) and sunbelow and (gimeencasa or joseencasa) }}
        icon_template: "{{ 'mdi:lightbulb' if is_state('binary_sensor.bs_luces_fondo', 'on') else 'mdi:lightbulb-outline' }}"
        friendly_name: Luces Fondo

automation:
  ## Luces Fondo Automation
  - alias: au_luces_fondo
    mode: single
    max_exceeded: silent
    trigger:
      - platform: state
        entity_id: binary_sensor.bs_luces_fondo
    action:
      - service_template: switch.turn_{{trigger.to_state.state}}
        entity_id: switch.luces_fondo

This is a binary_sensor and an automation (they need to go in their respective locations for your setup).
The binary sensor checks that -

  1. sun is below the horizon
  2. the time is after 12:00 (noon)
  3. that jose or gime are ā€˜homeā€™
  4. the time is before 3 hours after sunset

If all true the automation switches the light on else it is turned off (Manual Switching will overide this but the automation will ā€˜catch-upā€™ the next time it switches to the opposite state)

My issue with this is : -
a) it may tend to be an indicator to a burglar to show you are NOT at home
b) sunset at my location varies by 04:51:44 through the year (but thatā€™s UTC variance, with DST itā€™s more, so 05:51:44 (I hate DST) ) Thatā€™s quite a variance so Iā€™d probably replace the ā€˜offtimeā€™ with a fixed time (say 22:00 ???) if so Then change ā€˜thisā€™ to ā€˜thatā€™ below : -

          {% set offtime = as_timestamp(state_attr('sun.sun', 'next_setting')) + timedelta(hours = 3) | timestamp_custom('%H:%M') %}

          {% set offtime = '22:00' %}

This is quite an advanced technique but it combines multiple states in an ā€˜easy to readā€™ way and creates a compact automation thatā€™s about as efficient as you can get.

Edit: The above requires both sun.sun to be set up and ALSO the time and date sensors to provide (at least) sensor.time