#Script with data passed through

1 messages · Page 1 of 1 (latest)

nimble lynx
#

Need help with trying to figure out how I can pass through the Data variable "room" into my script service. It works in the templating, but I am not sure I have the right syntax for it to be passed correctly:

fan_on: alias: Fan On mode: queued sequence: - service: input_boolean.turn_on target: entity_id: input_boolean.{{ room }}_fan_state - service: > {% if is_state("input_select.{{ room }}_fan_speed", '0') -%} script.fan_33 {%- else -%} script.fan_{{ states('input_select.{{ room }}_fan_speed') }} {%- endif %} data: room: "{{ room }}" - condition: template value_template: "{{ states('input_select.{{ room }}_fan_speed') == '0' }}" - service: input_select.select_option data: option: "33" target: entity_id: input_select.{{ room }}_fan_speed

Specifically, everyting after the "-servce: >" line doesn't seem to work. Note: I have also tested passing through the variable as:{% if is_state("input_select.' ~ room ~ '_fan_speed", "0") -%}

weak crystal
#

How are you invoking the script?

nimble lynx
#

Just through the Fan Template:
`fan:

  • platform: template
    fans:
    b3_fan:
    friendly_name: "Bedroom 3 Fan"
    value_template: "{{ states('input_boolean.b3_fan_state') }}"
    percentage_template: "{{ states('input_select.b3_fan_speed') }}"
    turn_on:
    service: script.fan_on
    data:
    room: "b3"
    turn_off:
    service: script.fan_off
    data:
    room: "b3"
    set_percentage:
    service: script.fan_set_speed
    data:
    percentage: "{{ percentage }}"
    room: "b3"`
weak crystal
#

The ' ~ room ~ ' syntax is new to me...

#

I've only used

          data:
            percentage: "{{ percentage }}"
            room: "b3"

structure and {{ var_name }}

nimble lynx
#

Well, even with the syntax above, the service with the IF syntax isn't calling correctly

#

it calls the ELSE script every time, even if input_select.b3_fan_speed is 0

#

in templating, hardcoring the b3 rather than {{ room }} passes the correct result, which makes me believe that somehow it's not pulling through the {{ room }} variable correctly to call the first option

weak crystal
#

Is the result of this true in template editor?
{% if is_state("input_select.b3_fan_speed", '0') -%}

nimble lynx
#

{% if is_state("input_select.b3_fan_speed", '0') -%} Result 1 {%- else -%} Result 2 {%- endif %}

Where it returns Result 1, and when I change input select to 33, it returns Result 2

weak crystal
#

What does the script trace show after it's been run?

nimble lynx
#

It always return the second result, when input_select.b3_fan_speed is 0, it comes up with this error:
2023-01-10 12:07:24.564 ERROR (MainThread) [homeassistant.components.script.fan_on] Fan On: Error executing script. Service not found for call_service at pos 2: Unable to find service script.fan_unknown 2023-01-10 12:07:24.570 ERROR (MainThread) [homeassistant.helpers.script.bedroom_3_fan] Bedroom 3 Fan: Error executing script. Service not found for call_service at pos 1: Unable to find service script.fan_unknown

#

it should have returned Result 1, or the hard coded script.fan_33, and not Result 2 or script.fan*{{ states('input_select.{{ room }}_fan_speed') }} which seems to have returned as Script.Fan_unknown

weak crystal
#

So this is failing to evaluate correctly when it is 0:
{% if is_state("input_select.{{ room }}_fan_speed", '0') -%}
causing this:
script.fan_{{ states('input_select.{{ room }}_fan_speed') }}
to execute as:
script.fan_unknown

nimble lynx
#

I got it working by lots of fiddling around and reloading 🙂 Thanks for your input though!!

fan_on: alias: Fan On mode: queued sequence: - service: input_boolean.turn_on target: entity_id: input_boolean.{{ room }}_fan_state - service: > {% if is_state('input_select.' ~ room ~ '_fan_speed', '0') -%} script.fan_33 {%- else -%} script.fan_{{ states('input_select.' ~ room ~ '_fan_speed') }} {%- endif %} data: room: "{{ room }}"

weak crystal
#

So, moving the variable outside the quotes!

("input_select.{{ room }}_fan_speed", '0')

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’.

#

(from the HA community post#5)

nimble lynx
#

TBH that post confused me more than helped haha but I think I understand it better now 🙂

#

again, really appreciate your quick responses and help @weak crystal ! Have a great day (or night) ahead!