Dynamically select correct datetime helper based on sensor attribute

Hey everyone, I’m racking my brain here. Maybe someone can point me in the right direction. I have a plant sensor (very basic), but I want an attribute that dynamically points to the right input_datetime using the plant_name attribute (thats the naming convention for the datetime helpers).

- sensor:
    - name: "Avocado"
      unique_id: 7604a08d-1f22-49b8-98e2-e0c9b35a2308
      unit_of_measurement: "Days"
      icon: mdi:sprout
      state: >
        {% set dt = this.attributes.last_watered | default(0) 
          | as_datetime | as_local %}
        {{ (dt.date() - now().date()).days | abs }}
      attributes:
        plant_name: "Persea Americana"
        watering_interval: "7"
        last_watered: >
          "{{ states('input_datetime.' ~ this.attributes.plant_name.replace(' ', '_') | lower) }}"

My sensor goes unavailable as soon as I do this. When I change my sensor as follows:

- sensor:
    - name: "Avocado"
      unique_id: 7604a08d-1f22-49b8-98e2-e0c9b35a2308
      unit_of_measurement: "Days"
      icon: mdi:sprout
      state: >
        {% set dt = this.attributes.last_watered | default(0) 
          | as_datetime | as_local %}
        {{ (dt.date() - now().date()).days | abs }}
      attributes:
        plant_name: "Persea Americana"
        watering_interval: "7"
        test: "{{ states('input_datetime.' ~ this.attributes.plant_name.replace(' ', '_') | lower) }}"
        last_watered: "2025-05-13"

You can see below the output values of “test” and “last_watered” are identical. Still it won’t work when I use the template in the last_watered attribute.

Thanks for taking your time in advance.

Found the fix:

last_watered: >
            {% set pn = this.attributes.plant_name | lower | replace(' ', '_') %}
            {{ states('input_datetime.' ~ pn) }}