Automation after getting up

Hello,
I want to get up at a certain time every weekday, switch on two lamps that stay on for 2:30 hours. Unless the sun rises during this time (2:39). Then the two lamps should go out. This is exactly the condition for switching off the lights after sunrise. I don’t know how to do that. This is my approach. I can’t get any further.

alias: LichtAufstehen
description: Nach dem Aufstehen geht das Licht im Wohnzimmer an.
trigger:
  - platform: time
    at: input_datetime.aufstehen_wochentags
condition: []
action:
  - if:
      - condition: state
        entity_id: binary_sensor.workday_sensor
        state: "on"
    then:
      - action: light.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: light.lichtwohnzimmer
      - if:
          - condition: sun
            after: sunrise
        then:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.lichtwohnzimmer
      - delay:
          hours: 1
          minutes: 30
          seconds: 0
          milliseconds: 0
      - action: light.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: light.lichtwohnzimmer
mode: single

input_datetime.aufstehen_wochentags = 6:20 Uhr
Do you have any idea how I can implement this?
Greetings from Stefan

My suggestion would be to have multiple automations:

  • one to turn the lights on at the time and then delay 2:30 and turn them off
  • one to turn them off at sunrise

I do this, but it doesn’t work for me in Sweden because the time of sunrise changes so drastically during the year. So what I have is

  • turn on the lights at the time AND the sun has not risen, then delay and turn them off
  • turn them of when the sun rises

Otherwise the lights go on during the summer when it is already fully bright outside.

1 Like

Make that a condition. No need for ìf`, here.

Here you go:

alias: LichtAufstehen
description: Nach dem Aufstehen geht das Licht im Wohnzimmer an.
trigger:
  - platform: time
    at: input_datetime.aufstehen_wochentags
    id: 'on'
  - platform: state
    entity_id: light.lichtwohnzimmer
    to: 'on'
    for: "02:30:00"
    id: 'off'
  - platform: sun
    event: sunrise
    id: 'off'
condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: "on"
action:
  - action: light.turn_{{ trigger.id }}
    target:
      entity_id: light.lichtwohnzimmer

Turns on at your set time; turns off when the light has been on for 2:30 or when the sun rises. It doesn’t matter that the light gets turned off twice.

It’s good practice not to have long delays in automations; and I prefer to keep complex logic out of the action block.

Note that this will turn off the lights after 2:30 regardless of whether they were turned on by this automation or elsewhere. If that’s a problem, you could create a template sensor with device class timestamp that is your input_datetime plus 2:30 and replace the state trigger with a time trigger for that sensor.

1 Like

Pretty!
So you can have identical id’s? I thought they ought to be unique…

Nope: