How to render data template before call to script?

Hello

I have been trying this for a long time, in all kinds of ways.

I have a script that takes two parameters (fields temperature and useoffsets))

alias: SetTemperature
description: Set the Temperature
icon: mdi:home-thermometer
fields:
  temperature:
    selector:
      number:
        min: 5
        max: 32
        step: 0.5
    name: Temperature
    description: Temperature to set
    required: true
    default: 15
  useoffsets:
    selector:
      boolean: {}
    name: useoffsets
    required: true
    description: Use Thermostat Offset or not
sequence:
  - action: input_select.set_options
    metadata: {}
    data:
      options:
        - On/Automatic
    target:
      entity_id: input_select.heating_mode
  - action: input_number.set_value
    target:
      entity_id: input_number.set_bedroom_temperature
    data:
      value: >-
        {% if false==useoffsets %}  {{ temperature|float }}  {% else %} {{
        states('input_number.offset_bedroom_temperature')|float +
        temperature|float }} {% endif %}

When I call this script from an automation, then it works perfectly well…

alias: New automation
description: ""
triggers: []
conditions: []
actions:
  - action: script.settemperature
    data:
      temperature: "{{ states('input_number.temperature_day')|float}}"
      useoffsets: true
mode: single

However, when I call from a button (the second one) on an entities card
(The first button with constant value 10 works perfectly)

type: entities
state_color: true
entities:
  - type: button
    name: 10 degrees
    entity: script.settemperature
    icon: mdi:thermometer-low
    tap_action:
      action: perform-action
      perform_action: script.settemperature
      data:
        temperature: 10
        useoffsets: false
  - type: button
    name: Day temperature
    entity: script.settemperature
    icon: mdi:thermometer-low
    tap_action:
      action: perform-action
      perform_action: script.settemperature
      data:
        temperature: "{{ states('input_number.temperature_day')|float }}"
        useoffsets: true

then I get:

Oct 13 13:46:09 HomeAssistant hass[1470]: 2024-10-13 13:46:09.120 ERROR (MainThread) [homeassistant.components.script.settemperature] SetTemperature: Error executing script. Error for call_service at pos 2: Error rendering data template: ValueError: Template error: float got invalid input '{{ states('input_number.temperature_day')|float }}' when rendering template '{% if false==useoffsets %}  {{ temperature|float }}  {% else %} {{ states('input_number.offset_bedroom_temperature')|float + temperature|float }} {% endif %}' but no default was specified
Oct 13 13:46:09 HomeAssistant hass[1470]: 2024-10-13 13:46:09.122 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [2441779928] Unexpected exception
Oct 13 13:46:09 HomeAssistant hass[1470]: Traceback (most recent call last):
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 2348, in forgiving_float_filter
Oct 13 13:46:09 HomeAssistant hass[1470]:     return float(value)
Oct 13 13:46:09 HomeAssistant hass[1470]:            ^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]: ValueError: could not convert string to float: "{{ states('input_number.temperature_day')|float }}"
Oct 13 13:46:09 HomeAssistant hass[1470]: During handling of the above exception, another exception occurred:
Oct 13 13:46:09 HomeAssistant hass[1470]: Traceback (most recent call last):
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 632, in async_render
Oct 13 13:46:09 HomeAssistant hass[1470]:     render_result = _render_with_context(self.template, compiled, **kwargs)
Oct 13 13:46:09 HomeAssistant hass[1470]:                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 2729, in _render_with_context
Oct 13 13:46:09 HomeAssistant hass[1470]:     return template.render(**kwargs)
Oct 13 13:46:09 HomeAssistant hass[1470]:            ^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/jinja2/environment.py", line 1304, in render
Oct 13 13:46:09 HomeAssistant hass[1470]:     self.environment.handle_exception()
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/jinja2/environment.py", line 939, in handle_exception
Oct 13 13:46:09 HomeAssistant hass[1470]:     raise rewrite_traceback_stack(source=source)
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "<template>", line 1, in top-level template code
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 2351, in forgiving_float_filter
Oct 13 13:46:09 HomeAssistant hass[1470]:     raise_no_default("float", value)
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 1905, in raise_no_default
Oct 13 13:46:09 HomeAssistant hass[1470]:     raise ValueError(
Oct 13 13:46:09 HomeAssistant hass[1470]: ValueError: Template error: float got invalid input '{{ states('input_number.temperature_day')|float }}' when rendering template '{% if false==useoffsets %}  {{ temperature|float }}  {% else %} {{ states('input_number.offset_bedroom_temperature')|float + temperature|float }} {% endif %}' but no default was specified
Oct 13 13:46:09 HomeAssistant hass[1470]: The above exception was the direct cause of the following exception:
Oct 13 13:46:09 HomeAssistant hass[1470]: Traceback (most recent call last):
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 414, in async_prepare_call_from_config
Oct 13 13:46:09 HomeAssistant hass[1470]:     render = template.render_complex(config[conf], variables)
Oct 13 13:46:09 HomeAssistant hass[1470]:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 247, in render_complex
Oct 13 13:46:09 HomeAssistant hass[1470]:     render_complex(key, variables, limited, parse_result): render_complex(
Oct 13 13:46:09 HomeAssistant hass[1470]:                                                            ^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 253, in render_complex
Oct 13 13:46:09 HomeAssistant hass[1470]:     return value.async_render(variables, limited=limited, parse_result=parse_result)
Oct 13 13:46:09 HomeAssistant hass[1470]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/template.py", line 634, in async_render
Oct 13 13:46:09 HomeAssistant hass[1470]:     raise TemplateError(err) from err
Oct 13 13:46:09 HomeAssistant hass[1470]: homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input '{{ states('input_number.temperature_day')|float }}' when rendering template '{% if false==useoffsets %}  {{ temperature|float }}  {% else %} {{ states('input_number.offset_bedroom_temperature')|float + temperature|float }} {% endif %}' but no default was specified
Oct 13 13:46:09 HomeAssistant hass[1470]: The above exception was the direct cause of the following exception:
Oct 13 13:46:09 HomeAssistant hass[1470]: Traceback (most recent call last):
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/components/websocket_api/commands.py", line 245, in handle_call_service
Oct 13 13:46:09 HomeAssistant hass[1470]:     response = await hass.services.async_call(
Oct 13 13:46:09 HomeAssistant hass[1470]:                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/core.py", line 2761, in async_call
Oct 13 13:46:09 HomeAssistant hass[1470]:     response_data = await coro
Oct 13 13:46:09 HomeAssistant hass[1470]:                     ^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/core.py", line 2804, in _execute_service
Oct 13 13:46:09 HomeAssistant hass[1470]:     return await target(service_call)
Oct 13 13:46:09 HomeAssistant hass[1470]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/components/script/__init__.py", line 718, in _service_handler
Oct 13 13:46:09 HomeAssistant hass[1470]:     response = await self._async_start_run(
Oct 13 13:46:09 HomeAssistant hass[1470]:                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/components/script/__init__.py", line 674, in _async_start_run
Oct 13 13:46:09 HomeAssistant hass[1470]:     script_result = await coro
Oct 13 13:46:09 HomeAssistant hass[1470]:                     ^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/components/script/__init__.py", line 707, in _async_run
Oct 13 13:46:09 HomeAssistant hass[1470]:     return await self.script.async_run(script_vars, context)
Oct 13 13:46:09 HomeAssistant hass[1470]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 1795, in async_run
Oct 13 13:46:09 HomeAssistant hass[1470]:     return await asyncio.shield(create_eager_task(run.async_run()))
Oct 13 13:46:09 HomeAssistant hass[1470]:            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 463, in async_run
Oct 13 13:46:09 HomeAssistant hass[1470]:     await self._async_step(log_exceptions=False)
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 527, in _async_step
Oct 13 13:46:09 HomeAssistant hass[1470]:     self._handle_exception(
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 557, in _handle_exception
Oct 13 13:46:09 HomeAssistant hass[1470]:     raise exception
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 525, in _async_step
Oct 13 13:46:09 HomeAssistant hass[1470]:     await getattr(self, handler)()
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/script.py", line 734, in _async_call_service_step
Oct 13 13:46:09 HomeAssistant hass[1470]:     params = service.async_prepare_call_from_config(
Oct 13 13:46:09 HomeAssistant hass[1470]:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 13 13:46:09 HomeAssistant hass[1470]:   File "/home/hass/lib/python3.12/site-packages/homeassistant/helpers/service.py", line 421, in async_prepare_call_from_config
Oct 13 13:46:09 HomeAssistant hass[1470]:     raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
Oct 13 13:46:09 HomeAssistant hass[1470]: homeassistant.exceptions.HomeAssistantError: Error rendering data template: ValueError: Template error: float got invalid input '{{ states('input_number.temperature_day')|float }}' when rendering template '{% if false==useoffsets %}  {{ temperature|float }}  {% else %} {{ states('input_number.offset_bedroom_temperature')|float + temperature|float }} {% endif %}' but no default was specified

It is as if the template does not get rendered, when used in a button …but it does get rendered when executed from an Automation… ???

What am I doing wrong ?

If you have any ideas on how to fix my script call in the button, they are very welcome :slight_smile:

HC

Correct. Very few core cards support templates.

Pass the entity id to your script and put the template in your script.

Thank you for your feed back :slight_smile:

Am I to understand that the Buttons on cards (and perhaps elsewhere) does not use the actions used by e.g. automations (which does support templates) ?
Basically that: “Actions” are not simply “Actions” :open_mouth:

This is a template:

Most core cards do not support them.

The actions performed by the frontend (dashboard cards) are not the same as actions performed by the backend (scripts and automations).

1 Like