Conditions in action

Im trying to create an automation that turns off our bedside lights depending on the brightness when triggered.

If the lights are on the lowest brightness 1 then turn off, If the brightness is above the setting 1 then dim the lights to off.

Ive tried so far but with no luck

- alias: "Dim to off bedside lights"
  initial_state: "on"
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: bedroom_switch
      event: 1002
  action:
    - service_template: >
        {%- if state_attr("light.bedroom_table_lamps", "brightness") == 1 -%}
          light.turn_off
        {%- else -%}
          service: light.toggle
          data:
            brightness: 255
            transition: 60
        {%- endif %}
      entity_id: light.bedroom_table_lamps

and

- alias: "Dim to off bedside lights"
  initial_state: "on"
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: bedroom_switch
      event: 1002
  action:
    - condition: template
      value_template: '{{ state_attr("light.bedroom_table_lamps", "brightness") == 1 }}'
    - service: light.turn_off
      entity_id: light.bedroom_table_lamps
    - condition: template
      value_template: '{{ state_attr("light.bedroom_table_lamps", "brightness") > 1 }}'
    - service: light.toggle
      data:
        brightness: 255
        transition: 60
        entity_id: light.bedroom_table_lamps

I would be grateful for some advice on this.

Your automation doesn’t make any sense to me.
Do I understand correctly that you want:

  • brightness == 1 -> turn off immediately
  • brightness > 1 -> turn off lights gradually over a period of 60 seconds

If so, then you need something like this (untested):

- alias: "Dim to off bedside lights"
  initial_state: "on"
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id: bedroom_switch
      event: 1002
  action:
    - service: light.turn_off
      entity_id: light.bedroom_table_lamps
      data_template:
        transition: >
          {% if state_attr("light.bedroom_table_lamps", "brightness") == 1 %}
            0
          {% else %}
            60
          {% endif %}

This will always turn your light off. If the brightness is equalt to 1, then transition will be 0 (light turns off immediately) otherwise transition will be 60. If it doesn’t work, post the logs here.

As far as I know you can’t include the data for the service call in the service_template.

That is just what I wanted Burningstone, thank you.

1 Like