Saving system variables

Hello!
I am a new HomeAssistant user. Started writing code to automate house heating with heat pump and charging electric cars. These activities need to be based on quite a few external variables (electricity price from NordPool, external temperature, internal temperature, and alas also efficiency or COP of the heat pump). As an intelligent method relies on the past and also modified / updated tables defining the building (heat capacitance and heat loss of rooms, actual COP of the heat pump vs external temp, etc), I would need to build a set of 2 or 3 axis tables…
But writing the YAML code (JINJA) for the sensors, I realized I have no idea where to save system variables - so that they would not be lost after a reset and so I can access them in the future… examples: actual measured COP of a heat pump as a function of ext_T, output_T, wind speed, humidity. Then later I would need to take the electricity stock price and the COP into account to understand what the actual price per kWh of heat will be…
I also understand this is a dummy question probably, but would not want to waste time and know what is the best practice for this.
A gentle push to a right direction would be appreciated!
Thanks in advance!

If you’re looking to store things, you can use helpers depending on what you need to store.

settings → Devices & services → helpers

Is that what you’re asking for?

You’re probably also going to want statistics integration to store/access past data trends.

Hi Rob,

Thanks.
Helpers seem to be one way to at least get input variables to any code. However, I cannot write back to the helper variables. If I can read the helper value relatively easily:

{% set charge_time = states('input_number.CarCharge_time') | int -%}

Then I am not sure how could I write some values back to these helpers (and thus “store” variable values)…
I started off with a simpler piece - charging an electric car based on the electricity stock price. For a night time charging (after coming home from office and before leaving in the morning, I wish to enable charging only during x cheapest hours). I have written a code that selects the cheapest hours and makes an array containing the hour (as int) that charging should happen and the corresponding price. Now I am not sure where should I put this code. I want to run this once every day @Car.Charge_Night_Start. And where should I store the result array…

{# Variables read from Helpers -#}
{% set charge_time = states('input_number.CarCharge_time') | int -%}
{% set night_start = states('input_datetime.CarCharge_Night_Start') | replace ("0", "") | replace (":", "") | int -%}
{% set night_end = states('input_datetime.CarCharge_Night_End') | replace ("0", "") | replace (":", "") | int -%}

{# Setting price addition -#}
{% set a = {
  "day_tariff": 0.0521,
  "day_peak_tariff": 0.0802,
  "weekend_peak_tariff": 0.0464,
  "night_tariff": 0.0299,
  "contract_price_addition": 0.0024
          }
-%}

{# Gettting Nordpool prices -  NaN exception handling  should be added-#}
{% set prices = state_attr('sensor.nordpool_kwh_ee_eur_3_10_02', 'today') +
                state_attr('sensor.nordpool_kwh_ee_eur_3_10_02', 'tomorrow') -%}
{# Adding price additions to get final price vector for today + tomorrow -#}
{% set final_prices = namespace(values = []) -%}
{% for price in prices -%}
  {% set x = loop.index0 -%}
  {% if loop.index0 < 24 -%}
    {% set hour = loop.index0 -%}
    {% set day = now().weekday() -%}
  {% elif loop.index0 >= 24 -%}
    {% set hour = loop.index0 - 24 -%}
    {% set day = now().weekday() + 1 -%}
  {% endif -%}
  {% if day < 5 and ((hour >= 7 and hour < 9)
                        or (hour >= 12 and hour < 16)
                        or (hour >= 20 and hour < 22))-%}
    {% set final_prices.values = final_prices.values + 
                        [(price + a.day_tariff + a.contract_price_addition) | round(4) ]  -%}
  {% elif day < 5 and ((hour >= 9 and hour < 12)
                        or (hour >= 16 and hour < 20)) -%}
    {% set final_prices.values = final_prices.values +
                        [(price + a.day_peak_tariff + a.contract_price_addition) | round(4) ]   -%}
  {% elif day >= 5 and hour > 16 and hour < 20 -%}
    {% set final_prices.values = final_prices.values +
                        [(price + a.weekend_peak_tariff + a.contract_price_addition) | round(4) ] -%}
  {% else -%}
    {% set final_prices.values = final_prices.values +
                        [(price + a.night_tariff + a.contract_price_addition) | round(4) ] -%}
{% endif -%}
{% endfor -%}

{#{ final_prices.values }#}
{# making a vector of prices/times during "night charge" time window i.e. from night_start to
                                                                                night_end  -#}
{% set night_charge_prices = namespace(values = [], time = []) -%}
{% for price in final_prices.values -%}
  {% if (loop.index0 >= night_start) and (loop.index0 < (night_end + 24)) -%}
      {% set night_charge_prices.values = night_charge_prices.values + [price] -%}
      {% set night_charge_prices.time = night_charge_prices.time + [loop.index0 % 24]-%}
  {% endif -%}
{% endfor -%}

{{ 'unsorted night prices', night_charge_prices.values}}
{{ 'sorted night prices', night_charge_prices.values | sort }}

{{ 'need to turn on the charger (at hour, price)' }}
{% for price in night_charge_prices.values -%}
  {% if (night_charge_prices.values | sort)[charge_time - 1] >= price  -%}
    {{ night_charge_prices.time[loop.index0], night_charge_prices.values[loop.index0] }}
  {% endif -%}
{% endfor -%}

Help would be much appreciated.