Creating templates for entities of same integration

  1. Is it possible to create templates out of entities of an integration in a for loop of some sort? I don’t want to repeat all the code for each entity when creating the template when the only thing different between each template is the entity being templated. Here’s what I mean. yahoofinance produces stock entities. I’d like to template each one to customize the icon and the state. I don’t want to repeat this huge template for each of these entities. I’d like to apply them to all entities exposed by this integration.

  2. When building a template called x with a template attribute y, is there a way to refer to attribute y inside this template by saying something like self.attribute.y instead of using state_attr(‘sensor.x’, ‘y’)

No one? srsly?

Ever figure anything else for this? I’ve not been able to figure out how to do this, but I’d really like to be able to do at the very least number 2.

I’d love to be able to do:

  - platform: template
        friendly_name: "Jim's Birthday"
        value_template: 1999-05-16
          days_unti: |
            {% set day = self.state[-5:] %}
            {% if now().replace(tzinfo=None) > strptime(now().year | string + day, '%Y%m-%d') %}
              {{ ((as_timestamp(strptime((now().year + 1) | string + "-" + day, '%Y-%m-%d')) - as_timestamp(now()))/86400) | round }}
            {% else %}
              {{ ((as_timestamp(strptime(now().year | string + "-" + day, '%Y-%m-%d')) - as_timestamp(now()))/86400) | round }}
            {% endif %}

Ever figure anything else for this?