How to create template sensor that returns sensor value

Hi there!
i need to create a template sensor with some “if states” that, in some of those, returns the value of another sensor.

I tried so:

    pdc_temperatura_mandata_acs_template:
        value_template: >-
            {% if states('sensor.pdc_stato') == '4' %}
            sensor.pdc_temperatura_mandata
            {% else %}
            -
            {% endif %}

but it returns the text “sensor.pdc_temperatura_mandata” and it’s wrong.
But i need when if “sensor.pdc_stato” = 4 the template returns the “sensor.pdc_temperatura_mandata” value.

thanks

    pdc_temperatura_mandata_acs_template:
        value_template: >-
            {% if states('sensor.pdc_stato') == '4' %}
              {{ states('sensor.pdc_temperatura_mandata') }}
            {% else %}
            -
            {% endif %}
1 Like

If you’re just starting to write YAML template sensors, you should be using the modern configuration instead:

template:
  - sensor:
      - name: "PDC temperatura mandata acs template"
        state:

rather than

sensor:
  - platform: template
    sensors:
    pdc_temperatura_mandata_acs_template:
        value_template:

See here:

1 Like

thanks! now it works