# Calculate power for a thermostat valve

I have radiators with thermostat valves which I want to use for an automation.
Each valve has an entity which gives me the current position in %

``````The idea: I would map a power to each radiator and calculate a current power.
template:
- sensor:
- name: "AZ HC power"
unique_id: "az_hc_power"
unit_of_measurement: "W"
state: >
{% set pers, power = states('sensor.az_thermostat_valve_state') | float, 2782 %}
{% if pers == 'unknown' -%}
0
{%- else %}
{{power*pers |round(0)}}
{% endif %}
``````

this works fine but the disadvantage is:

The max power is a value which is direct in the template (here the 2782W)

In the next step I would like to sum up the maximum power and the current power.
As the maximum power is here fix in the template I have no access to it.

How can I make the maximum power (here the 2782W) accessible ?
It would be great if I could add it as an attribute to the device but I have no idea how to do it.

Thank you

I am one step further (may be in the wrong direction ).

I found a possibility to add an attribute:

``````- name: "AZ HC power"
unique_id: "az_hc_power"
unit_of_measurement: "W"
attributes:
state: >
{% set pers = states('sensor.az_thermostat_valve_state') | float %}
{% set power = this.attributes.radiatorpower %}
{% if pers == 'unknown' -%}
0
{%- else %}
{{ power }}
{% endif %}
``````

This leads to the expected output of “2783” for the state.

Problem 1: The expected type for the attribute is a string but I would like to use a float or integer
Problem 2: I can not use “power” or " this.attributes.radiatorpower" for a calculation.

If I use: {{ power | as float }}

I get the error:

``````Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 423, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1950, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 4, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1710, in forgiving_float_filter
return float(value)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 540, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 425, in async_render
raise TemplateError(err) from err