Time-dependent dim level

Hello everyone

I am brand new to Home Assistant.
Until now, I have controlled my smarthome based on openHAB and switched to HA last week.

I had a rule in openHAB that calculates the time between sunrise and sunset and sets a default dimming level that is set when KNX connected lamps are switched on.
To make things even more complicated, I had a group in openHAB to which the dim level is set. If a lamp is now switched manually, it is removed from this group until it is switched off.

Now I’m trying to map the same thing with HA and have also consulted the AI, but I’m not really getting anywhere.

This are the automations, I built:

  description: Setzt den Dimmlevel für neu eingeschaltete KNX Lampen zwischen Sonnenaufgang und Sonnenuntergang gleichmäßig von 90 % auf 10 %
  trigger:
    - platform: state
      entity_id:
        - light.deckenlicht_bad
        - light.deckenlicht_balkon
        - light.deckenlicht_schlafzimmer
        - light.deckenlicht_wohnzimmer
        - light.indirektes_licht_wohnzimmer
      from: 'off'
      to: 'on'
  condition:
    - condition: sun
      before: sunset
      after: sunrise
    - condition: state
      entity_id: input_boolean.dimmlevel_manually_changed
      state: 'off'
  action:
    - service: light.turn_on
      target:
        entity_id: "{{ trigger.entity_id }}"
      data_template:
        brightness_pct: >
          {% set sunrise = state_attr('sun.sun', 'next_rising').timestamp() %}
          {% set sunset = state_attr('sun.sun', 'next_setting').timestamp() %}
          {% set now = as_timestamp(now()) %}
          {% set total_daylight = sunset - sunrise %}
          {% set elapsed_daylight = now - sunrise %}
          {% set brightness = 90 - (80 * (elapsed_daylight / total_daylight)) %}
          {{ brightness | int }}
  mode: single

alias: KNX Lampen Dimmlevel regelmäßig anpassen
  description: Überprüft und passt den Dimmlevel für bestehende KNX Lampen zwischen Sonnenaufgang und Sonnenuntergang an, wenn er nicht manuell verstellt wurde
  trigger:
    - platform: time_pattern
      minutes: '/5'
  condition:
    - condition: sun
      before: sunset
      after: sunrise
    - condition: state
      entity_id: input_boolean.dimmlevel_manually_changed
      state: 'off'
  action:
    - service: light.turn_on
      target:
        entity_id:
          - light.deckenlicht_bad
          - light.deckenlicht_balkon
          - light.deckenlicht_schlafzimmer
          - light.deckenlicht_wohnzimmer
          - light.indirektes_licht_wohnzimmer
      data_template:
        brightness_pct: >
          {% set sunrise = state_attr('sun.sun', 'next_rising').timestamp() %}
          {% set sunset = state_attr('sun.sun', 'next_setting').timestamp() %}
          {% set now = as_timestamp(now()) %}
          {% set total_daylight = sunset - sunrise %}
          {% set elapsed_daylight = now - sunrise %}
          {% set brightness = 90 - (80 * (elapsed_daylight / total_daylight)) %}
          {{ brightness | int }}
  mode: single

  alias: Dimmlevel manuell geändert
  description: Setzt die Hilfsvariable, wenn der Dimmlevel manuell geändert wird
  trigger:
    - platform: state
      entity_id:
        - light.deckenlicht_bad
        - light.deckenlicht_balkon
        - light.deckenlicht_schlafzimmer
        - light.deckenlicht_wohnzimmer
        - light.indirektes_licht_wohnzimmer
      attribute: brightness
  action:
    - service: input_boolean.turn_on
      target:
        entity_id: input_boolean.dimmlevel_manually_changed
  mode: single

  alias: Dimmlevel manuell geändert zurücksetzen
  description: Setzt die Hilfsvariable zurück, wenn das Licht ausgeschaltet wird
  trigger:
    - platform: state
      entity_id:
        - light.deckenlicht_bad
        - light.deckenlicht_balkon
        - light.deckenlicht_schlafzimmer
        - light.deckenlicht_wohnzimmer
        - light.indirektes_licht_wohnzimmer
      from: 'on'
      to: 'off'
  action:
    - service: input_boolean.turn_off
      target:
        entity_id: input_boolean.dimmlevel_manually_changed
  mode: single

But it didnt trigger.
Where is my think-error?

THX, Michi

Hi,
have you had a look at Adaptive Lighting?

If I understand you correctly this should be an easier solution to your automation?

Hey Gregor

That looks really interessting. I will take a look on that.