Trying to create a Light Timer active between extended SunRise/Set

My goal,… is to extend the switch on-time for the light by 30mins either side of SunRise/Set, I have tried more variations than I care to include here,… without any real success.

my visual editor looks as follows:-

Giving a yaml that looks as follows:-

alias: HallWay Lights with SR/SS offset
description: My 1st Delayed Motion detect
triggers:
  - trigger: event
    event_type: timer.finished
    event_data:
      entity_id: timer.side_light_timer
    id: Timer Finished
  - type: no_motion
    device_id: 51276afec065e74ba8e4add88f8d41ce
    entity_id: 4a0e089f8276b1cb7e3a41b857c1e36a
    domain: binary_sensor
    trigger: device
    id: Motion Stopped
  - type: motion
    device_id: 51276afec065e74ba8e4add88f8d41ce
    entity_id: 4a0e089f8276b1cb7e3a41b857c1e36a
    domain: binary_sensor
    trigger: device
    id: Motion Dectected
conditions:
  - condition: sun
    before: sunset
    before_offset: "00:30:00"
  - condition: or
    conditions:
      - condition: sun
        after: sunrise
        after_offset: "00:30:00"
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion Stopped
        sequence:
          - action: timer.start
            target:
              entity_id: timer.side_light_timer
            data: {}
      - conditions:
          - condition: trigger
            id:
              - Motion Dectected
        sequence:
          - action: timer.cancel
            data: {}
            target:
              entity_id: timer.side_light_timer
          - type: turn_on
            device_id: 37ef7b19e1af5f92dd1f50279efcd647
            entity_id: 40495e6d13e574f3e84e365b46558079
            domain: light
      - conditions:
          - condition: trigger
            id:
              - Timer Finished
        sequence:
          - type: turn_off
            device_id: 37ef7b19e1af5f92dd1f50279efcd647
            entity_id: 40495e6d13e574f3e84e365b46558079
            domain: light
mode: single

What am I missing,… should I be just using one condition,… I am not sure…
Many tx

I don’t mess with timers, instead I just define a template binary sensor with an “off” delay for example:

template:
  - binary_sensor:
      - name: "Sensor Group Hall Delayed"
        unique_id: sensor_group_hall_delayed
        device_class: occupancy
        delay_off:
          minutes: 3
        state: >
          {{ states("binary_sensor.sensor_group_hall") }}

I use this so that whenever a sensor in the hall detects motion the light comes on for 3 minutes (after the last motion detected) then goes off.

To limit this to time of day** I would add an additional condition in my automation so that the light wouldn’t turn ON during the day - I am fine with it turning OFF at any time.

** - I use a manual “house mode” that I set to “day” when I get up, since I don’t have a fixed schedule, but the effect is the same.

Its been a while since I worked with sunset and sunrise, but that line I read as it should only work after sunrise, so the automation would work during the day and not the night.
Is that what you want?

Same thing goes for your sunset line.

Also avoid using device_ids.
Use entity_ids instead.

No no,… your question is quite valid,…
I want the automation to turn the light on (enable) before sunrise,… and after Sunset,…
But to extend each 30 Mins… ie end later than Sunrise, but to start earlier than sunset…
Thankyou for pointing out the obvious…

PS,… why use entity_ids rather than device_ids?? for my education


So,… Do I ‘logically’ read this statement as,…

Sun, condition Active , Before Sunrise, ( but offset ) by 30Mins
And
Sun condition Active , After Sunset, ( But offset ) by 30mins

… or do I need to create / write it as two statements to use an ‘or’ function

Thankyou for your help

Its the second point in the automations section.

I am not that much into the GUI part, but as I see it.

If sunrise is at 06:00 then the first part will be true until 06:30.

If sunset is at 18:00, then the second part will be true until 18:30.

I think the offset can be negative too, so -00:30:00 might what you want in the fields for after sunset.

Based on your description you want:

      - condition: sun
        before: sunrise
        before_offset: "0:30"
        after: sunset
        after_offset: "-0:30"

For the sake of this explanation lets say that:

  • The sun rises at 06:00 (6:00 AM)
  • The sun sets at 18:00 (6:00 PM).

With no offsets it’s pretty clear, you want to allow the light to turn on:

  • Before Sunrise.
  • After Sunset.

The after_offset is pretty clear too:

  • If you set a positive value (0:30) it would not allow the lights to turn on until 18:30
  • If you set a negative value (-0:30) it would not allow the lights to turn on until 17:30 (5:30 PM).

The before_offset is confusing, but it actually work the same way as the after_offset, hence:

  • If you set a positive value (0:30) it would allow the lights to turn on until 06:30
  • If you set a negative value (-0:30) it would allow the lights to turn on until 05:30
1 Like

David,… Many tx for explanation,… Trying to understand and second guess if you have it correctly configured can be a little painful,… but when it is verbally explained, for a working example,… it makes so much more sense…
And then gives one insight to move to the next level/task…
Again Many tx