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.
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
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
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.
Go to Developer Tools > States and confirm the state values for the three entities:
sun.sun is above_horizon
device_tracker.mi_9t_pro is home
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) .
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.
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.
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 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.