Automation still turns on light even though conditions do not pass

Hi there,

I have an automation set up so that when the doorbell is pressed, it will turn on the porch lights if all the following conditions are met:

  1. It is after sunset
  2. It’s before sunrise
  3. The light is currently off

It’s currently the afternoon (still daylight) and the lights turn on when the doorbell is pressed. Any ideas as to why? Thanks.

Config:

alias: Demo
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.g4_doorbell_doorbell
    to: "on"
condition:
  - condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
      - condition: state
        entity_id: light.porch_light_front_light
        state: "off"
action:
  - service: light.turn_on
    entity_id: light.porch_light_front_light

Hi, welcome to the forum!

You can check the conditions in your automations by clicking on the 3 dots at the right hand side of the condition section to see what the result is of your statement.
That way you can check what goes wrong.

Action still runs (when it shouldn’t)

Use a state condition that the sun is below the horizon instead of the before / after pair.

Strange, have you looked at the traces?
You could use Troon’s suggestion.

Tip: multiple conditions are evaluated as AND anyway.

1 Like

The automation code is not the same as the automation screenshot.

But yeah, anyways, use Troon’s suggestion first. Sun’s “before/after” has been a source of headaches for years

1 Like

Yeah, many misunderstand the point of view for those terms.
They assume it is a rolling point of view, but this caused a ambiguity.
If you want say an automation to run on a Monday after sunset, then, with this view Monday night at 00:00:01 will meet the criterias, but if the sun sets at 18:00:00, then 18:00:01 will also meet the criterias. Two times a day.

This ambiguity is avoided by looking only at a single day from 00:00:00 to 23:59:59.
Here there will only be one sunrise and one sunset.
So before sunrise means from 00:00:00 to actual sunrise and after sunset means from actual sunset until 23:59:59.
Each one will only occur once per day.

But if you look at them again, then you will see that they can not both occor on the same time, so and AND of the criterias will always fail. The correct use would be OR instead.

1 Like