Move a calculation from configuration.yaml to a Helpers

Hi,

Long time ago I have created a sensor in configuration.yaml and I am displaying the value of the sensor in a dashboard.

I would like to move this sensor and its calculation to a Helpers of type Template but I don’t really know what to write in the Helpers. At the same time, I would like to use entities instead of state_attr(xxx) for the calculation.

This is what I have in configuation.yaml:

  - platform: template
    sensors:
      feels_like:
        unique_id: '0a7476cc-d6c1-40ba-8ae1-606518c30004'
        friendly_name: 'Utomhus känns som'
        value_template: >
                        {% set temperature = state_attr('weather.forecast_home', 'temperature') %}
                        {% set humidity = state_attr('weather.forecast_home', 'humidity') %}
                        {% set windspeedmps = state_attr('weather.forecast_home', 'wind_speed') / 3.6 %}
                        {% set wvp = (humidity/100) * 6.105 * e**((17.27*temperature) / (237.7 + temperature))%}
                        {{(temperature + 0.33 * wvp - 0.70 * windspeedmps - 4.00)|round|float}}
        unit_of_measurement: '°C'type or paste code here

Does somebody has an idea how to do this?
Thank’s!

Helpers / Template sensor.

State template is this (exactly as yours, but without the unnecessary float):

{% set temperature = state_attr('weather.forecast_home', 'temperature') %}
{% set humidity = state_attr('weather.forecast_home', 'humidity') %}
{% set windspeedmps = state_attr('weather.forecast_home', 'wind_speed') / 3.6 %}
{% set wvp = (humidity/100) * 6.105 * e**((17.27*temperature) / (237.7 + temperature))%}
{{ (temperature + 0.33 * wvp - 0.70 * windspeedmps - 4.00)|round }}

Unit of measurement is °C, and you can set device class to Temperature if you want.

Like this (using my weather entity instead):

Don’t understand what you mean. The way you’ve done it is the preferred way.

Hi again,

Thank you for your reply, it works perfect! :slight_smile:

What I would like to do is to use entities in the calculation instead of using state_attr(‘xxx’), but I don’t know how to use entities directly in the calculation.

In other words, I would like to replace:

{% set temperature = state_attr('weather.forecast_home', 'temperature') %}

By the right code to be able to assign temperature with the entity “sensor.smart_implant_air_temperature_8_2”

I have tried, to write it like this but I doesn’t work:

{% set temperature1 = state_attr('sensor.smart_implant_air_temperature_8_2') %}

Any idea how to do this?
Thank’s

{% set temperature1 = states('sensor.smart_implant_air_temperature_8_2') | float(0) %}

It works perfect, thank’s!