Please help trouble shoot value template

Hi all,

First post here. Thank you so very much for this amazing piece of software, and all the work everyone puts into it.

I have been using hassio for about 8 months but havent really had much confidence with any serious config editing. I use node red for most of my automations and confess to being pretty scared of YAML and JSON.

I have a small indoor garden for micro herbs etc. I am running some automations for temperature and humidity etc and all is working lovely. What I’d like to do now is some maths to calculate the VPD based on the output of the temp and humidity sensors, and create a sensor output for it.

I keep getting syntax errors when i check the config that i simply dont understand!

Here is the error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘integer’) for dictionary value @ data[‘sensors’][‘vpd_veg’][‘value_template’]. Got “{% set veg_leaf_temp = ((states(‘sensor.temperature_158d000238bd6e’) | float) -2) %} {% set es = 0.6108 * e ** (17.27 * veg_leaf_temp / (veg_leaf_temp + 237.3)) %} {# Saturation vapor pressure in kPa #} {% set ea = ((states(‘sensor.humidity_158d000238bd6e’) | float) / 100 * es) %} {# Actual Vapor Pressure in kPa #} {% set vpd = (ea - es) %} {{ vpd | round 3}}\n”. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

and here is the relevent part of my config:

sensor:
  - platform: template
    sensors:
      solar_angle_test:
        friendly_name: "Sun test Angle"
        unit_of_measurement: '°'
        value_template: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"
      vpd_veg:
        friendly_name: "VPD Veg"
        unit_of_measurement: 'kPa'
        value_template: >
            {% set veg_leaf_temp = ((states('sensor.temperature_158d000238bd6e') | float) -2) %}
            {% set es = 0.6108 * e ** (17.27 * veg_leaf_temp / (veg_leaf_temp + 237.3)) %} {# Saturation vapor pressure in kPa #}
            {% set ea = ((states('sensor.humidity_158d000238bd6e') | float) / 100 * es) %} {# Actual Vapor Pressure in kPa #}
            {% set vpd = (ea - es) %}
            {{ vpd | round 3}}

Any light shed on this would be very appreciated. I got the basis for the caclulation from this thread: Heat Index / Apparent Temperature / Feels Like Temperature in a post by rasputin. Hes doing something quite similar.

The mechanics i want are to reduce the temp by 2 degrees, then perform this calculation:

Given T is temperature in degrees Celsius, and RH is relative humidity:

Saturation Vapor Pressure (es) =

0.6108 * exp(17.27 * T / (T + 237.3))

Actual Vapor Pressure (ea) =

RH / 100 * es 

Vapor Pressure Deficit =

ea - es

Thanks very much in advance.

You can’t put the comments in the template like that, take them out and see what you get then.

Thanks Marc,

I removed those comments and now i get this error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘integer’) for dictionary value @ data[‘sensors’][‘vpd_veg’][‘value_template’]. Got “{% set veg_leaf_temp = ((states(‘sensor.temperature_158d000238bd6e’) | float) -2) %} {% set es = 0.6108 * e ** (17.27 * veg_leaf_temp / (veg_leaf_temp + 237.3)) %} {% set ea = ((states(‘sensor.humidity_158d000238bd6e’) | float) / 100 * es) %} {% set vpd = (ea - es) %} {{ vpd | round 3}}\n”. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

It looks the same but maybe I’m missing something? shrug

Try this:

        value_template: >
            {% set veg_leaf_temp = states('sensor.temperature_158d000238bd6e')|float - 2 %}
            {% set es = 0.6108 *( e ** (17.27 * veg_leaf_temp / (veg_leaf_temp + 237.3))) %}
            {% set ea = states('sensor.humidity_158d000238bd6e')|float / (100 * es) %}
            {% set vpd = ea - es %}
            {{ vpd|round(3) }}

Those are jinja comment tags he used, I believe they are actually valid. They definitely work in the template editor. I don’t believe that is is issue.

{# this is a valid comment in a jinja template #}
{% set x = 1 %}
{# another comment #}
{% if x == 1 %}{% endif %}
{# one more comment #}
{{ x }}

As per Tom’s post, the cause of the error message is the last line in value_template. That lonely 3 is the integer the error message is complaining about.

round is a function so change this:

            {{ vpd | round 3 }}

to this:

            {{ vpd | round(3) }}

Hah!

Wow guys thanks very much. I’ve struggled with this on my own for a looooong time! lol.

Hello,
What changes would be needed if using Fahrenheit? Thanks in advance.