Help with automation using sun & time conditions, crossing midnight

I’ve been using Home Assistant for a couple of months now, and love it. I’ve written several automations, but I need some help with this one. I’ve been puzzling over this, looking at all kinds of automation examples with time and sunset, and I can’t figure out how to make this work. I want to turn a light on when a motion detector triggers, but only between sunset and 10pm. This is where I am so far:

- alias: Turn on light when motion between sunset and 10pm
  trigger:
    platform: state
    entity_id: sensor.motion_sensor
    to: 'motion detected'
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: time
      before: '22:00:00'
  action:
    service: light.turn_on
    entity_id: light.my_light
    data:
      brightness: 120

It partially works…The problem is that after midnight, when the day changes, the conditions become true again (until sunrise). I do not want the light to turn on between midnight and sunrise. I’m probably missing something obvious, but if someone could help I’d really appreciate it. Thanks in advance!

It’s somewhat of a dirty solution but will work. Just update your code like this.

- condition: time
  before: '22:00:00'
  after: '12:00:00' 

You can make the after time anything bigger than the maximum sunrise time but lower than minimum sunset time at your location .

1 Like

Thank you very much! I never though of doing it this way. I’ll try it out!

You should also be able to change the condition to something like below. I have not tested this so I don’t know for sure if it is the exact syntax.

  - condition: or
    conditions:
      - condition: time
        before: '22:00:00'
      - condition: sun
        after: sunset

I would just like to warm this up, because for me this is not working. What am I doing wrong? My goal is to only trigger the action in case it is in between 30 mins before sunset and before 22:00:00 in the evening.

My code looks like this:

  condition: []
  action:
    - choose:
        - conditions:
            - condition: and
              conditions:
                - condition: time
                  before: 22:00
                - condition: sun
                  after: sunset
                  after_offset: -00:30:00
            - condition: device
              type: is_off
              device_id: 918f0aa482c8e168fe3959a7ca636692
              entity_id: light.osram_light_strip
              domain: light
            - type: is_not_plugged_in
              condition: device
              device_id: 41430627056111eb83f403adf3fd0c80
              entity_id: binary_sensor.sm_g965f_a
              domain: binary_sensor
            - type: is_not_plugged_in
              condition: device
              device_id: 18bb63a0034f11eba24b9dd4dd2ad209
              entity_id: binary_sensor.sm_g965f_b
              domain: binary_sensor
          sequence:
            - service: light.turn_on
              data:
                brightness: 254
                xy_color:
                  - 0.425311978171859
                  - 0.399745377726
              target:
                entity_id: light.osram_light_strip

Specifying ‘after sunset’ and setting a negative offset works pretty nice in another Automation. But combined in this case, the Automation cannot be set up. I also tried it without the inner ‘and’ condition, so just adding those conditions next to the other ones, but it does not work.
In case I remove one of both conditions, it works nicely again. So it seems they are somehow conflicting. When I say ‘before 01:00’ it works again, so it might be something because of midnight, but I don’t know what it is.

Any ideas?

Thanks in advance,
Fridolin

Don’t know why, but after restarting HA it works. Reloading simply didn’t work, so maybe there was an internal hickup. The typical ‘Have you tried turning it off and on again’ worked.

Thanks