Automation dimming lights on movement after sunset

i am trying to make a automation, that brightens and dims lights after sunset after movement is detected by xiaomi body sensor
The lights are turned on by another automation when the sun is down, at 30%
I want them to 90% for 3 minutes, and then return to 30% again
I am using xiaomi body sensors for this

I currently use this, brightening to 90% is ok, but they dont go back to 30 after 3 minutes

- alias: "Garage links On"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_garage_occupancy
      to: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
  action:
    - service: homeassistant.turn_on
      entity_id: light.garage_links
      data:
        brightness_pct: '90'
        transition: 3
#    - service: notify.notify
#      data:
#        message: Office Motion Light On
        
- alias: "Garage links Off"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_garage_occupancy
      to: 'off'
      for:
        minutes: 3
  action:
    - service: homeassistant.turn_off
      entity_id: light.garage_links
      data:
        brightness_pct: '30'
        transition: 3
#    - service: notify.notify
#      data:
#        message: Office Motion Light Off

    - service: homeassistant.turn_on
      entity_id: light.garage_links
      data:
        brightness_pct: '30'
        transition: 3

You want homeassistant.turn_on if you want to set brightness.

1 Like

so like this?

- alias: "Garage links On"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_garage_occupancy
      to: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: below_horizon
  action:
    - service: homeassistant.turn_on
      entity_id: light.garage_links
      data:
        brightness_pct: '90'
        transition: 3
#    - service: notify.notify
#      data:
#        message: Office Motion Light On
        
- alias: "Garage links Off"
  trigger:
    - platform: state
      entity_id: binary_sensor.motion_garage_occupancy
      to: 'off'
      for:
        minutes: 3
  action:
    - service: homeassistant.turn_on
      entity_id: light.garage_links
      data:
        brightness_pct: '35'
        transition: 3
#    - service: notify.notify
#      data:
#        message: Office Motion Light Off

i use this to turn on/off a group of lights outside:

- id: turn_outside_lights_on
  alias: buitenlampen_aan_zonsondergang
  trigger:
  - platform: sun
    event: sunset
  - platform: sun
    event: sunrise
  action:
    service_template: "{% if trigger.event == 'sunset' %}\n  homeassistant.turn_on\n\
      {% elif trigger.event == 'sunrise' %}\n  homeassistant.turn_off\n{% endif %}"
    entity_id: light.buitenlampen_aan_zonsondergang
    data:
      brightness_pct: '35'
      transition: 3

The turn_off service does not have the attribute brightness_pct, if you use it you sould have an error in the log.
Every time you need to change the brightness you need to call turn_on

I am not using that anymore?
Both are turn_on I think?

service_template: "{% if trigger.event == 'sunset' %}\n  homeassistant.turn_on\n\
      {% elif trigger.event == 'sunrise' %}\n  homeassistant.turn_off\n{% endif %}"

Lets fix your templates.

https://yaml-multiline.info/

That website will show you yaml multi line formatting with examples.

This is much easier to read using the folded style scalar.

service_template: >
  {% if trigger.event == 'sunset' %}
    homeassistant.turn_on
  {% elif trigger.event == 'sunrise' %}
    homeassistant.turn_off
  {% endif %}

Or if you want it to be more compact…

service_template: "homeassistant.turn_{{ 'on' if trigger.event == 'sunset' else 'off' }}"

But you still have the same problem. Brightness percent wont be used when it’s sunrise.

Are the lights just turning off after 3 minutes? Because that’s what should be happening.

Yes, basically they should turn off after a set time
That’s easier I think then turning off after no movement in 5 minutes?

Yeah. We just aren’t sure what, if anything, is still the problem. It was originally about dimming some lights, but then you posted “i use this to turn off a group of oustide lights”. Wasn’t sure if there was some problem with that or not.

Basically it’s this:
A group of lights, garden, garage etc is turned on at sunset, and turned off at sunrise
At the garage door, in the garden, at my garden shed, I have various xiaomi body sensors. If triggered, the lights at that place should brighten to 90%.

right now this code is not turning off the lights at sunrise?

- id: turn_outside_lights_on
  alias: buitenlampen_aan_zonsondergang
  trigger:
  - platform: sun
    event: sunset
  - platform: sun
    event: sunrise
  action:
service_template: >
  {% if trigger.event == 'sunset' %}
    homeassistant.turn_on
  {% elif trigger.event == 'sunrise' %}
    homeassistant.turn_off
  {% endif %}
    entity_id: light.buitenlampen_aan_zonsondergang

service_template indentation is not correct. Could just be a copy/paste error…but I don’t see any other problems with it.

- id: turn_outside_lights_on
  alias: buitenlampen_aan_zonsondergang
  trigger:
  - platform: sun
    event: sunset
  - platform: sun
    event: sunrise
  action:
    service_template: >
      {% if trigger.event == 'sunset' %}
        homeassistant.turn_on
      {% elif trigger.event == 'sunrise' %}
        homeassistant.turn_off
      {% endif %}
    entity_id: light.buitenlampen_aan_zonsondergang

Try this:

- id: turn_outside_lights_on
  alias: buitenlampen_aan_zonsondergang
  trigger:
  - platform: sun
    event: sunset
  - platform: sun
    event: sunrise
  action:
    service_template: "light.turn_{{ 'on' if trigger.event == 'sunset' else 'off' }}"
    entity_id: light.buitenlampen_aan_zonsondergang