Alternatives to below horizon

Hi,
I have created with some search and c&p an automation for my lights depending on motion detection and the sun state. The automation works, but has one problem.
If the light turn on at sun blow_horizon (especial early in the morning) and the sun rise is also over the horizon the light doesn’t turn off.
Can someone give me a suggestion how can I solve this? Also at sunset, I want to turn the lights on 30min before the sun is below the horizon, but I can’t set a negative vale -30min or so.

Thanks in advance

Micha

- id: '1643474163000'
  alias: Licht Schlafzimmer  nach Sonnenuntergang
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
    to: 'on'
    from: 'off'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
    to: 'off'
    from: 'on'
    for:
      hours: 0
      minutes: 1
      seconds: 10
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
    for:
      hours: 0
      minutes: 1
      seconds: 0
  action:
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''on'' }}'
      - condition: template
        value_template: '{{ is_state(''light.blitzwolf_decke'', ''off'') }}'
      sequence:
      - service: light.turn_on
        data:
          brightness: 117
        target:
          device_id: 912266f424bc242cebdd1704ac72aeaa
    - conditions:
      - condition: template
        value_template: '{{ trigger.to_state.state == ''off'' }}'
      - condition: template
        value_template: '{{ is_state(''light.blitzwolf_decke'', ''on'') }}'
      sequence:
      - service: light.turn_off
        target:
          entity_id:
          - light.blitzwolf_decke
        data: {}
  mode: single

Does this help?

https://www.home-assistant.io/docs/automation/trigger/#sun-trigger

My personal solution for a similar problem was to create an input_boolean helper with automations that follow its state to control the light, with another automation that watches the input_boolean for being on for the desired time period and then flips it (the input-boolean that controls the light) off. I have it set to turn the outside light on for five minutes when the dog lets himself out at night, with an added benefit that the turn-off timer doesn’t reset when he comes back inside or walks past the dog door and triggers the lights-on automation a second time…

1 Like

Use two separate automations. One for on with the condition, and one for off without the condition.

1 Like

Also at sunset, I want to turn the lights on 30min before the sun is below the horizon, but I can’t set a negative vale -30min or so.

I personally use GitHub - pnbruckner/ha-sun2: Home Assistant Sun2 Sensor for this, it’s discussed here: Enhanced Sun component - Share your Projects! / Custom Components - Home Assistant Community (home-assistant.io). It doesn’t solve the negative value, but with the ‘Above 3.0’ sensor it provides, it suffices for my automations.

This seems to work for me.

alias: light on 30mins before sunset
trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.blitzwolf_decke
mode: single

My understanding is that you want the light to turn on at motion, but only when the sun is down, is that correct?
in that case: the below should work:

The trigger is any state change of your motion sensor, but it will only work when the sun is below the horizon. (I’ve removed the condition of 1 minute of sunset as I believe that would prevent it from triggering at any longer time the sun was down).
When trigger and condition are true, then it chooses between:

  • If motion is ‘on’ and light is ‘off’ → turn light on
  • If motion is ‘off’ for 1 minute and light is ‘on’ → turn light off
- id: '1643474163000'
  alias: Licht Schlafzimmer  nach Sonnenuntergang
  description: ''
  mode: single
  trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
            state: 'on'
          - condition: state
            entity_id: light.blitzwolf_decke
            state: 'off'
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.blitzwolf_decke
      - conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
            state: 'off'
            for:
              hours: 0
              minutes: 1
              seconds: 0
          - condition: state
            entity_id: light.blitzwolf_decke
            state: 'on'
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.blitzwolf_decke
    default: []

Just a curious question, lets say you just use the turn on light, how long time will your motion detector take before “detecting” again ?
I have 4 Aqara motion detector, Default seems to be ca 60 seconds ( Detecting intervals)

which made me use 2 simple short automation ( for 2 of them )
Trigger = Motion-Detected > Sun_below Horizon> Turn on light
Trigger =No-Motion-Detected> Turn of Light

Which gives me time, to go to Bed, (as long as im moving around, there is light, 1min under the blanket, Lights turn of , … and another Room, primarily a walk through Area, same “automation”, moving around lights are on, out of area, light turns of while “no motion detected” after Default 1min) … Don’t complicate a simple function

Nope. This condition will prevent the lights turning off if they turned on just before sunset:

  - condition: state
    entity_id: sun.sun
    state: below_horizon

That is the posters issue. The lights stay on after sunset sometimes as the condition prevents them turning off.

Move the condition to your on choose action sequence to fix this:

- id: '1643474163000'
  alias: Licht Schlafzimmer  nach Sonnenuntergang
  description: ''
  mode: single
  trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
  action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
            state: 'on'
          - condition: state
            entity_id: light.blitzwolf_decke
            state: 'off'
          - condition: state
            entity_id: sun.sun
            state: below_horizon
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.blitzwolf_decke
      - conditions:
          - condition: state
            entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
            state: 'off'
            for:
              hours: 0
              minutes: 1
              seconds: 0
          - condition: state
            entity_id: light.blitzwolf_decke
            state: 'on'
        sequence:
          - service: light.turn_off
            data: {}
            target:
              entity_id: light.blitzwolf_decke
    default: []

thanks, I will try the suggestions.

Since you asked a question about my sun2 custom integration in relation to this topic, I thought I’d share my thoughts here.

I generally agree with @tom_l’s solution, although I see two problems with it.

First, I don’t think it will work for turning the light off, because it has a condition that the sensor should have been off for a minute, which it can’t be when it has just turned off (i.e., triggered by going off.)

Second, it doesn’t address your question about how to include the 30 minutes that precede sunset.

Before I give my suggested solution, I’ll comment (like I think maybe others already have), that you should probably use the sun’s elevation as opposed to time-based offsets from sunrise/sunset, since the rate at which the sun rises and falls changes throughout the year.

Anyway, here’s what I’d suggest:

- id: '1643474163000'
  alias: Licht Schlafzimmer  nach Sonnenuntergang
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 1
      seconds: 10
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
        state: 'on'
      - condition: state
        entity_id: light.blitzwolf_decke
        state: 'off'
      - condition: or
        conditions:
        - condition: sun
          before: sunrise
        # The following means after 30 minutes before sunset.
        - condition: sun
          after: sunset
          after_offset: "-00:30:00"
      sequence:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.blitzwolf_decke
    - conditions:
      - condition: state
        entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_429fd407_ias_zone
        state: 'off'
      - condition: state
        entity_id: light.blitzwolf_decke
        state: 'on'
      sequence:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.blitzwolf_decke
1 Like

Your way works. It seems that I had an understanding issue with the sunset. I have thought every day has only one sunset with a specific time, so in my opinion it only works one times a day :sweat_smile: