Automation setup with Three conditions not working

Hi all,
I am trying to build an automation that must start when three conditions occur at the same time.
The three conditions are:
Device tracker 1: not home
Device tracker 2: not home
Sun: below_horizon
The goal is to switch on a light for my pets when we are away and the sun is below horizon

This is the code but it doesn’t work

- id: luce_test
  alias: Luce test
  trigger:
  - platform: state
    entity_id: device_tracker.mi_9t_pro, device_tracker.mi_9_pro, sun.sun
  condition:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
  - service: >
      {% if is_state ('device_tracker.mi_9t_pro', 'home') and is_state ('device_tracker.mi_9_pro', 'home') %}
      light.turn_on
      {% else %}
      light.turn_off
      {% endif %}
    target:
      entity_id: light.comodino

I have verified the code spaces/indentation in Visual Studio Code and is ok
Where is the error ?
Thank You for help and sorry for my bad eng

The requirement seems clear so why is your code doing the opposite? It turns on the light when everyone is home, otherwise it turns it off.

      {% if is_state ('device_tracker.mi_9t_pro', 'home') and is_state ('device_tracker.mi_9_pro', 'home') %}
      light.turn_on 
      {% else %}
      light.turn_off
      {% endif %}

Try this version.

  • It is triggered whenever either device_tracker changes to not_home or when the sun sets.
  • It then checks if the sun is below_horizon and both device_trackers are not_home. If they are then the light is turned on.
- id: luce_test
  alias: Luce test
  trigger:
  - platform: state
    entity_id: device_tracker.mi_9t_pro, device_tracker.mi_9_pro
    to: 'not_home'
  - platform: state
    entity_id: sun.sun
    to: 'below_horizon'
  condition:
  - "{{ is_state('sun.sun', 'below_horizon') }}"
  - "{{ is_state('device_tracker.mi_9t_pro', 'not_home') }}"
  - "{{ is_state('device_tracker.mi_9_pro',  'not_home') }}"
  action:
  - service: light.turn_on
    target:
      entity_id: light.comodino

It also handles this case:
Both people leave before sunset. When sunset arrives, the automation is triggered, confirms both device_trackers are not_home, and turns on the light.


NOTE

Your original requirements did not describe how and when you want the light to be turned off. If you explain how you want that part to work, I can help you enhance the automation.


EDIT

Correction. Added missing ) to two conditions.

Taras thak for your help
The light should be on with this three conditions and off if one of the two device tracker is home…at home I manage my light by myself :smiley:

Your solution give me an error

Logger: homeassistant.config
Source: config.py:437
First occurred: 0:00:53 (1 occurrences)
Last logged: 0:00:53

Invalid config for [automation]: Expected a dictionary @ data['condition'][1]. Got None Expected a dictionary @ data['condition'][2]. Got None. (See /config/configuration.yaml, line 9).

How to solve ?

P.S.
You are right I posted the code that I was testing when I was at home that the reason why in the code you found “home” and not “not_home”, but "not_home is the right way

My mistake; the last two of the three conditions were missing a closing ) character. I have corrected the example in my previous post.

Anyway, I will need to redesign it because it only turns the light on, as per your original requirement, not off.

Thank You very much
I’ll wait your redesign

Try this version:

- id: luce_test
  alias: Luce test
  trigger:
  - platform: state
    entity_id: device_tracker.mi_9t_pro, device_tracker.mi_9_pro
  - platform: state
    entity_id: sun.sun
    to: 'below_horizon'
  action:
  - choose:
    - conditions:
      - "{{ is_state('sun.sun', 'below_horizon') }}"
      - "{{ is_state('device_tracker.mi_9t_pro', 'not_home') }}"
      - "{{ is_state('device_tracker.mi_9_pro',  'not_home') }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: light.comodino
    default:
    - service: light.turn_off
      target:
         entity_id: light.comodino

In Italy it’s 15.42 and we are both at home so to test I have put this code, but the light doesn’t switch to on

- id: luce_test
  alias: Luce test
  trigger:
  - platform: state
    entity_id: device_tracker.mi_9t_pro, device_tracker.mi_9_pro
  - platform: state
    entity_id: sun.sun
    to: 'above_horizon'
  action:
  - choose:
    - conditions:
      - "{{ is_state('sun.sun', 'above_horizon') }}"
      - "{{ is_state('device_tracker.mi_9t_pro', 'home') }}"
      - "{{ is_state('device_tracker.mi_9_pro', 'home') }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: light.comodino
    default:
    - service: light.turn_off
      target:
        entity_id: light.comodino

The automation will do nothing until it is triggered by a state-change. Either the sun’s state must change from below_horizonnto above_horizon or the device_trachets must change from not_home to home. If the sun is currently above the horizon and you are both home, the automation will not trigger (something has to change for it to be triggered).

If you simply want to test the action then manually execute the automation. Go to Configuration > Automations, find your automation and click Run Actions. It will skip the trigger and execute the action.

Ok thank You!
I tried activating manually the automation with the last code I posted but the light doesn’t switch on

Go to Developer Tools > States and confirm the state values for the three entities:

  1. sun.sun is above_horizon
  2. device_tracker.mi_9t_pro is home
  3. device_tracker.mi_9_pro is home

If all three have the expected state values and the light still fails to turn on (when you manually execute the automation) then you will have inspect the automation’s trace and see if it reveals why it isn’t working as expected (Automation Debugging is a new feature in version 2021.4) .

The state of the three entities is confirmed, but the automation always choose the default action when I run it manually

What you described is a contradiction (if all three state values match what is in conditions, it will not choose default) so you have an error somewhere in your automation. The automation trace can help you find it.

I know that is a contraddiction, the automation you wrote for me should work but in the debugger I read that the automation choose default

I’m new on Home Assistant and this is my best debug practice


Copy-paste this into the Template Editor. If the sun is currently below_horizon and all device_trackers are home then the reported result of each one of the three templates will be true.

      {{ is_state('sun.sun', 'below_horizon') }}
      {{ is_state('device_tracker.mi_9t_pro', 'home') }}
      {{ is_state('device_tracker.mi_9_pro', 'home') }}

If any of them report false then that’s what causes choose to select default (when you execute the automation manually).

Thank You
I’ll try

I have a problem with the automation when we are at home, the automation switch off the light becouse of default action.
Is there a way to set default “do nothing” ?

If you remove the entire default section then the action will do nothing if the first (and only) conditions statement evaluates to false.

  action:
  - choose:
    - conditions:
      - "{{ is_state('sun.sun', 'above_horizon') }}"
      - "{{ is_state('device_tracker.mi_9t_pro', 'home') }}"
      - "{{ is_state('device_tracker.mi_9_pro', 'home') }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: light.comodino

On a separate note, did you try the conditions in the Template Editor to see what they reported?

The conditions in template editor is all true

Thank You I removed all the default section

If they are all true then if one of the device_trackers changes to not_home and then back to home it will trigger the automation, the conditions in choose will be true and the light will be turned on. If that’s not happening then there is something else happening in your environment that is not apparent.

Don’t forget that without the default section, nothing in this automation will turn the light off.

So should be somenthing like default: trackers not home and sun above horizon turn light off