Single automation to control brightness level

Found this brilliant code to put 2 automation into 1 which works great to turn something ON/OFF, but I can’t work out how to adjust the dimming when the light goes on.
Can someone put me on the right track :slight_smile:

- id: Automatic_Study_lamp
  alias: Automatic Study Lamp
  trigger:
  - platform: state
    entity_id: binary_sensor.study_motion_occupancy_a
    to: 'on'
  - platform: state
    entity_id: binary_sensor.study_motion_occupancy_a
    to: 'off'
    for:
      minutes: 1
  condition:
    - condition: or
      conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.study_motion_occupancy_a
          state: 'on'
        - condition: numeric_state
          entity_id: sensor.study_motion_illuminance_a
          below: 100
      - condition: state
        entity_id: binary_sensor.study_motion_occupancy_a
        state: 'off'
  action:
    service_template: >
      {% if trigger.to_state.state == 'on' %}
      light.turn_on
      {% else %}
      light.turn_off
      {% endif %}
    data_template:
      transition: 1
    entity_id: light.study_test

Instead of using a service_template to select between light.turn_on and light.turn_off, always use service: light.turn_on. Then, for brightness, use a template that selects between 0 (which will actually turn the light off) and whatever brightness value you want when the light should really go on.

thanks Phil, but i’m just learning templates and really struggle… I understand what you mean I just can’t work out how to add the correct code.

Something like this? but it errors.

  action:
    service: light.turn_on
    entity_id: light.study_test
    data_template:
      brightness: >
        {% if states('binary_sensor.study_motion_occupancy_a', 'on') %} 254
        {% else %} 0
        {% endif %}

Use is_state instead of states.

1 Like