Issue getting Sun and Time Condition Working

Hi everyone.
I need some assistant. I have looked and tried the suggestions and I am not getting my automation to turn on lights during only the specified time to occur.

I want the lights to turn on at sunset in my bedroom, triggered by a motion detector.
This should only occur during the time at sunset and before 10pm.

This morning the lights came on and it was 7:15am. Here is an example of my code.

## Master Bedroom Turn on Light based on Motion up to 10:00pm

- alias: Turn on Master Bedroom light when there is movement
  trigger:
    platform: state
    entity_id: binary_sensor.masterbdrpir_192
    to: 'on'
  condition:
    condition: or
    conditions: # Or condition used with the sunset and sunrise
    - condition: sun
      after: sunset
      #after_offset: "-00:15:00"
    - condition: time
      before: '22:00:00'
  action:
    service: homeassistant.turn_on
    entity_id: light.masterbedroomlight_98
    data:
      brightness: 100
  1. put your alias in single quotes.
  2. did you build the automation with the automation editor? It can’t handle OR’ed conditions apparently.

Also:

Do you mean between sunset and 10pm (rather than at sunset and before 10pm)?

if so try this:

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

If you want the offset you commented out:

  condition:
    - condition: sun
      after: sunset
      offset: "-00:15:00"
    - condition: time
      before: '22:00:00'

Also, if you want to turn a light on and specify the brightness, you need to use the light.turn_on service instead of the homeassistant.turn_on service. BTW, brightness goes from 0 to 255, whereas brightness_pct goes from 0 to 100.

This isn’t correct. I only use the homeassistant.turn_on / off services in my configuration (because I’m lazy) and it handles all the requests the same.

As for OP - you need an AND condition, not an OR as per @tom_l 's suggestions.

1 Like

Ok, thanks for the correction. I know I’ve seen issues where in some cases homeassistant.xxx doesn’t work the same as domain.xxx, but maybe I’m remembering incorrectly.

Still, calling light.turn_on is more efficient than calling homeassistant.turn_on, because the latter just has to turn around and call the former. But what’s a few milliseconds between friends?! BTW, if your reasoning is laziness, then that doesn’t make sense, because it takes longer to type homeassistant than light. :wink:

Lazy in the sense of can’t be bothered to think about it :wink:

1 Like

Likewise (method and reason).

2 Likes

Yes I wanted the logic to only happen between (sunset and 10pm)
I will put an ‘AND’ and see the outcome.

I did not use the automation editor.

I changed the condition to ‘AND’ and currently it does not turn on now. So I will see after sunset that the lights trigger and i will report back.

Thanks everyone for your input

1 Like

The suggestion of adding a ‘AND’ in my condition seems to work.

1 Like