I have an automation where I need to compute some values to be used in multiple actions. the variables: section should be used for that. However to make it efficient I want to do the computation for multiple variables in one go. That seems not to be possible. This definition of the variables section is not accepted:
variables: >-
{% set ns = namespace() %}
{% set ns.highamprice = -1 %}
{% set ns.highpmprice = -1 %}
{% set ns.highamhour = 0 %}
{% set ns.highpmhour = 0 %}
{% for item in state_attr("sensor.epex_average_electricity_price", "prices_today") %}
{% set hour = as_datetime(item.time).hour %}
{% set price = item.price|float %}
{% if hour < 12 %}
{% if price > ns.highamprice %}
{% set ns.highamprice = price %}
{% set ns.highamhour = hour %}
{% endif %}
{% else %}
{% if price > ns.highpmprice %}
{% set ns.highpmprice = price %}
{% set ns.highpmhour = hour %}
{% endif %}
{% endif %}
{% endfor %}
highamhour: '{{ ns.highamhour }}'
highpmhour: '{{ ns.highpmhour }}'
highamprice: '{{ ns.highamprice }}'
highpmprice: '{{ ns.highpmprice }}'
A template per variable is working ok.
Anyone knows a way around this.