I have a seemingly common requirement but cant seem to find any examples of what is needed in the config file. Basically, I have a motion sensor and some lights in my foyer. When motion is detected, turn on the lights. Turn off the lights when no motion for 5 minutes. Here is what I have (and working) so far:
What is missing is the timeframe during which these lights should be turned on. The actions above should only occur between sunset and sunrise. What do I need to add to make the above work this way?
This did not fire last night. The minute I remove the condition the light turns on when motion detected. Unfortunately, it turns on regards of the time.
I checked the Sun values and both the sunrise and sunset times are correct for my area. Is there something wrong in the way the condition is formatted??
BTW: Is there a way to simulate sunset/rise states? Hate to have to wait till tonight to see if what ever changes I make actually work
Just looking at it here it doesn’t seemed to be aligned correctly and I know yaml is very picky. As far as testing you could try adding an offset for the number of hours until current time otherwise I am not aware of another way to simulate it.
Just checking but I also assume you have this in your config file:
'# Track the sun
sun:
Yes, thats included as well as the lat/lon for my location. I just changed the sun condition to a luminance (which I get from the same sensor) condition. That seemed to work.
I will try the offset as you suggested. Who knows. maybe other factors prevented the light from coming on last night
Sorry for being a thread necromancer, but I figured out a solution for HASS 0.44.1 and wanted to post it here in case anyone else was wondering in the future:
I got an automation working for my own purposes with the following OR statement:
# Turn on porch light when I arrive if it's dark outside
- alias: 'Lit porch on arrival'
trigger:
- platform: state
entity_id: device_tracker.xxxxxxxxxxxxx
state: 'home'
condition:
#You can test your automations before and after sunrise by setting your lat/long under "homeassistant:" for an area that is about to have sunrise or sunset (civil twilight). Go to https://in-the-sky.org to learn of an area about to undergo civil twilight, place a marker on the map at this link ( https://in-the-sky.org/location.php? ), then read the lat/long coordinates. Add the coordinates to homeassitant: , then restart HomeAssistant. Clicking on the Sun badge in HomeAssistant will tell you how long until "sunrise" or "sunset." Test your automations now.
#OR statement looks strange but is correct https://home-assistant.io/docs/scripts/conditions/#or-condition
#Using "before: sunset" and "after: sunrise" causes light to come on when arriving during the day.
#"after_offset:" appears to have to be used with "after:" and "before_offset:" appears to have to be used with "before:" since the trigger doesn't fire correctly using "after: ... before_offset: ..." or "before: ... after_offset: ..." in my experience. Positive or negative offset values only seem to matter on the after_offset (but produce the same outcomes for the before_offset).
condition: or
conditions:
#Condition beginning 30 min before sunset (and including after sunset): https://home-assistant.io/docs/scripts/conditions/#sun-condition
- condition: sun
after: sunset
after_offset: "-0:30:00"
#Condition ending 30 min after sunrise (and including before sunrise)
- condition: sun
before: sunrise
before_offset: "-0:30:00"
action:
service: switch.turn_on
entity_id: switch.porch_light
Edits: I attempted to fine-tune this condition, but ended up using the code I posted originally anyway. I have, however, annotated the things I learned about the sun condition in the code’s comments.
Bonus info about testing automations / simulating sunrise/sunset:
Add those coordinates to your configration.yaml 's homeassitant: area, then restart HomeAssistant (Developer Tools > Services > homeassistant , restart > Call Service).
Clicking on the Sun badge in HomeAssistant will tell you how long until the next “sunrise” or “sunset.”
Pretend to be “home” or “not_home” by setting those states for your device tracker (Developer Tools > States > device_tracker.###### > Set State). HASS detects the change in state, so simulate coming home by switching “home” to “not_home”, then switch back to “home”.
- alias: Turn ON Dany's lamp when movement
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d00015a8786
to: 'on'
condition:
condition: state
entity_id: sun.sun
state: 'below_horizon'
action:
service: homeassistant.turn_on
entity_id: switch.dannys_night_light__jackson1
- alias: Turn OFF Dany's lamp after last movement
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d00015a8786
to: 'off'
for:
minutes: 15
action:
service: homeassistant.turn_off
entity_id: switch.dannys_night_light__jackson1
This works perfectly. There seems to be a bug when using the built in automation editor when selecting (after sunset and before sunrise at the same time) I’m guessing that it’s using an AND to check between sunset and sunrise rather than an OR
Im trying to get my lights to turn on at sunset and turn off at sunrise.
But the turn on condition i would like to get it to work with the hue motion/light sensor (lux). Is this going to work?