Dimm light by motion

Good morning,

I have the following automation to turn on and off my lamp. Now my question, is it possible to change this automation to turn the light to 30% if a motion is detected.


description: ''
trigger:
  - platform: state
    to: 'on'
    entity_id: binary_sensor.multisensor_gang_bewegungs_alarm
  - platform: state
    to: 'off'
    for:
      hours: 0
      minutes: 2
      seconds: 0
    entity_id: binary_sensor.multisensor_gang_bewegungs_alarm
condition:
  - condition: numeric_state
    entity_id: sensor.multisensor_gang_helligkeit
    below: '11'
action:
  - service: light.turn_{{ trigger.to_state.state }}
    target:
      entity_id: light.licht_gang
mode: restart

So 30% if motion, and if no motion for 2 minutes then off?

The secret is that 0% brightness should turn your light off, even if using the light turn on service.

description: ''
trigger:
  - platform: state
    to: 'on'
    entity_id: binary_sensor.multisensor_gang_bewegungs_alarm
  - platform: state
    to: 'off'
    for:
      hours: 0
      minutes: 2
      seconds: 0
    entity_id: binary_sensor.multisensor_gang_bewegungs_alarm
condition:
  - condition: numeric_state
    entity_id: sensor.multisensor_gang_helligkeit
    below: '11'
action:
  - service: light.turn_on
    target:
      entity_id: light.licht_gang
    data:
      brightness_pct: "{{ 30 if trigger.to_state.state == 'on' else 0 }}"
mode: restart

Or if you prefer the new immediate if format iif()

      brightness_pct: "{{ iif(trigger.to_state.state == 'on', 30, 0) }}"

or

      brightness_pct: "{{ (trigger.to_state.state == 'on')|iif(30, 0) }}"

So there’s three ways. Want one more?

There’s the long-winded multi line template:

      brightness_pct: >
        {% if trigger.to_state.state == 'on' %}
          30
        {% else %}
          0
        {% endif %}

Personally I find the first one is a good combination of brevity and readability.