I’m having trouble with my automation turning the lights on when we come from "not_home’ to ‘home’ despite the sunset event.
What I’m trying to do is have the lights come on when we come from ‘not_home’ to ‘home’ but only if its an hour and a half before sunset. And also, have the lights come on if we are already ‘home’ but only if its an hour and a half before sunset. Below is my config.
What is working is if we are already home, the lights come on when they are supposed to, but they are coming on at anytime we go from ‘not_home’ to ‘home’.
- alias: 'Arriving Home Lights On'
hide_entity: false
trigger:
- platform: sun
event: sunset
offset: '-01:30:00'
- platform: state
entity_id: group.caspiller
from: 'not_home'
to: 'home'
condition:
condition: and
conditions:
- condition: state
entity_id: group.caspiller
state: 'home'
- condition: sun
before: sunset
before_offset: '01:30:00'
action:
service: scene.turn_on
entity_id: scene.tv_time
You probably do need some more conditions (e.g., you probably want it to run when you come home after midnight but before some time around sunrise, too.) But the main problem with your current automation is:
- condition: sun
before: sunset
before_offset: '01:30:00'
should be:
- condition: sun
after: sunset
after_offset: '-01:30:00'
Your current condition will only let it run if the time is before an hour and a half after sunset, whereas what you really want is to only let it run if the time is after an hour and a half before sunset.
Thanks for the reply @silvrr and @pnbruckner. I’m going to give @pnbruckner suggestion a try and will report back tonight.
So I’m I understanding that sunset only works up to 11:59pm and then sunrise would take over from 12am until actual sunrise?
The before/after_sunset and then before/after_offset function confuses me, I’m still not sure I fully grasp which applies to what. Feelin like a dummy.
Kind of. Think of it this way. With sun triggers and conditions, they specify periods of a single day (i.e., from midnight to midnight); they do not span midnight (i.e., they don’t go from one day to the next.) So “before sunrise” is from midnight to sunrise; “after sunrise and before sunset” is the period between them; and “after sunset” is from sunset to midnight.
Logically we think “after sunset and before sunrise” is the period between them that spans a midnight. But given the way this works, you can’t specify that directly. If you want to specify the time between sunset of one day and sunrise of the following day, you have two choices…
If you don’t need offsets (i.e., you just want the period exactly between sunset and sunrise), then you can use the state of the sun.sun entity and specify ‘below_horizon’.
But if you need offsets, then you need a condition that is “after sunset, optionally with an after_offset” or’ed with “before sunrise, optionally with a before_offset”. So what you’re really saying is either from sunset to midnight (of one day), or from midnight to sunrise (of the following day.)
So getting back to your automation, just remember, before_offset goes with before, and after_offset goes with after. First decide if you want to specify a period before or after sunrise or sunset. Then apply the corresponding offset if you want the reference point to not be exactly sunrise or sunset, but rather a time relative to them (where a positive number is after the sunrise/sunset event, and a negative number is before the sunrise/sunset event.)
Thank you very much @pnbruckner for taking the time to write out that explination, its very helpful. I guess my hangup is with using after: sunset instead of before: sunset. I thought that since I am trying to create a rule that says, when I’m home and its an hour and a half before sunset, turn on the lights. That wasn’t the case though, you had me change to after: sunset and that worked.
The thing that you might not be grasping yet is the difference between a trigger and a condition. Triggers are events that happen when something changes. They are used to decide when to run automation actions. Conditions, on the other hand, are evaluated when a trigger “fires” and decides if the automation actions will run.
So in your case, both “I’m home” and “a half hour before sunset” are both triggers and conditions. To put that another way, there’s a trigger that determines when it’s an hour and a half before sunset (i.e., offset is -01:30:00), and in this case the “I’m home” condition comes into play.
But there’s also a trigger that determines when you come home, and in this case the condition is not that the time is before an hour and a half before sunset, but rather that the time is after an hour and a half before sunset. The before/after specifies whether you want the period before or after sunset. The before_offset/after_offset determine the offset from sunset (where negative means before and positive means after.)
If it’s still not 100% clear, remember what @silvrr said – you never come home exactly at an hour and a half before sunset. So the condition has to specify a period of time, not an exact moment in time.
Hope that helps. At least, I hope I just didn’t make it foggier again!