Light action if else problem

Hi
is this configuration under action correct? it will not work but the test show no errors.
Only the light go on if its off. if its on nothing happens. :frowning:
Has anyone may a alternative config?

thanks

  - alias: Wohnzimmer_Licht_Tag
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: timer.wohnzimmer_licht_timer
        to: 'active'       
    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunrise
          after_offset: "0:15:00"
        - condition: time
          before: '23:15:00'
    action:
      - service: light.turn_on 
        data_template:
          entity_id: light.wohnraum_und_essbereich
          brightness: >-
            {% if is_state('light.wohnraum_und_essbereich', 'on') %}
              254 
            {% else %}
              254
            {% endif %}
          kelvin: >-
            {% if is_state('light.wohnraum_und_essbereich', 'off') %}
              2700
            {% endif %}            
          transition: >-
            {% if is_state('light.wohnraum_und_essbereich', 'off') %}
              1
            {% else %}
              3
            {% endif %} 

You say it doesn’t work … and then you explain how it’s working.

So it does work, just not the way you want it. Please explain what you want it to do.

the room has 2 motion sensors and if I change the color via app the motionsensor change the color and brightness to default. it should be manual change.

Why don’t you take the motion sensor as a trigger and add a condition to only turn the light on if it is not already on?

Sorry, I do not understand your explanation. Your automation contains references to sunrise and time yet your explanation contains no reference to any of that.

yes motionsensors are trigger in a other automation.
Based on timer the motion sensor starts a timer and if the timer is active it trigger this automation and differentiate inside the conditions for the specified brightness and color.

Sorry, I also don’t understand what you want to do.

Like this?
trigger:
Motion detected
condition:
only do this between 15 min after sunrise until 23:15
action:
if light is on -> do nothing
if light is off -> turn it on

For a start, this template has the same output regardless of the test result:

brightness: >-
            {% if is_state('light.wohnraum_und_essbereich', 'on') %}
              254 
            {% else %}
              254
            {% endif %}

So can be replaced with:

brightness: 254

And this will generate an error if the state is ‘on’ as there is no else case to provide a kelvin value:

kelvin: >-
            {% if is_state('light.wohnraum_und_essbereich', 'off') %}
              2700
            {% endif %} 

oh you are right! I thought the there is no else part required
thanks

Your templates appear to set values only if the light is currently off and to leave the values unchanged if the light is already on. You can simplify your templates by adding a condition that the light must be off.

    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunrise
          after_offset: "0:15:00"
        - condition: time
          before: '23:15:00'
        - condition: state
          entity_id: light.wohnraum_und_essbereich
          state: 'off'
    action:
      - service: light.turn_on 
        data_template:
          entity_id: light.wohnraum_und_essbereich
          brightness: 254
          kelvin: 2700
          transition: 1
1 Like

yes that’s a good idea but there is one more automation which dimm the light. so this rule should start the previous state maybe.
I can post my full light automation if you want.

Please post you full automation. Maybe we’ll understand better.

  - alias: Wohnzimmer_isDark
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: sensor.wohnzimmer_bewegungsmelder_1_3
      - platform: state
        entity_id: input_number.wohnzimmer_lux_slider
      - platform: homeassistant
        event: start
    condition:
      - condition: state
        entity_id: light.wohnraum_und_essbereich
        state: 'off'
    action: 
      - condition: template
        value_template: '{{ states.sensor.wohnzimmer_bewegungsmelder_1_3.last_changed >= states.input_datetime.wohnzimmer_last_off.last_changed }}'
      - service: variable.set_variable
        data:
          variable: wohnzimmer_night
          value_template: >-
            {% if states("sensor.wohnzimmer_bewegungsmelder_1_3") | float < states("input_number.wohnzimmer_lux_slider") | float %}
              True
            {% else %}
              False
            {% endif %}

  - alias: Wohnzimmer_NoMotion
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.wohnzimmer_bewegungsmelder_1
        to: 'off' 
    action:        
      - service: input_datetime.set_datetime
        data_template: 
          entity_id: input_datetime.wohnzimmer_last_off         
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

  - alias: Wohnzimmer_Motion
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.wohnzimmer_bewegungsmelder_1
        to: 'off'
      - platform: homeassistant
        event: start 
    action:        
      - service: input_datetime.set_datetime
        data_template: 
          entity_id: input_datetime.wohnzimmer_last_on         
          datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"

  - alias: Wohnzimmer_Licht_above_45Lux
    initial_state: 'on'
    trigger:
      - platform: numeric_state
        entity_id: sensor.wohnzimmer_bewegungsmelder_1_3
        above: 45
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: light.wohnraum_und_essbereich
        state: 'on'   
      - condition: sun
        after: sunrise
        after_offset: "03:00:00"  
    action:        
      - service: light.turn_off
        entity_id: light.wohnraum_und_essbereich        
      - service: variable.set_variable
        data:
          variable: wohnzimmer_night
          value: False

  - alias: Wohnzimmer_Bewegung_erkannt
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.wohnzimmer_bewegungsmelder_1
        to: 'on'
      - platform: state
        entity_id: binary_sensor.wohnzimmer_bewegungsmelder_2
        to: 'on'        
    condition:            
      - condition: state
        entity_id: variable.wohnzimmer_night
        state: 'True'
    action: 
    #problem wenn nach den dimmen das ausgefüht wird
      - service: timer.cancel
        entity_id: 
          - timer.wohnzimmer_licht_timer
          - timer.wohnzimmer_licht_dim_timer
          - timer.wohnzimmer_licht_taster_shortpress_timer
          - timer.wohnzimmer_licht_taster_longpress_timer
      - service: timer.start
        entity_id: timer.wohnzimmer_licht_timer    


  - alias: Wohnzimmer_Timer_Ende
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: timer.finished
        event_data: 
          entity_id: timer.wohnzimmer_licht_timer
    condition:
      condition: and
      conditions:
          - condition: state
            entity_id: light.wohnraum_und_essbereich
            state: 'on'
          - condition: state
            entity_id: group.wohnbereich_motionsensor
            state: 'off'
    action:
      - service: homeassistant.turn_on
        entity_id: light.wohnraum_und_essbereich
        data_template:
          brightness:  '{{ ((float(states.light.wohnraum_und_essbereich.attributes.brightness)) / 2) | int}}'
          transition: 5
      - service: timer.start
        entity_id: timer.wohnzimmer_licht_dim_timer


  - alias: Wohnzimmer_Timer_Ende_2
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: timer.finished
        event_data: 
          entity_id: timer.wohnzimmer_licht_timer
    condition:
      condition: and
      conditions:
          - condition: state
            entity_id: light.wohnraum_und_essbereich
            state: 'on'
          - condition: state
            entity_id: group.wohnbereich_motionsensor
            state: 'on'
    action:
      - service: timer.start
        entity_id: timer.wohnzimmer_licht_timer


  - alias: Wohnzimmer_Timer_Dim_Ende
    initial_state: 'on'
    trigger:
      - platform: event
        event_type: timer.finished
        event_data: 
          entity_id: timer.wohnzimmer_licht_dim_timer
    condition:
          - condition: state
            entity_id: light.wohnraum_und_essbereich
            state: 'on'
    action:
      - service: homeassistant.turn_off
        entity_id: 
          - light.wohnraum_und_essbereich


# Lichteinstellungen

  - alias: Wohnzimmer_Licht_Morgen
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: timer.wohnzimmer_licht_timer
        to: 'active'
    condition:
      condition: and
      conditions:
        - condition: time
          after: '05:00:00'        
        - condition: sun
          before: sunrise
          before_offset: "0:15:00" 
    action:
      - service: homeassistant.turn_on
        entity_id: 
          - light.wohnraum_und_essbereich
        data:      
          kelvin: 2700
          brightness_pct: 100
          transition: 4    



  - alias: Wohnzimmer_Licht_Tag
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: timer.wohnzimmer_licht_timer
        to: 'active'       
    condition:
      condition: and
      conditions:
        - condition: sun
          after: sunrise
          after_offset: "0:15:00"
        - condition: time
          before: '23:15:00'
    action:
      - service: light.turn_on 
        data_template:
          entity_id: light.wohnraum_und_essbereich
          brightness: >-
            {% if is_state('light.wohnraum_und_essbereich', 'on') %}
              254 
            {% else %}
              254
            {% endif %}
          color_temp: >-
            {% if is_state('light.wohnraum_und_essbereich', 'on') %}
              370  
            {% else %}
              370
            {% endif %}            
          transition: >-
            {% if is_state('light.wohnraum_und_essbereich', 'on') %}
              3
            {% else %}
              1
            {% endif %} 

  - alias: Wohnzimmer_Licht_Nacht
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: timer.wohnzimmer_licht_timer
        to: 'active'
    condition:
      condition: or
      conditions:
          - condition: time
            after: '23:15:00'
            before: '23:59:59'  
          - condition: time
            after: '00:00:00'
            before: '05:00:00'   
    action:
      service: homeassistant.turn_on
      entity_id: 
        - light.wohnraum_und_essbereich
      data:      
        kelvin: 2700
        brightness_pct: 65
        transition: 4   

What is the reason for doing this?

          brightness: >-
            {% if is_state('light.wohnraum_und_essbereich', 'on') %}
              254 
            {% else %}
              254
            {% endif %}

It is equivalent to this:

          brightness: 254

that’s the preparation for later to restore the old/current value

You can’t retrieve anything from that.
Start again, what is it you are trying to do ?

the problem is solved. I only post the complete automation.

Well, not truly complete because you just explained to me:

In its current form, the example contains templates that, hopefully, no one will copy verbatim.

Also this:

      - service: variable.set_variable
        data:
          variable: wohnzimmer_night
          value_template: >-
            {% if states("sensor.wohnzimmer_bewegungsmelder_1_3") | float < states("input_number.wohnzimmer_lux_slider") | float %}
              True
            {% else %}
              False
            {% endif %}

Can be simplified to:

      - service: variable.set_variable
        data:
          variable: wohnzimmer_night
          value_template: >-
            {{ states("sensor.wohnzimmer_bewegungsmelder_1_3")|float < states("input_number.wohnzimmer_lux_slider")|float }}

As the comparison will return True/False.

You can’t retrieve anything from that.

I believe Steve means it is a place-holder for future code to be entered to retrieve a previous state.