Retrieve multiple values with one template

I am new with templates. I am trying to program my HVAC system based on certain conditions. I made several conditions in one template and I would like to retrieve multiple outcomes of the function to different things. For instance I want the variable HVAC_controller to tell what state the HVAC is in, and show this in a dashboard. But the HVAC_power needs to be used as input for the HVAC system.

This is what I currently have, and it retrieves only the tekst as state. What I want is to use both values in a separate state. What direction do I have to search to achieve this?

##### Ventilator power control #####
- platform: template
  sensors:
    ventilatie_trigger:
      friendly_name: Ventileren
      value_template: >-
                  {###scene variables (off, low, mid, high, auto)###}
                  {% set HVAC_scene = states('input_select.hvacstates') %} 

                  {###Trigger variables###}
                  {% set fire_alarm = 'off' %}
                  {% set toilet_lights = 'off' %}
                  {% set cooking_fan = 00| int %}

                  {###Temperature variables###}
                  {% set temperature_setpoint = states('input_number.temperature_setpoint')| int %}
                  {% set indoor_temp = states('sensor.thermometer_1_temperature')| int %}
                  {% set outdoor_temp = states('sensor.temperature')| int %}
                  {% set max_indoor_temp = max(
                  states('sensor.thermometer_1_temperature')| int,
                  states('sensor.thermometer_2_temperature')| int,
                  states('sensor.thermometer_3_temperature')| int) %}

                  {###Humidity variables###}
                  {% set humidity_setpoint = states('input_number.humidity_setpoint')| int %}
                  {% set humidity_sensor_internal_1 = 40| int %}
                  {% set outdoor_RH = states('sensor.humidity')| int %}
                  {% set indoor_RH = states('sensor.thermometer_1_humidity')| int %}
                  {% set max_indoor_humidity = max(
                  states('sensor.thermometer_1_humidity')| int,
                  states('sensor.thermometer_2_humidity')| int,
                  states('sensor.thermometer_3_humidity')| int) %}

                  {###Calculated variables###}
                  {% set outdoor_dewpoint = 243.04*(log(outdoor_RH/100)+(17.625*outdoor_temp/(243.04+outdoor_temp)))/(17.625-log(outdoor_RH/100)-(17.625*outdoor_temp/(243.04+outdoor_temp))) %}
                  {% set indoor_RH_prediction = 100*(e**(17.625*outdoor_dewpoint/(243.04+outdoor_dewpoint))/e**(17.625*indoor_temp/(243.04+indoor_temp))) %}

                  {###Base hvac settings###}
                  {% set HVAC_power = 50|int %}

                  {###Manual setpoints###}
                  {% if HVAC_scene == 'Off' %}
                  {% set HVAC_power = 0|int %}
                  {% set HVAC_controller = 'Manual' %}
                  {% endif %}

                  {% if HVAC_scene == 'Low' %}
                  {% set HVAC_power = 10|int %}
                  {% set HVAC_controller = 'Manual' %}
                  {% endif %}

                  {% if HVAC_scene == 'Midium' %}
                  {% set HVAC_power = 55|int %}
                  {% set HVAC_controller = 'Manual' %}
                  {% endif %}

                  {% if HVAC_scene == 'High' %}
                  {% set HVAC_power = 100|int %}
                  {% set HVAC_controller = 'Manual' %}
                  {% endif %}

                  {###Automated###}
                  {% if HVAC_scene == 'Auto' %}
                  {###Ventilation based on humidity###}
                  {% if max_indoor_humidity > humidity_setpoint %}
                        {% if indoor_RH_prediction < max_indoor_humidity%}
                        {% set HVAC_power = 100|int %}
                        {% set HVAC_controller = 'Auto - humidity control' %}
                        {% else %}
                        {% set HVAC_power = 10|int %}
                        {% set HVAC_controller = 'Auto - humidity control' %}
                        {% endif %}
                  {% else %}
                  {% set HVAC_power = 50|int %}
                  {% set HVAC_controller = 'Auto - base state' %}
                  {% endif %}

                  {###Ventilation based on temperature###}
                  {% if HVAC_power == 50 %}
                  {% if max_indoor_temp > temperature_setpoint and outdoor_temp < temperature_setpoint%}
                        {% set HVAC_power = 100|int %}
                        {% set HVAC_controller = 'Auto - temperature control' %}
                  {% else %}
                        {%if outdoor_temp > temperature_setpoint%}
                        {% set HVAC_power = 10|int %}
                        {% set HVAC_controller = 'Auto - temperature control' %}
                        {% else %}
                        {% set HVAC_power = 50|int %}
                        {% set HVAC_controller = 'Auto - base state' %}
                  {% endif %}
                  {% endif %}
                  {% endif %}

                  {###end auotmated scene###}
                  {% endif %}

                  {###General (always applicable###}
                  {###showering###}
                  {% if HVAC_power > 1 %}
                  {% if humidity_sensor_internal_1 > humidity_setpoint%}
                  {% set HVAC_power = 100|int %}
                  {% set HVAC_controller = 'Showering' %}
                  {% endif %}
                  {% endif %}

                  {###toilet###}
                  {% if HVAC_power > 1 %}
                  {% if toilet_lights == 'on' %}
                  {% set HVAC_power = 100|int %}
                  {% set HVAC_controller = 'Toilet usage' %}
                  {% endif %}
                  {% endif %}

                  {###cooking###}
                  {% if HVAC_power > 1 %}
                  {% if cooking_fan > 100%}
                  {% set HVAC_power = 100|int %}
                  {% set HVAC_controller = 'Cooking' %}
                  {% endif %}
                  {% endif %}

                  {###cooking###}
                  {% if HVAC_power > 1 %}
                  {% if fire_alarm == 'on' %}
                  {% set HVAC_power = 0|int %}
                  {% set HVAC_controller = 'Fire' %}
                  {% endif %}
                  {% endif %}
                  Ventilation at {{HVAC_power}}%
                  Ventilation control type : {{HVAC_controller}}
      icon_template: mdi:hvac

You could put one value in the state and the other values in attributes of the sensor.

You should be using the new template sensor format for new sensors. The Legacy template sensor you are using will not be getting any new features (like state_class).

1 Like

Tnx, I’m going to look into the new template format. I just found out how this works, and read somewhere else as well that this was the old style.

To solve my issue I also tried to return lists or dicts, but found out HA only returns strings. After 6 hours of desperately trying everything I could think of, I found a workaround to this problem.

I now have the entire logic in a reusable custom template as a function. This function ends with a dictionary with all the variables in it. HA can only retrieve one of these values at a time. The function requires one variable which is the string of the variable I want to retrieve. I use this as a filter to retrieve the right variabel.

In short:

{% set output = ({"HVAC_power": HVAC_power, "HVAC_controller": HVAC_controller})%}
{{ output[component] }}

I can now set new sensors for every variable I want to retrieve from this function. I want to retrieve more variables like predicted RH for instance.

It’s a huge workaround, but it functions.

?

The value of an entity’s state property is always a string (and only up to 255 characters) but an entity’s attributes can store a value of any type (boolean, dict, list, float, etc) up to a maximum of 16KB.

Similarly, a template can report its value as a list, string, float, dict, etc.

Ok this sounds like it’s going to be what I need. What kind of attribute can be used to store data like this than?

Refer to the Template integration’s documentation for attributes. There’s an example here.

1 Like

Tnx! I changed the format to the new template sensor format, and got the attributes working. I passed a dictionary of output values through an attribute called data and used this in separate sensors

I stored the code in a separate templates.yaml. To get this working I had to include the templates.yaml in the configuration.yaml file with the following line:

template: !include templates.yaml

The templates.yaml file looks like this:

- sensor:
      - name: "ventilatie_trigger"
        state: >
            see attributes
        attributes:
            data: >
                  The long code with all the if/else logic
                  {% set output = ({"HVAC_power": HVAC_power, 
                                    "HVAC_controller": HVAC_controller,
                                    "indoor_RH_prediction": indoor_RH_prediction})%}
                  {{output}}

- sensor:
      - name: "ventilator_power"
        unit_of_measurement: "%"
        state: >
            {{state_attr('sensor.ventilatie_trigger','data')["HVAC_power"]}}

- sensor:
      - name: "ventilator_controller"
        state: >
            {{state_attr('sensor.ventilatie_trigger','data')["HVAC_controller"]}}

- sensor:
      - name: "predicted_RH"
        unit_of_measurement: "%"
        state: >
            {{state_attr('sensor.ventilatie_trigger','data')["indoor_RH_prediction"]|round}}

It works works fine now.