Condition: template, can't see where I did wrong

Hi,

I got a script that takes a parameter which is what view should be cast to my Nest hub.

From the examples I’ve seen I should be able to set a variable in a condition template
While also performing a conditional check
I.e.

- condition: template
  value_template: >
    {{ param in state_attr('input_select.google_cast_rooms', 'options') }
    {% set cast_target = true %}

So basically if it’s an valid parameter, perform the cast, if not, ignore.

But my last conditional does not seem to evaluate properly?
If I uncomment it, the script works…
Where am I doing wrong… Can someone help me?

- condition: template
  value_template: '{{ cast_target == true }}'

Full function

cast_by_param2:
  alias: Cast|Hubben|Param
  description: "Cast Param to Hubben"
  variables:
    cast_target: false
  fields:
    param:
      description: "Room name as found in input_select.google_cast_rooms"
      example: "vardagsrum"
  sequence:
    - service: media_player.turn_off
      entity_id: media_player.hubben
    - choose:
      - conditions:
        - condition: template
          value_template: >
            {{ param in state_attr('input_select.google_cast_rooms', 'options') }}
            {% set cast_target = true %} 
        sequence:
          - service: input_select.select_option
            data:
              entity_id: input_select.google_cast_rooms
              option: "{{ param }}"
      - conditions:
        - condition: template
          value_template: >
            {{ param in state_attr('input_select.google_cast_others', 'options') }}
            {% set cast_target = true %} 
        sequence:
          - service: input_select.select_option
            data:
              entity_id: input_select.google_cast_others
              option: "{{ param }}"
    # end choose      
    - delay: 1
    - condition: template
      value_template: '{{ cast_target == true }}'
    - service: cast.show_lovelace_view
      data:
        dashboard_path: lovelace-rum
        entity_id: media_player.hubben
        view_path: "{{ param }}"

Your templates are attempting to change a variable’s value. As far as I know, that doesn’t work.

Try this version:

cast_by_param2:
  alias: Cast|Hubben|Param
  description: "Cast Param to Hubben"
  fields:
    param:
      description: "Room name as found in input_select.google_cast_rooms"
      example: "vardagsrum"
  sequence:
    - variables:
        cast_rooms: "{{ param in state_attr('input_select.google_cast_rooms', 'options') }}"
        cast_others: "{{ param in state_attr('input_select.google_cast_others', 'options') }}"
    - service: media_player.turn_off
      entity_id: media_player.hubben
    - choose:
      - conditions: "{{ cast_rooms }}"
        sequence:
          - service: input_select.select_option
            data:
              entity_id: input_select.google_cast_rooms
              option: "{{ param }}"
      - conditions: "{{ cast_others }}"
        sequence:
          - service: input_select.select_option
            data:
              entity_id: input_select.google_cast_others
              option: "{{ param }}"
    - delay: 1
    - condition: template
      value_template: '{{ cast_rooms or cast_others }}'
    - service: cast.show_lovelace_view
      data:
        dashboard_path: lovelace-rum
        entity_id: media_player.hubben
        view_path: "{{ param }}"
1 Like

That did the trick, thanks.
I understand your approach :ok_hand:

1 Like

Glad to hear it solved the problem.

Please consider marking my post (above) with the Solution tag to indicate to other users that this topic has an accepted solution. Effectively, it shows this topic is resolved and helps users find answers to similar questions.

1 Like