Hello to the fantastic Homeassistant community!
I have a problem with a template that I am stuck on.
I have a number of temperature sensors for which I calculate the dew point. However, I am not calculating the dew point for all temperature sensors. The naming is consistent, so all temperature sensor end with ā_temperatureā, and the dewpoint sensors end with ā_dewpointā. So the sensors would be, for example, sensor.kitchen_temperature and sensor.kitchen_dewpoint.
Now I would like to
Perform a comparison with the corresponding temperature sensor for all dewpoint sensors and then output a list of all sensors where the dewpoint is above the corresponding temperature.
It would be nice if I could also output the associated temperature difference next to the corresponding sensor name - but thatās an extra
I can generate the list of all dewpoint sensors:
{% set dewpoint = states.sensor
| selectattr('entity_id', 'search', 'thermohygrometer')
| selectattr('entity_id', 'search', 'dewpoint')
| list
%}
{{ dewpoint }}
However, when comparing with the corresponding temperature sensors, Iām at my wits endā¦ I think it will look something like this:
{% for i in dewpoint %}
{% set ns.result = i.state|int > i.state|replace('dewpoint', 'temperature')|int %}
{% endfor %}
{{ ns.result|join(', ') }}
Thank you very much already!
Is there a tutorial somewhere on how I can better understand and learn the functionality of namspaces and the for loops?
The template you posted selects all sensors whose entity_id contains dewpointorthermohygrometer. You only mentioned dewpoint in your explanation (and the for-loopās replace only handles dewpoint) so why is thermohygrometer in there?
The check for thermohygrometer ensures that I only include readings from my thermohygrometers. Some weather data also provides an entity with ādewpointā in its name, which I do not want to use.
My example above was not detailed enough. For me, the sensors are called: sensor.kitchen_thermohygrometer_temperature and sensor.kitchen_thermohygrometer_dewpoint.
that was almost a precision landing! I didnāt know states(i.entity_id[:-8] ~ 'temperature') yet. Simply remove the last 8 digits and replace them with ātemperatureā. Clever!
With my existing knowledge, I was able to implement the rest: The following template calculates the temperature differences of the respective entities (dewpoint - temperature). Theis temperature difference is assigned to the associated entity with a colon.
This was all I needed. Thank you very much!
{% set dewpoint = states.sensor
| selectattr('entity_id', 'search', 'thermohygrometer')
| selectattr('entity_id', 'search', 'dewpoint')
| list %}
{% set ns = namespace(result = []) %}
{% for i in dewpoint if i.state | float(0) > states(i.entity_id[:-8] ~ 'temperature') | float(0) %}
{% set ns.result = ns.result + [[i.name, i.state | float(0) - states(i.entity_id[:-8] ~ 'temperature') | float(0) ]|join(': ')] %}
{% endfor %}
{{ ns.result|join(',
') }}