Need template help ... entity not created

I’m puzzled why this template doesn’t create sensor.wp_cop_verwarming_vandaag (“Entity not available” is displayed in Lovelace dashboard). Both wp_energie_uit_verwarming_vandaag and wp_energie_in_verwarming_vandaag show valid values in the same dashboard.
When I test the template in Developer Tools/Template a value is rendered.

What am I overlooking?

  - sensor:
      - name: "WP-COP CV Vandaag"
        unique_id: "wp_cop_verwarming_vandaag"
        state_class: measurement
        unit_of_measurement: ""
        state: >-
          {% if has_value('sensor.wp_energie_uit_verwarming_vandaag') %}
            {% set wp_energie_uit = states('sensor.wp_energie_uit_verwarming_vandaag') | float(0) %}
          {% else %}
            {% set wp_energie_uit = 0 %}
          {% endif %}
          {% if has_value('sensor.wp_energie_in_verwarming_vandaag') %}
            {% set wp_energie_in = states('sensor.wp_energie_in_verwarming_vandaag') | float(0) %}
          {% else %}
            {% set wp_energie_in = 0 %}
          {% endif %}
          {% if (wp_energie_in != 0) %}
            {{ (wp_energie_uit / wp_energie_in) | float(0) | round(2) }}
          {% else %}
            {{ 0 }}
          {% endif %}         

All your if has_value() functions are pointless. The |float(0) filters you are also using will replace unknown and unavailable states with 0 anyway. That’s what the 0 in the brackets is for. A default value if the state can’t be converted to a number.

This will do exactly the same thing:

  - sensor:
      - name: "WP-COP CV Vandaag"
        unique_id: "wp_cop_verwarming_vandaag"
        state_class: measurement
        unit_of_measurement: " "
        state: >
          {% set wp_energie_uit = states('sensor.wp_energie_uit_verwarming_vandaag') | float(0) %}
          {% set wp_energie_in = states('sensor.wp_energie_in_verwarming_vandaag') | float(0) %}
          {% if (wp_energie_in != 0) %}
            {{ (wp_energie_uit / wp_energie_in) | float(0) | round(2) }}
          {% else %}
            {{ 0 }}
          {% endif %}

If this is the first template sensor you have created you need to restart home assistant. If you already have template sensors you can use the reload option.

Thanks Tom, in particular for enlightening me about the (mis-)use of has_value(). I have not found a proper explanation when to test for this and when it is not required.

Unfortunately your code gave the same result (ie “Entity not available”) although it also evaluated correctly in Developer/Template. It didn’t make a difference whether I restarted Home Assistant or just reloaded all yaml files.

This is the reponse I see in my dashboard:

A Template Sensor’s entity_id is derived from its name option, not its unique_id option.

You defined it as this:

name: "WP-COP CV Vandaag"

So the resulting entity_id will be:

sensor.wp_cop_cv_vandaag

Edit your card so that it points to the correct entity_id.

Thank you.

The odd thing is that I changed the other day the name of a number of entities without changing the unique_id at the same time. Probably because the entities were already created I can still use the sensor name as it was originally created.
Example: in yaml I changed the original name WP-Energie IN verwarming laatste uur to WP-Energie IN verwarming laatste uur (2291). However, I can still use the original name (without (2291)) in my dashboard and see the correct value.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps other users find answers to similar questions.

Probably because changing an entity’s name after it has been created won’t alter the entity’s entity_id (assuming it has a unique_id and its value remains unchanged).