Automation based on sun and time

Hi,

the situation: my wife works in shifts. When she has early shift, she gets up at 4:30 AM and walks down into the kitchen.
I want to have the light on when she enters the kitchen based on 2 conditions:

  • it has to be dark outside (or dim) and
  • I want to turn on the light at a specific time, say 4:35 AM

My first attempt that does not work:

- alias: Bine Frühdienst Licht an Montag bis Freitag
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 8
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: calendar.Sabine_Bine_Frueh
        state: 'on'
      - condition: state
        entity_id: 'binary_sensor.workday_sensor'
        state: 'on'
      - condition: time
        after: '04:30:00'
        before: '04:35:00'
  action:
    - service: light.turn_on
      data:
        entity_id: light.kuche
        brightness_pct: 100

(the calendar and workday conditions work fine, I am 100% sure)
Any ideas why the light stays off in the morning?

{{ state_attr(‘sun.sun’, elevation) }}

Thanks.
I’ll try that out.
And while we are at this:
Is there a better solution for

      - condition: time
        after: '04:30:00'
        before: '04:35:00'

? I’d like to have at '04:35:00' - but the time condition has no at.

This automation will probably never run.

It triggers when the sun goes from above 8 to below 8 degrees (and only then) and if the time is not between 4:30 and 4:35 when that occurs the automation wont run. You have your triggers and conditions back to front, try this instead:

- alias: Bine Frühdienst Licht an Montag bis Freitag
  trigger:
    platform: time
    at: '4:30'
  condition:
    - condition: state
      entity_id: calendar.Sabine_Bine_Frueh
      state: 'on'
    - condition: state
      entity_id: 'binary_sensor.workday_sensor'
      state: 'on'
    - condition: template
      value_template: "{{ state_attr('sun.sun', elevation)|float < 8 }}"
  action:
    - service: light.turn_on
      data:
        entity_id: light.kuche
        brightness_pct: 100

This way the automation triggers at 4:30 and if the sun is below 8 degrees or more (and the other conditions are met) the action will run.

Also as of v0.115 the sun condition could be written like this:

    - condition: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: 8
1 Like

I’m on 0.115.6 - and this looks much easier than using a template.

1 Like