Script immediately turning off after being run by automation

Brand new to HA…know just enough to screw something up. I’ve found several posts that dance around my issue, but couldn’t find a specific answer.

I have an automation that is triggered by arrival home and is intended to (i) open the garage door anytime; and (ii) turn on garage lights and entry hall lights only if after sunset/before sunrise.

The script runs from the automation, then immediately turns off. See log below. There is no issue with the garage door opening…that part works perfectly.

Here is the AUTOMATION:

alias: Open garage when XXXX or XXXX gets home
description: ''
trigger:
  - platform: zone
    entity_id: person.XXXX
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: person.XXXX
    zone: zone.home
    event: enter
condition: []
action:
  - service: script.entry_lights_on_evening
  - device_id: f7d8852b69c470063ed4bc66e7bdaee9
    domain: cover
    entity_id: cover.double_garage_door
    type: open
mode: single

Here is the SCRIPT:

sequence:
  - condition: sun
    before: sunrise
    after: sunset
  - type: turn_on
    device_id: 966f48877ba6b99d658f2ca667241c68
    entity_id: switch.garage
    domain: switch
  - type: turn_on
    device_id: 77787806bc87c61989d3f3396a18871a
    entity_id: switch.entry_hall
    domain: switch
mode: single
alias: Entry lights on when arriving home after sunset

Both the automation and the script were built in the UI, so this code was created by HA, not written by me. I know it’s something super basic, but in my limited experience I’m at a loss.

Please help!

I can certainly see how that would be confusing! However, turning off a script doesn’t actually do anything in this situation, and will not turn off the lights.

Have you confirmed that the lights don’t actually turn on? Can you run the script manually to confirm (removing the condition if it is the middle of the day for you)?

Correct…the lights do not turn on. I can run the script manually and it works. However, it will not run from the automation. More accurately, it shows that it has run, yet the lights do not turn on.

Here’s the trace from this morning, when I realized it wasn’t working correctly:

Here are the related logbook entries from the same instance this morning:

The problem is the Sunset/Sunrise Condition in the script. Refer to the documentation to understand why that doesn’t work.

Just replace it with this:

  - condition: state
    entity_id: sun.sun
    state: below_horizon

Good catch. Bit weird that this doesn’t work, I just assumed it would since time conditions can span across midnight. Shows me to read the docs closer!

Based on the docs, the Sunrise/Sunset Condition behaves differently from the Time Condition when it involves spanning midnight.

Looks like this did the trick! Thanks so much. I did read the documentation and thought that the addition of both before and after would do the trick, but your solution is even simpler. Thanks again.

1 Like