#don't override script default variables

1 messages · Page 1 of 1 (latest)

viscid palm
#

Hi,

I have a script that takes 3 variables. When I trigger it from an automation, I only want to set one of them. The other 2 should remain at their default values. However, setting any variable at all, results in the others being unset.

Is there a way around this?

script variables;

variables:
  segments: []
  valetudo_segments:
    - name: guest_toilet
      id: 10
    - name: entrance
      id: 8
    - name: office
      id: 5
    - name: living_room
      id: 6
    - name: dining_room
      id: 7
    - name: kitchen
      id: 9
    - name: master_hallway
      id: 4
    - name: master_bathroom
      id: 3
    - name: master_bedroom
      id: 2
  selected_segments: >-
    {% set data = namespace(segments=[]) %} {% set clean_all =
    is_state("input_boolean.valetudo_1stfloor_segment_all", "on") %}

    {% if segments|length > 0 %}
      {% for segment in valetudo_segments %}
        {% if segment.name in segments %}
          {% set data.segments = data.segments + [segment.id] %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% for segment in valetudo_segments %}
        {% if clean_all or is_state("input_boolean.valetudo_1stfloor_segment_" + segment.name, "on") %}
          {% set data.segments = data.segments + [segment.id] %}
        {% endif %}
      {% endfor %}
    {% endif %}

    [{{ data.segments | join(",") }}]

action in the automation:

action: script.valetudo_clean_segments
metadata: {}
data:
  segments:
    - guest_toilet
hollow sedge
#

your script must use parameters ( aka fields: ).

viscid palm
#

Is there any documentation for that?

hollow sedge
#

yes

main torrent
#

you can pass as many variables to a script as you want, without providing fields

hollow sedge
#

ok. then let's go for the hard way ...

main torrent
#

Not sure what you mean here, but using fields won't solve the problem

main torrent
hollow sedge
#

ahhh ... thx! interesting !

main torrent
#

@viscid palm you don't have to use those for loops in your last template, let me write something up

hollow sedge
#

but i'll continue to use ´fields: ` at least it is clear and the gui can show you how to re-use the script :-))))

main torrent
#

sure, there is no issue with using them, I use them as well. But it is not needed to pass variables to a script.

#

@viscid palm this avoids using for-loops

  selected_segments: >-
    {% set clean_all = is_state("input_boolean.valetudo_1stfloor_segment_all", "on") %}
    {% if segments | length > 0 %}
      {{ valetudo_segments | selectattr('name', 'in', segments) | map(attribute='id') | list }}
    {% elif clean_all %}
      {{ valetudo_segments | map(attribute='id') | list }}
    {% else %}
      {% set segments = states.input_boolean
                              | selectattr('state', 'eq', 'on')
                              | map(attribute='entity_id')
                              | select('search', '^input_boolean.valetudo_1stfloor_segment_')
                              | map('replace', 'input_boolean.valetudo_1stfloor_segment_', '')
                              | list %}
      {{ valetudo_segments | selectattr('name', 'in', segments) | map(attribute='id') | list }}                        
    {% endif %}
viscid palm
main torrent
#

that shouldn't have been required, they are processed in the order you define them

#

I have a lot of similar variables sections where I use the variables in order

viscid palm
#

Hm... to me it simply looked like invoking a script from an automation like this:

action: script.valetudo_clean_segments
metadata: {}
data:
  segments:
    - master_bathroom

overrides the entire first variables: block in the script with the provided data:. Thus all variables that are not defined explicitly end up being nulled.

I'll try again without my workaround later.