Dimming and turning off light with dynamic script

Hi folks.

I am trying to make some light dim after x seconds, and if no movements within a given timespan, the light should turn off completely.
I am trying to avoid having a script pr. room, so therefore, I am trying to send some variables from an automation to the script. However, I am stuck in my last condition, where I check, if there has still not been any movement in the room for x seconds.

My yaml looks like this:
Automation:

- alias: 'sluk lys wc 1 sal'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.wc_1_sal_1627738
      to: 'off'
      for:
        seconds: 180
  action:
    - script: script.turn_on
      entity_id: script.sluk_lys
      data_template:
        sensor: wc_1_sal_1627738
        light: wc_1_sal_gruppe

Script:

sluk_lys:
  sequence:
    - condition: template
      value_template: "{{ is_state('light.[light]', 'on') }}" 
    - service: light.turn_on
      data_template:
        entity_id: "light.{{ light }}"
        brightness: >
          {% set bri = 'states.light.[light].attributes.brightness' | int %}
          {{ [bri-50, 0] | max }}
    - delay:
        seconds: 30
    - condition: state
      value_template: "{{ is_state('binary_sensor.[sensor]', 'off') }}"
        for:
          seconds: 30
    - service: light.turn_off
      data_template:
        entity_id: "light.{{ light }}"

Anyone doing something similar?

You might run into “script already running” errors doing it this way. Particularly with that 30 second delay.

HA is not able to run multiple instances of the same script.

Aaaah yes ofc! Did not think about that one. That might be a stupid way of doing it then.
But I guess, the script can just be put directly into the automations action, since that is basically a script.
And I have an automation for listening for an off state on the binary anyway.
I’ll look into that.
Thanks for the feedback :blush:

script has some syntax errors.

sluk_lys:
  sequence:
    - condition: template
      value_template: "{{ is_state('light.'+light, 'on') }}" 
    - service: light.turn_on
      data_template:
        entity_id: "light.{{ light }}"
        brightness: >
          {% set bri = state_attr('light.'+light,'brightness') | int %}
          {{ [bri-50, 0] | max }}
    - delay:
        seconds: 30
    - condition: state
      value_template: "{{ is_state('binary_sensor.'+sensor, 'off') }}"
        for:
          seconds: 30
    - service: light.turn_off
      data_template:
        entity_id: "light.{{ light }}"

and, to avoid @tom_l’s error.

- alias: 'sluk lys wc 1 sal'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.wc_1_sal_1627738
      to: 'off'
      for:
        seconds: 180
  action:
    - script: script.turn_off
      entity_id: script.sluk_lys
    - script: script.sluk_lys
      data_template:
        sensor: wc_1_sal_1627738
        light: wc_1_sal_gruppe

so your syntax errors are because you are trying to place variables into strings and that’s not possible the way you are handling it. Putting bracket’s around a variable will not work and have it automatically place said variable into the string. You can put variables in strings but the strings themselves need a specific format and you need to call the format function:

'states.light.{light}.attributes.brightness'.format(light=light)

or

'states.light.{0}.attributes.brightness'.format(light)

or

'states.light.{}.attributes.brightness'.format(light)

but it will still leave it as a string and not an object, making all these examples not work.

the proper way to do what you were trying to do was to call the object_id (what you labeled as light and sensor) from the object directly.

states.light[light].attributes.brightness

2 things to note here:

  1. no quotes around it, that makes it the object
  2. No dot after the first light. That means you are accessing an attribute/key of the states.light object.
  action:
    - script: script.turn_off
      entity_id: script.sluk_lys
    - script: script.sluk_lys

Won’t restarting the script leave some of the lights on?

Yep, but it’s the same light in that automation. If other automations call it, then yes it will.

Yeah that’s what I was worried about considering @WonderMoose’s original intentions:

I am trying to avoid having a script pr. room, so therefore, I am trying to send some variables from an automation to the script.

Hi guys, thanks for your help.

I ended up just building it into the dimm automation, and scipped the script.