Developer tools - Template says it's fine, but Developer tools - Action gives " Failed to perform the action shell_command.send_ifeel. UndefinedError: 'nst' is undefined"

This code in configuration.yaml

shell_command:
  send_ifeel: |
    {% set nst = namespace(temp = state_attr('climate.neviweb130_climate_salon', 'current_temperature')|int(20)) %}
    {% set nsl = namespace(list=[]) %}
    {% for bit in range(0, 8) %} {% set nsl.list = nsl.list + ["630", (nst.temp / 2**bit)  | int | bitwise_and(1) | iif ("1680","550")] %}{% endfor %}
    wget --spider -q http://192.168.2.175/cm?cmnd='IRSend 0,5980,3015,{{ nsl.list | join(",")}},630,1680,630,580,630,1680,630,580,630,580,630,1680,630,580,630,1680,630'

Works while running it in the Developer tools - Template. It outputs, as expected
wget --spider -q http://192.168.2.175/cm?cmnd='IRSend 0,5980,3015,630,550,630,1680,630,550,630,550,630,1680,630,550,630,550,630,550,630,1680,630,580,630,1680,630,580,630,580,630,1680,630,580,630,1680,630'

But after restarting Home Assistant (so the entry in configuration.yaml takes effect), running it with Developer tools - Action

action: shell_command.send_ifeel
data: {}

It gives
Failed to perform the action shell_command.send_ifeel. UndefinedError: 'nst' is undefined

I don’t know what I’m doing wrong. At first, temp wasn’t inside a namespace keyword (although it didn’t need one in the Developer tools - Template) and I got the same error but for ‘temp’ instead of ‘nst’, so I added the namespace keyword, just in case, but it still doesn’t want to run. I’m at lost at why it gives this error.

PS. Yes, I do plan on changing this to a mqtt statement but for now, I just want to get it to work.

Thanks.

Does rthis post help? Shell command with parameters not working - #15 by koying

I bit the bullet and replaced the shell commands by mqtt publish commands instead. It’s cleaner that way.

Not sure why you’re even using namespace for temp there.

shell_command:
  send_ifeel: >
    {%- set temp = state_attr('climate.neviweb130_climate_salon', 'current_temperature')|int(20) %}
    {%- set ns = namespace(items=[]) %}
    {%- for bit in range(0, 8) %}{%- set ns.items = ns.items+ ["630", (temp / 2**bit) | int | bitwise_and(1) | iif ("1680","550")] %}{%- endfor %}
    wget --spider -q http://192.168.2.175/cm?cmnd='IRSend 0,5980,3015,{{ ns.items | join(",")}},630,1680,630,580,630,1680,630,580,630,580,630,1680,630,580,630,1680,630'

Because it wasn’t available in the for loop in the Developer Tools Template unless it was in a namespace.

Nope, that’s not how for loops work. Namespace is only needed in order to create and add to a list or modify a variable within the loop and have it be retained for the next iteration. All other variables are available in the for loop as long as you aren’t altering it. Which is what you’re doing. I.e. You only need namespace for the list.

Secondly, you could have used the same namespace for both the list and the temp but you created 2. That could be causing some of the problems but I doubt it.

Anyways, the code I posted above should work.

Read the first paragraph here:

When using templates, shell_command runs in a more secure environment which doesn’t allow any shell helpers like automatically expanding the home directory character (~), using pipe symbols (|) to run multiple commands, or operators redirecting output (such as > and >>). Similarly, only content after the first space can be generated by a template. This means the command name itself cannot be generated by a template, but it must be literally provided.

You can probably work around those, but it may be easier to generate the output in a template input text helper outside of the shell command, then just reference that helper in the wget command.