Pass variables from automation to script for conditions

Hi

I am having a problem getting a script to work.

I have an automation, which is called by a Shelly Button. I want this automation to call a script and pass some variables (for example, the name of the entity whose brightness I want to set.).

I know that I could pass the variables form the automation with something like that:
automation:

action:
  - service: script.script_adjust_brightness
    data:
      entity: name_of_the_entity
      step_size: 20

Now my problem is that I need to have the values of the variables before calling the service. Because I have a condition that checks the current brightness of the entity, before I call the service.
I would like to have the data template before the sequence.

data_template:
   entity: '{{entity}}'
   step_size: '{{step_size}}'

Script (not complete):

variables:
  current_brightness: >
    {{ (state_attr('light.entity', 'brightness') | float / 255.0 *
    100) }}
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ current_brightness <= 95 and
              states('input_boolean.entity') == 'on' }}
        sequence:
          - service: light.turn_on
            data:
              brightness_step_pct: '{{step_size}}'
            target:
              entity_id: light.{{entity}}

Is it possible to already get the value of the passed variables in the section variables before the sequence? Or is there an other possibility?

thanks

I think you made some mistakes in your script using variables,
for example if we have variable_entity: {{ light.test }}

In the script you should use it like this:
entity_id: {{ variable_entity }} not entity_id: light.{{entity}}
same thing in current_brightness variable
state_attr(variable_entity, ‘brightness’)

No, that’s not really the problem. Because the variable “entity” is just the name of the light, so in the script the entity_id:light.{{entity}} is replaced by entity_id:light.name_of_the_entity.
If I define the variables directly at the top of the script, it works fine:

variables:
  entity: 'name_of_the_entity'
  current_brightness: >
    {{ (state_attr('light.entity', 'brightness') | float / 255.0 *
    100) }}

The problem is to receive the value from the passed variables at the top of the script.

So, as i understand, the variable ‘entity’ is not visible by the variables: part of the script.
Therefore the problem is that you can’t not define the ‘current_brightness’ variable, is that correct?

I think the location of the quotes are a problem… move entity outside the quotes so you can concatenate the value of the variable to ‘light.’ and ‘input_boolean.’ instead of the string ‘entity’. Also, float() needs a default, and do your future-self a favor and put in the data variable fields so it appears in the Services test tool.

fields:
  entity:
    description: Entity name (entity_id with domain removed)
    example: basement_overhead
  step_size:
    description: Number of percentage points each step will increase brightness
    example: 20
variables:
  current_brightness: >
    {{ (state_attr('light.'~entity, 'brightness') | float(0) / 2.55) }}
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ current_brightness <= 95 and
              is_state('input_boolean.'~entity, 'on') }}
        sequence:
          - service: light.turn_on
            data:
              brightness_step_pct: '{{step_size}}'
            target:
              entity_id: light.{{entity}}
1 Like

Thanks, now everything works fine