Trying to create automations with conditions, if my condition does not pass the automation should not run?

based on my location and time the following condition does not pass. however the automation still works when I test it by physically opening and closing the door.

alias: Downstairs Hallway Closet Light Control
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.downstairs_hallway_closet_door_sensor_contact
    from: "off"
    to: "on"
    id: DoorOpen
  - platform: state
    entity_id:
      - binary_sensor.downstairs_hallway_closet_door_sensor_contact
    to: "off"
    id: DoorClosed
    from: "on"
condition:
  - condition: sun
    after: sunset
    before: sunrise
    after_offset: "-00:30:00"
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - DoorOpen
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.downstairs_hallway_light_4
            data: {}
      - conditions:
          - condition: trigger
            id:
              - DoorClosed
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.downstairs_hallway_light_4
            data: {}
mode: single

Not sure if this will help, but I have a couple of these and I noticed the sunset/sunrise is the other way round, yours is…

from: "on"
condition:
  - condition: sun
    after: sunset
    before: sunrise
    after_offset: "-00:30:00"
action:

Mine are…

from: "on"
condition:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: "-00:30:00"
action:

Worth a try?

Still runs

Based on the trace it shouldn’t?

Make two automations. One for open, one for closed.

Complex, multitrigger, multicondition automations are difficult to write and difficult to diagnose.

1 Like

The trace shows the automation was triggered, evaluated its condition, and did not execute its actions (because the condition evaluated to false). It is working nominally, exactly like you designed it to work.

A trace is produced when an automation is triggered and processes its conditions (if any). Your automation is working properly because it didn’t execute its actions (because the current time isn’t between sunset and sunrise).

I’m not sure what you mean by “Still runs” but it obviously has to “run” to process its trigger and condition.

Your original automation can be significantly simplified / shortened:

alias: Downstairs Hallway Closet Light Control
trigger:
  - platform: state
    entity_id: binary_sensor.downstairs_hallway_closet_door_sensor_contact
condition:
  - condition: sun
    after: sunset
    before: sunrise
    after_offset: "-00:30:00"
action:
  - service: light.turn_{{ trigger.to_state.state }}
    target:
      entity_id: light.downstairs_hallway_light_4
1 Like

I’m new to HA. Your code reads what the current state of the device is and will perform the opposite of the current state?

For example, the light is off. Once the automation is triggered HA will read the off state and perform the opposite of that state?

Is using the trigger better than calling a service? I just learned I should call a service for entity actions I want performed in automations.

The automation is triggered by a change to the binary sensor.

The trigger variable contains both the prior and new state objects of the entity that caused the trigger.

In this case, {{ trigger.to_state.state }} will be on or off, matching the new state of the binary sensor.

So the service call will be light.turn_on or light.turn_off.

When you say “still works” do you mean:

  1. The automation turned on the light when you opened the door even though the sun had not set yet?

  2. The automation did not turn on the light when you opened the door but still created a trace file.

#1 would be abnormal whereas #2 is normal.