Good day all. I have now for a few weeks noticed that whenever I use “before sunrise, After sunset” as a condition to make an automation only execute at night then automation does not work. If I use it the other way around for daytime it works fine. Has anyone else experienced this?
before sunrise is from midnight till sunrise
after sunset is from sunset till midnight.
it’s impossible for it to be before sunrise and after sunset at the same time.
This is probably one of the the most common questions from new users. We all went thru it.
You can’t combine both before sunrise and after sunset into a single condition. You have to break them up into two separate conditions inside an OR condition.
Or better still use '{{ state_attr(''sun.sun'', ''elevation'') < 4 }}'
That is just a copy/paste from mine, 4 degrees is good for me, it means the stair lights start turning on a bit before sunset and stop just after sunrise. The number is arbitrary.
Thanks so much. I have fixed the issue
I actually like the above solution best, but before I knew I could do that, I set up some automations based on time before or after sunrise/sunset:
trigger:
- event: sunset
offset: -00:11:00
platform: sun
This works well, and is simple to understand. 11 minutes before sunset, this automation triggers (to turn on a light.)
Another thing I did was to set up two automations, one to turn the light on before sunset, and another to turn it off later. I found this easier to follow than one longer automation with more complex logic to do both on and off. Obviously that’s a personal preference.
was just trying to figure this out, prefect timing, thanks guys.
- id: "1681243461881"
description: "Switch furniture on/off with motion sensor."
alias: Furniture switch motion
trigger:
platform: state
entity_id: binary_sensor.motion_kitchen_occupancy
to: "on"
condition:
- condition: or
conditions:
- condition: sun
after: sunset
after_offset: '-00:30:00'
- condition: sun
before: sunrise
action:
- service: light.turn_on
entity_id: light.furniture_2
- delay:
minutes: 0
seconds: 10
- condition: state
entity_id: binary_sensor.motion_kitchen_occupancy
state: "off"
- service: light.turn_off
entity_id: light.furniture_2
same problem. Not working for me.
Your condition block isn’t properly indented.
Also, be aware that many motion/presence have a period during which they will be “on” even if there isn’t any motion or presence. It is not uncommon for that period to be longer than the 10 seconds your action sequence allows.
I wrote 10 seconds for the test. Normally it is 3 minutes.
Best not to build delays into automations if you can avoid it. Add another trigger.
- id: "1681243461881"
description: "Switch furniture on/off with motion sensor."
alias: Furniture switch motion
trigger:
- platform: state
entity_id: binary_sensor.motion_kitchen_occupancy
to: "on"
- platform: state
entity_id: binary_sensor.motion_kitchen_occupancy
to: "off"
for: "00:03:00"
condition:
- condition: or
conditions:
- condition: sun
after: sunset
after_offset: '-00:30:00'
- condition: sun
before: sunrise
- condition: state
entity_id: binary_sensor.motion_kitchen_occupancy
state: "off"
action:
- service: light.turn_{{ trigger.to_state.state }}
entity_id: light.furniture_2
I added a third condition to cover the situation where the sun rises before the light goes off.
I need light.furniture_2 to turn on for 3 minutes when the binary_sensor.motion_kitchen_occupancy sensor detects motion. But let this happen only when the sun goes down. This automation should not work after sunrise.
I understand that, and my version will do that.
You want the light to go on only when the sun goes down, but off after 3 minutes regardless of where the sun is. Almost all the time, the second trigger I added (light has been on for 3 minutes) will do that on its own. However, there is an edge case where the sun rises during those 3 minutes — without my extra condition, the light would not turn off again.
Actually, my version turns the light off after 3 minutes of no motion. Do you want that, or do you want the light to go off after 3 minutes whether there is motion or not? This version does the second option:
- id: "1681243461881"
description: "Switch furniture on/off with motion sensor."
alias: Furniture switch motion
trigger:
- platform: state
entity_id: binary_sensor.motion_kitchen_occupancy
to: "on"
id: on
- platform: state
entity_id: light.furniture_2
to: "on"
for: "00:03:00"
id: off
condition:
- condition: or
conditions:
- condition: sun
after: sunset
after_offset: '-00:30:00'
- condition: sun
before: sunrise
- "{{ trigger.id == 'off' }}"
action:
- service: light.turn_{{ trigger.id }}
entity_id: light.furniture_2
It didn’t work. Let me explain it like this. This is the hidden led lights of the furniture. I need the led to turn on after sunset, when motion is detected in the kitchen. I don’t want it to burn during the day. Can I explain?
What “didn’t work”? If it’s the condition block, here’s a version using the state of the sun which should be more reliable.
If you’re testing the automation by manually executing it, that won’t work.
This will:
- turn the light on when motion is detected if the sun is down
- turn the light off when it has been on for three minutes.
- id: "1681243461881"
description: "Switch furniture on/off with motion sensor."
alias: Furniture switch motion
trigger:
- platform: state
entity_id: binary_sensor.motion_kitchen_occupancy
to: "on"
id: on
- platform: state
entity_id: light.furniture_2
to: "on"
for: "00:03:00"
id: off
condition:
- condition: or
conditions:
- condition: state
entity_id: sun.sun
state: "below_horizon"
- "{{ trigger.id == 'off' }}"
action:
- service: light.turn_{{ trigger.id }}
entity_id: light.furniture_2
What “didn’t work”? If it’s the condition block, here’s a version using the state of the sun which should be more reliable.
This automation doesn`t apper in Automation list. He probably sees the error so he doesn’t show it in the list.
- There is no problem with the automation file. There are other automations in the same file.
- I have also restarted config.
I’ve just added that exact code to my system, and:
My fault: should have quoted the trigger id
s:
- id: "1681243461881"
description: "Switch furniture on/off with motion sensor."
alias: Furniture switch motion
trigger:
- platform: state
entity_id: binary_sensor.motion_kitchen_occupancy
to: "on"
id: "on"
- platform: state
entity_id: light.furniture_2
to: "on"
for: "00:03:00"
id: "off"
condition:
- condition: or
conditions:
- condition: state
entity_id: sun.sun
state: "below_horizon"
- "{{ trigger.id == 'off' }}"
action:
- service: light.turn_{{ trigger.id }}
entity_id: light.furniture_2
Now it appears — doesn’t work on my system of course, as I don’t have your entities.
Building off @nickrout’s answer, create a slider and an entities card, and you can fine tune your elevation with ease.
Template:
{{ state_attr('sun.sun', 'elevation') < states('input_number.stairs_lights_sun_elevation')|float }}
Card:
type: entities
entities:
- type: attribute
entity: sun.sun
attribute: elevation
- entity: input_number.stairs_lights_sun_elevation
- entity: input_number.bathroom_lights_sun_elevation
title: Sensors