Sensor Light to come on between certain times from sunset

Hi

New to this arena of HA,
I am looking to turn a light strip on between 45 minutes before sunset and 1 hour after sunset using a Philips hue sensor
I will run another automation after that however for now I don’t seem to be getting it right
The light only turns on once then never again
In all honesty i may be doing things out of sync with how HA does however I would appreciate any guidance to assist me on this journey

please put code in via preformatted text instead of a screenshot. it makes it easier for us to edit your code.

your conditions are kinda wacky. you have both before sunset and after sunset in the first condition. which do you mean?

you should also not be using device_id.

you said you want to turn on a light strip 45min before sunset and 1 hour after. is that what you really want? your automation seems to say you want to turn on the light on a motion detection.

thank you for the quick response, appreciated
I am using the visual editor so this is what it put out when I changed to YAML
basically I want the system to only trigger in between 45min BEFORE sunset and 1 hour AFTER sunset on trigger from motion sensor
I am appreciate the fixing my code part however I don’t feel I will learn why and how if someone changes is for me, so if at all possible please could I understand why the code needs to change and what the recommended changes would be, I hope that doesnt sound unappreciative.

alias: Stairs Strip on - Non Bed Time
description: ""
trigger:
  - type: motion
    platform: device
    device_id: d77601be3abbdcad201631a3329b7e94
    entity_id: 77d2afc4a9ecb19646f0edbdc4eb222b
    domain: binary_sensor
condition:
  - condition: sun
    before: sunset
    before_offset: "-00:45:00"
    after: sunset
  - condition: sun
    after: sunset
    after_offset: "01:00:00"
action:
  - type: turn_on
    device_id: 57c860768a69e5ebd6a87d02e40d2c25
    entity_id: 0dbbfde0686843e5c656269c92c84e9a
    domain: light
    brightness_pct: 95
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 57c860768a69e5ebd6a87d02e40d2c25
    entity_id: 0dbbfde0686843e5c656269c92c84e9a
    domain: light
mode: single

if you use the visual editor, use the green entity button to choose your lights. not the blue device ones.

i did my automation purely with the ui as well…

Conditions are AND by default, so it looks to me as if your automation will only run if it is both 45 minutes before sunset AND 1 hour after sunset - in other words, never. :grin:

ah that’s makes sense :rofl:

that’s what i thought at first, but then look more carefully at this:

  - condition: sun
    before: sunset
    before_offset: "-00:45:00"
    after: sunset

in one condition it has before and after. ui shouldn’t allow that imho

1 Like

Aha! Didn’t see that. In the UI it is possible to click both before and after in the same condition…

So what would the trigger be? Sunset or motion or both
and how would I then create the conditions around the timings I would prefer and how would I make it happen every time the motion sensor is triggered during that time frame

if you change the condition to this, does it do what you want?
what i posted above or this variant (i personaly like this below, but it’s a preference only)

this says:

// do the code if all of the below are true:
// 1) it is before 1 hour AFTER sunset
// 2) it is after 45 minutes BEFORE sunset

  - condition: sun
    before: sunset
# 1) it is before 

    before_offset: "01:00:00"
#      1 hour AFTER sunset 

  - condition: sun
    after: sunset
# 2) it is after 

    after_offset: "-00:45:00"
3       45 minutes BEFORE sunset

as I was reading it, the post was deleted

does this make more sense and now without the device ids

alias: Stairs Strip on - Non Bed Time
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.stairs_motion
    from: "off"
    to: "on"
condition:
  - condition: sun
    before: sunset
    before_offset: "00:45:00"
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: "01:00:00"
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 95
    target:
      entity_id: light.stairs
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.stairs
mode: single

sorry, i thought i had a bug and wanted to verify it… so i temporarily deleted. but i restored it now.

no, your code doesn’t make sense to me still.

you asked to learn why… here’s what your code says. forget about reading the code, is this what you intend?

//// do the script if the following are ALL true:
/// 1) if it is currently before 45 minutes before sunset
/// 2) AND also
/// one of the following (1) conditions:
/// 2b)that it is after 1 hour after sunset

#  do the script if the following are ALL true:
  - condition: sun
#  1)if it is currently before  45 minutes before sunset
    before: sunset
    before_offset: "00:45:00"
# 2) AND also
  - condition: or
#     one of the following (1) conditions:
    conditions:
      - condition: sun
#    2b) that it is after 1 hour after sunset
        after: sunset
        after_offset: "01:00:00"

ah right I understand that now thank you
now my question is how do create the conditions, or am I thinking about incorrectly

cleaned up. i’d use this:

condition:
  - condition: sun
    before: sunset
    before_offset: "01:00:00"
  - condition: sun
    after: sunset
    after_offset: "-00:45:00"

I think I may be slowly understanding )Hopefully)

dow this make sense

alias: Stairs Strip on - Non Bed Time
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.stairs_motion
    from: "off"
    to: "on"
condition:
  - condition: sun
    before: sunset
    before_offset: "-00:45:00"
    after: sunset
    after_offset: "01:00:00"
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 95
    target:
      entity_id: light.stairs
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.stairs
mode: single

nope. you have the same problem with this.

and this

alias: Stairs Strip on - Non Bed Time
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.stairs_motion
    from: "off"
    to: "on"
condition:
  - condition: sun
    before: sunset
    before_offset: "00:45:00"
    after: sunset
    after_offset: "-01:00:00"
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 95
    target:
      entity_id: light.stairs
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.stairs
mode: single

yeah I see this issue, its the AND
I need a BETWEEN

ok, that last one looks right. my reply above was to the one 2 above… race condition on posting!

gonna delete that one so that anyone coming later to read won’t get confused.