Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 105738

Dynamic state access for dynamic script fields

$
0
0

I want to run a script select_maintenance_task to select an “asset” option in an input_select. It looks something like this obviously. My question relates to parameterising the option.

...
sequence:
        - service: input_select.select_option
          target:
            entity_id: input_select.maintenance_asset
          data:
            option: "selected option"
...

The script is called via a call service from a card tap action as below.

...
tap_action:
      action: call-service
      service: script.select_maintenance_task
      data:
        ...     

The card doesn’t know the “asset” in question so it can’t simply pass that as a variable which the script could then use (passing-variables-to-scripts).

It just knows it is either task 0, task 1 or task 2. It gets its label by looking that index up in the attributes of an entity called maintenance_tasks which has attributes like below.

This way I can add several cards, each with a different index and their label dynamically generated. The attributes of maintenance_tasks change, so this is all necessary.

task_0_asset: Outdoor Blinds
task_1_asset: Food Fridge
task_2_asset: Washing Machine

I need to pass the “asset” to the script. I experimented with using templates for the data in the service call of the card tap_action but understand you cannot use templates here.

eg: this doesn't work
...
tap_action:
      action: call-service
      service: script.select_maintenance_task
      data:
        asset: {{ state_attr('var.maintenance_tasks', 'task_0_asset') }}
...

I’ve then looked at simply passing the index and getting the script to find the asset via lookup from the state attributes of maintenance_tasks.

eg: this part works obviously
...
tap_action:
      action: call-service
      service: script.select_maintenance_task
      data:
        index: 0
...

…but then I hit snags in the script trying to lookup the attribute value. Below I show two failed attempts.

script:
    select_maintenance_task:
      ...
      fields:
        index:
          description: '0-based index of UI element corresponding to the upcoming task.'
          example: 0
      sequence:
        - service: input_select.select_option
          target:
            entity_id: input_select.maintenance_assets
          data:
        **this doesn't work...**
            option: |
              {% set attribute = 'task_' + {{ index }} + '_asset' %}
              {{ state_attr('var.maintenance_tasks', attribute) }}"
        **nor this...**
            option: |
              {% set attribute = 'task_' + {{ index }} + '_asset' %}
              {{ state_attr('var.maintenance_tasks', attribute) }}"
        **or this...**
            option: "{{ state_attr('var.maintenance_tasks', 'task_{{ index }}_tasl' ) }}"
...

Is there any way I can achieve this dynamic access of state?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 105738

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>