Lights to turn on after sunset, but before 10pm

So I’m trying to use an automation, which turns on light, when there is a movement in a timeframe.
The timeframe should be:
After: Sunset -2 hrs
Before: 10:00 pm

There is one more filter to check if the bedroom tv is off. If it’s on, it means, we’re already in the bed, and I would like another dimmed automation to run then.
For some reason, the Sun state isn’t working as expected, because if I remove that, the automation runs properly.
The code is:

  • id: ‘1537982716650’
    alias: 2. Kitchen afternoon movement lights on
    trigger:
    • entity_id: binary_sensor.motion_sensor_158d0002047cb7
      platform: state
      to: ‘on’
      condition:
    • before: ‘22:00’
      condition: time
    • before: sunset
      before_offset: 02:00:00
      condition: sun
    • condition: state
      entity_id: media_player.bedroom_tv
      state: ‘off’
      action:
    • data:
      entity_id: scene.konyha
      service: scene.turn_on

Anyone any suggestions?

Not sure if they before_offset is the problem, but this is the start of an automation I use to switch my lights on 45 minutes before sunset,

- id: '1526947723366'
  alias: SunSet Lights on
  trigger:
  - event: sunset
    offset: -00:45:00
    platform: sun

The difference is, you use the sunset as a trigger, for me, trigger is the motion sensor.
I’d like to use sunset as a condition.

looking at the example docs, it seems you might have missed out some quotes, so not sure if that is doing it,

condition:
  condition: sun
  after: sunset
  # Optional offset value - in this case it must from -1 hours relative to sunset, or after
  after_offset: "-01:00:00"

I’m not sure it is the problem.
If I edit the yaml file, and then reopen it in the automation editor, the quotes dissappear.
they are removed from the yaml also.

but since you mentioned it, I looked up for the documentation

and probably this is the missunderstood part.
according to this, my configuration should look like this.

  • before: sunset
    before_offset: “-02:00:00”
    condition: sun
- id: '1537982716650'
  alias: '2. Kitchen afternoon movement lights on'
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d0002047cb7
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-02:00:00'
      - condition: time
        before: '22:00:00'
      - condition: state
        entity_id: media_player.bedroom_tv
        state: 'off'
  action:
  - service: scene.turn_on
    entity_id: scene.konyha

Try this.

1 Like

You said you want to trigger AFTER the sunset and BEFORE 10pm

one note here: after sunset but 2 hours before sunset is a bit tricky.
Lets talk numbers here: Sunset is at 19:00. 2 hours before that is 17:00. That means that you want to trigger AFTER 17:00 and before 22:00.

that is why you have to use:
after_offset: ’ -02:00:00’

I see. this ‘and’ is new for me.

  • id: ‘1537982716650’
    alias: 2. Kitchen afternoon movement lights on
    trigger:
    • entity_id: binary_sensor.motion_sensor_158d0002047cb7
      platform: state
      to: ‘on’
      condition:
    • before: ‘22:00’
      condition: time
    • before: sunset
      before_offset: “-08:00:00”
      condition: sun
    • condition: state
      entity_id: media_player.bedroom_tv
      state: ‘off’
      action:
    • data:
      entity_id: scene.konyha
      service: scene.turn_on

I’ve tried this, and worked. (changed to 8 hours just to see if the sun condition works)
If it won’t work, will try your solution. I think the main problem was that I didn’t know, I have to use -02:00:00.

The ‘and’ is something that I like to use when i have multiple conditions. It works by default like that without using it. It is quicker when I want to test automations by just changing it to “or”.
If you had multiple conditions but you only wanted one of them to be true, then you can change the “and” to an “or”.
Good luck with your automation. :wink:

1 Like

If I remember correctly that could be a problem, instead of using after sunset, better use sun elevation.
Because 22:00 is today but 00:01 is other day

The ‘-02:00:00’ state means that it will subtract 2 hours from the sunset hour. It doesnt mean 2AM. :wink:

I am not sure if you can have a sunset at 00:01. Usually the sunset is at around 19:00-20:00
That being said, the automation will always be at the same day.

@argykaraz is on the correct track and his template should work. I have seen issues using ‘and’ at the top level of conditions. One thing to note, the 'and’ed conditions are redundant at the top level. Providing condition with a list of conditions will ‘and’ them all together by default.

- id: '1537982716650'
  alias: '2. Kitchen afternoon movement lights on'
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d0002047cb7
    to: 'on'
  condition:
    - condition: sun
      after: sunset
      after_offset: '-02:00:00'
    - condition: time
      before: '22:00:00'
    - condition: state
      entity_id: media_player.bedroom_tv
      state: 'off'
  action:
  - service: scene.turn_on
    entity_id: scene.konyha
2 Likes

This one solved it.
Thx @argykaraz, @petro!