Compare dewpoint and temperature sensors with a for-loop

Hello to the fantastic Homeassistant community! :partying_face: :heart_eyes:
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 :slight_smile:

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 dewpoint or thermohygrometer. 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.

I hope this makes more sense now :slight_smile:

Copy-paste the following template into the Template Editor and confirm it reports what you want.


{% 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 | int(0) > states(i.entity_id[:-8] ~ 'temperature') | int(0) %}
  {% set ns.result = ns.result + [i.state | int(0)] %}
{% endfor %}
{{ ns.result|join(', ') }}
2 Likes

Dear Taras,

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(', 
') }}
1 Like