Turn off a lamp at sunrise but not before 8 o'clock

Hello,

I want to turn off a lamp at sunrise. But if that is earlier than 8 o’clock in the morning, it may not go out until 8 o’clock in the morning.
To achieve this I have created the code below. But when I execute this the light still goes off at sunrise and not at 8 o’clock. Anyone have any idea why this turns off at sunrise?

description: ""
triggers:
  - event: sunrise
    trigger: sun
    offset: "00:31:00"
    variables:
      is_true: "{{ now() > today_at('08:00') }}"
      mode: turn_off
  - at: "08:00:00"
    trigger: time
    variables:
      is_true: "{{ is_state('sun.sun', 'above_horizon') }}"
      mode: turn_off
  - event: sunset
    trigger: sun
    offset: "-01:00:00"
    variables:
      is_true: "{{ is_state('sun.sun', 'below_horizon') }}"
      mode: turn_on
conditions: []
actions:
  - action: switch.{{ mode }}
    metadata: {}
    data: {}
    target:
      device_id: 4f5df7ec56f47c717fa0252ea50dbec7
mode: single

Also, this should turn on at sunset. But that part does work.

Because you have no conditions. It will turn off when the first trigger of the two “off” triggers happens.

You create an is_true variable but then don’t use it to do anything — I suspect you’ve done this via copy-paste-modify, and I’m guessing the original was one of @123’s. Use this condition block, which tests whether is_true is true.

conditions:
  - condition: template
    value_template: "{{ is_true }}"

Your mixture of “31 minutes before after sunrise” and “above_horizon” make me wonder if there’s a set of circumstances where it won’t turn off. I think you’re OK though.

Ah ok, thanks, I missed that bit of conditions “copy past…” :frowning:

And I see your point about “31 minutes before sunrise” and “above_horizon” I need to look into how I can solve that.

You could use sun.sun’s elevation attribute as a measurable and testable threshold value.

Trigger:

Condition:

Thanks, I’ll try this out.

Simply for my understanding…In the first step, you want a light to turn off at sunrise unless it’s before 8:00 AM. If sunrise is after 8AM you want the light to go out at 8AM, correct?