I'm stuck: trying to combine conditions 'time:weekday' and 'sun:before sunrise,' no luck

I want certain lights to come on Monday-Friday at 6:30 a.m., but only if the sun is below the horizon.

I’ve read through a large handful of posts*; originally I couldn’t get the automation to not fire on the weekend, but you folks helped me out, thanks**! Unfortunately, every different syntax I’ve tried for combining two conditions has resulted in the sun coming up before 6:30, and the lights coming on anyway.

Here’s what I have currently; I’m hopeful it’s a simple syntax error (I’m relatively new to scripting):

- id: '1571596809000'
  alias: Weekday TEST
  trigger:
  - at: 06:30:00
    platform: time
  - condition:
    conditions:
      - condition: sun
        before: sunrise
      - condition: time
        weekday:
         - mon
         - tue
         - wed
         - thu
         - fri
  action:
  - data:
      brightness: 26
      entity_id: light.office
    service: light.turn_on
  - data:
      brightness: 26
      entity_id: light.console_lamp
    service: light.turn_on
  - data:
      brightness: 102
      entity_id: light.cube_lamp
    service: light.turn_on

Can anyone spot what I’m doing wrong? Thanks in advance!


*Here are the topics I’ve been using as reference:
Trigger at time but only if before sunrise, Why don't this work? (before: sunrise)

**Here is my one-condition script that does fire only M-F:

- id: '1571596809000'
  alias: Weekday TEST
  trigger:
  - at: 06:30:00
    platform: time
  condition:
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
  action:
  - data:
      brightness: 26
      entity_id: light.office
    service: light.turn_on
  - data:
      brightness: 26
      entity_id: light.console_lamp
    service: light.turn_on
  - data:
      brightness: 102
      entity_id: light.cube_lamp
    service: light.turn_on

Try this:

  - condition:
    - condition: sun
      before: sunrise
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
1 Like

You’ve tried to add the condition as a trigger.

Ah, OK, I think I understand, thank you. I need to back condition (and what follows before action) in line with trigger and action, yes?

Yep, that’s right. But you still have to nest everything under condition: and action: properly.

TYTY. So looking at the rest of the nesting/formatting in that automation, here’s what I have:

  trigger:
  - at: 06:30:00
    platform: time
  condition:
  - condition: sun
    before: sunrise
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
  action:
  - data:
      brightness: 26

etc.

:crossed_fingers:


ETA: Sorry @tom_l … I just scrolled up and saw your reply, thank you!

1 Like

Hmm. Even though “sunrise” happened today at 6:26, the lights in the script still came on at 6:30.

The order I have the trigger and conditions (including the time and sun conditions) shouldn’t make a difference, right? I think I read that, as soon as a false condition is reached, the script should terminate. So it should be checking to see if it’s before sunrise, false, terminate. But that isn’t happening.

The only other thing I can think of is to reboot hass.io and hope for the best tomorrow.

Conditions are AND by default, so if those two conditions are not both true when the trigger becomes true, the action won’t fire. Order doesn’t matter in this case since you don’t have any nested condition: and or condition: or conditions.

Check that your location is set correctly in Home Assistant. Also, are you saying that per the logbook in Home Assistant, the sun rose at 6:26? Either way, you could try changing your sun condition to this:

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

The docs warn about using the sunset/sunrise conditions in certain cases. Perhaps one of them applies to you.

2 Likes

I’ve checked and confirmed that my location is set correctly; I also have a sunrise event trigger that shuts off any active lights and switches, which fired at 6:26, so that’s working as expected.

I think my location would be considered pretty “standard,” but since what I have isn’t working, I’ve changed my sun condition to the state condition you suggested, thanks!

Hopefully tomorrow I have good news to report.

Success, Part 1! Lights did NOT come on this morning, thank you again to everyone who has helped with this.

Part 2 will be to see if they do come on in about a week when the sun will be below the horizon still at 6:30.

1 Like

Success, Part 2! Sun still below the horizon at 6:30 on a Monday, lights came on, then 3 minutes later turned off with everything else as they should.

Thank you again everyone for your help with this.

1 Like