I would like to create an automation that has as a condition: location of people, “home”, to run or if the location is not “home” the execution is fulfilled at a certain time. i have created next automation but it doesn’t works condition or: for certain hour, please could you take a look:
- id: Auto-off lights
alias: Auto-off lights at 23:30pm
trigger:
platform: time
at: '23:00:00'
condition:
condition: and
conditions:
- condition: state
entity_id: person.jonh
state: home
for:
minutes: 1
- condition: state
entity_id: person.dan
state: home
for:
minutes: 1
condition: or
conditions:
- condition: time
after: '23:50:00'
action:
- data:
entity_id: switch.sonoff_light
service: homeassistant.turn_off
I think you need to read up on some basics. Specifically what the triggers and conditions of an automation mean and do. Also, you need to read more on how to use the and & or conditions (which, understandably, are a bit confusing.)
So think about it this way. Triggers define when the actions of an automation should run. Conditions, on the other hand, decide if the actions will run (when a trigger “fires”.)
So, ask yourself – when do you want to turn off the light? Then compose a trigger or triggers that will watch for those events (i.e., a time happening, an entity changing state, etc.)
Then, when a trigger fires, ask yourself if the light should be turned off at that point regardless of other things, or if it should only be turned off at that point if other conditions are met – e.g., an entity is in a certain state, or has been for a minimum amount of time, etc. Then write a condition or conditions that describe that.
understood
but I am not able to translate to an automatism.
I want the light to go out at 23.00 and as a condition that two people are in the home state. here thr script:
- id: Auto-off lights
alias: Auto-off lights at 23:30pm
trigger:
platform: time
at: '23:00:00'
condition:
condition: and
conditions:
- condition: state
entity_id: person.jonh
state: home
for:
minutes: 1
- condition: state
entity_id: person.dan
state: home
for:
minutes: 1
then if it is not fulfilled that one of the persons are not at home, action must be executed if the time is 23.30pm even though the above is not fulfilled
condition: or
conditions:
- condition: time
after: '23:50:00'
action:
- data:
entity_id: switch.sonoff_light
service: homeassistant.turn_off
You’re trying to use a condition as a trigger. Also you’re not using the or condition properly, even if using a condition was correct in this case, which it isn’t.
Take a step back. There are two times when the light should be turned off, not one. So you need two triggers. One is time changing to 23:00:00, and the other is time changing to 23:30:00. Those are when you want to turn the light off.
The first trigger (i.e., 23:00:00) needs a condition, because at 23:00:00 the light should be turned off only if both people have been home for at least a minute at that point.
The second trigger (i.e., 23:30:00) doesn’t need a condition, because the light should be turned off at that time regardless.
The easiest way to implement this is with two automations. Here’s the first:
- id: 1
alias: Auto-off lights at 23:00 if home
trigger:
platform: time
at: '23:00:00'
condition:
- condition: state
entity_id: person.jonh
state: home
for:
minutes: 1
- condition: state
entity_id: person.dan
state: home
for:
minutes: 1
action:
service: homeassistant.turn_off
entity_id: switch.sonoff_light
Here’s the second:
- id: 2
alias: Auto-off lights at 23:30
trigger:
platform: time
at: '23:30:00'
action:
service: homeassistant.turn_off
entity_id: switch.sonoff_light
When it’s appropriate. There are many cases where that would be needed.
This could be done in one automation. But in this case the extra complexity probably doesn’t make it worth the effort, and would require the use of more advanced techniques. You should focus on the basics first.