Request to add additional attribute_templates to template-based request

Hello community!

Is it possible to add additional “attribute_templates” to a template-based request?

For example, for this “rest” request:

sensor:
  - platform: rest
	name: deepl_translation_expired
	resource_template: >-
		https://api-free.deepl.com/v2/translate?text={{ states.sensor.packages_expired.attributes.packages[0].status | urlencode }}&target_lang=DE
	method: POST
	headers:
		Content-Type: application/x-www-form-urlencoded
		Authorization: !secret deepl_apikey
	payload: ""
	value_template: "{{ value_json.translations[0].text }}"
	json_attributes:
		- translations
	scan_interval: 300
	verify_ssl: true

I would like to add an “attribute_templates” similar to the one used here.

sensor:
  - platform: template
    sensors:
     morgen_abholung:
      icon_template: "mdi:delete-empty"
      friendly_name: "Morgen Abholung"
      value_template: >-
       {% set mr_states = states('sensor.tage_abholung', 'state')[0]%}
       {% if mr_states == "0" %}1{% elif mr_ststes == "1"%}1{% else %}0{% endif %}
      attribute_templates:
       nächste_abholung: >-
       {{ states('sensor.next_waste_collection_daysto') }}

Use the rest integration rather than the rest sensor platform. That way you can create as many sensors as you want from the one call to the resource.

e.g. (in your configuration.yaml file)

rest:
  - resource_template: >-
		https://api-free.deepl.com/v2/translate?text={{ states.sensor.packages_expired.attributes.packages[0].status | urlencode }}&target_lang=DE
    method: POST
    headers:
		Content-Type: application/x-www-form-urlencoded
		Authorization: !secret deepl_apikey
    sensor:
      - name: "Deepl Translation Expired"
        value_template: "{{ value_json.translations[0].text }}"
      - name: "Another Name Here"
        value_template: "{{ value_json.another.path.here }}"
      - name: "Some Other Name"
        value_template: "{{ value_json.some.other.path }}"

Thank you for the quick solution suggestion!

It’s something different that I need. I simply want additional attributes to be added for the newly created “sensor.deepl_translation_####”, which should be extracted, for example, from “states.sensor.seventeentrack_packages_delivered.attributes.packages[0].location” and added as an attribute to the newly created “sensor.deepl_translation_####”.

Your solution suggestion creates additional new sensors, which is not needed in my case. Unfortunately, your solution suggestion is not working, and errors are displayed after restarting.

Logger: homeassistant.setup
Source: setup.py:363
First occurred: 10:33:09 (7 occurrences)
Last logged: 10:33:09

Unable to prepare setup for platform rest.sensor: Unable to set up component.

Logger: homeassistant.setup
Source: helpers/template.py:460
First occurred: 10:33:09 (1 occurrences)
Last logged: 10:33:09

Error during setup of component rest
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 458, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2007, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 326, in getattr
    value = getattr(obj, attribute)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2033, in _fail_with_undefined_error
    raise ex
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2024, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'attributes'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/setup.py", line 256, in _async_setup_component
    result = await task
  File "/usr/src/homeassistant/homeassistant/components/rest/__init__.py", line 79, in async_setup
    return await _async_process_config(hass, config)
  File "/usr/src/homeassistant/homeassistant/components/rest/__init__.py", line 99, in _async_process_config
    rest = create_rest_data_from_config(hass, conf)
  File "/usr/src/homeassistant/homeassistant/components/rest/__init__.py", line 188, in create_rest_data_from_config
    resource = resource_template.async_render(parse_result=False)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 460, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'attributes'

My solution does not have attributes so I have no idea why you are getting these errors:

I don’t know if I check the status with e.g. {{ states.sensor.seventeentrack_packages_delivered.attributes.packages[0].status }} check i get a positive response.

Is there any way to add more attributes?