Trouble subtracting two variables

Hey guys!

I got a question I can’t figure out. I am trying to create a template sensor to help me judge whether or not it is useful to open the windows to lower the humidity. I can calculate the respective absolute humidity just fine but I am having trouble calculating the offset … it will just not calculate anything but instead turn the sensor to „unavailable“.

variables:
  inside:
    {{ ((e ** ((17.67 * (states('sensor.tuyathsensor2_temperature')|float)) /
    (243.5 + (states('sensor.tuyathsensor2_temperature')|float)) ) * 6.112 *
    (states('sensor.tuyathsensor2_humidity')|float) * 2.1674 ) / (273.15 +
    (states('sensor.tuyathsensor2_temperature')|float))) | round(1) }}
  outside:
    {{ ((e ** ((17.67 * (state_attr('weather.forecast_home',
    'temperature')|float)) / (243.5 + (state_attr('weather.forecast_home',
    'temperature')|float)) ) * 6.112 * (state_attr('weather.forecast_home',
    'humidity')|float) * 2.1674 ) / (273.15 +
    (state_attr('weather.forecast_home', 'temperature')|float))) | round(1) }}
  offset: {{ 'inside' - 'outside' }}
state: {% if 'inside' < 'outside' %} Lüften
 {% else %} Schliessen
 {% endif %}

Also, is there a way to show this offset value as some kind of a gauge?

If you want the template to refer to its own attributes, you refer to them like this:

state: >
  {% if this.attributes.inside < this.attributes.outside %} Lüften
  {% else %} Schliessen
  {% endif %}

However, this may not produce the result you expected because the Template Sensor’s options are processed in this order:

  1. state
  2. the attributes in the order they are defined:
    • inside
    • outside

That means state’s template will be computed first using the current values of inside and outside, then it will compute new values for inside and outside.

In addition, this template contains no entities:

 if this.attributes.inside < this.attributes.outside 

Home Assistant evaluates a template whenever one of its entities changes state but your template contains no entities. However, your two attributes contain entities so their templates will be processed whenever one of their entities changes state. Given the processing order I described earlier, and the fact that there are only entities in the attributes, it may affect how and when the value of state is computed.

Thanks for your reply.

I got that working so far however I am having trouble with the offset variable … doesn’t mutter how I put it in, it simply won’t calculate the difference … is there any trick to substracting two variables?

This should do the trick.

template
- trigger:
  - platform: homeassistant
    event: start
  - platform: state
    entity_id:
    - sensor.tuyathsensor2_temperature
    - sensor.tuyathsensor2_humidity
    - weather.forecast_home
  action:
  - variables:
      config:
        inside:
          temperature: "{{ states('sensor.tuyathsensor2_temperature') }}"
          humidity: "{{ states('sensor.tuyathsensor2_humidity') }}"
        outside:
          temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}"
          humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}"
      calculation: >
        {% set ns = namespace(items=[]) %}
        {% for location, item in config.items() %}
          {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %}
          {% set ns.items = ns.items + [ (location, calculation) ] %}
        {% endfor %}
        {{ dict.from_keys(ns.items) }}
      offset: "{{ calculation.inside - calculation.outside }}"
  sensor:
  - name: ....
    state: "{{ iif(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}"

your calculations aren’t working because you’re wrapping your variables in quotes. 'inside' is a string, the literal word “inside”. If you want to tread “inside” as a variable to access it’s information, use inside (No quotes).

1 Like

Thanks for your reply and your help and sorry for only getting back so late.
Pasting in the solution like this will still only produce an „Unavailable“ even correcting the small typo with the If in the last line.

I can’t figure out what I am doing wrong here and sadly Home Assistants error messages aren’t really helpful.

Post the error in your logs.

First one:


Logger: homeassistant.helpers.template
Source: helpers/template.py:2652
First occurred: July 9, 2024 at 13:00:00 (13 occurrences)
Last logged: 14:18:09

Template variable error: 'dict object' has no attribute 'from_state' when rendering 'Feuchtigkeit ist zu hoch. Lüften. Auslöser: {{ trigger.from_state.attributes.friendly_name }}. Absolute Luftfeuchtigkeit Innen {{ inside }}, Außen {{ outside }}.'
Template variable error: 'config' is undefined when rendering 'template - trigger: - platform: homeassistant event: start - platform: state entity_id: - sensor.tuyathsensor2_temperature - sensor.tuyathsensor2_humidity - weather.forecast_home action: - variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: .... state: "{{ iif(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}"'
Template variable error: 'config' is undefined when rendering 'template - trigger: - platform: homeassistant event: start - platform: state entity_id: - sensor.tuyathsensor2_temperature - sensor.tuyathsensor2_humidity - weather.forecast_home action: - variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: Test state: "{{ if (calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}"'
Template variable error: 'config' is undefined when rendering 'variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home','temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" state: "{{ if (calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}"'
Template variable error: 'config' is undefined when rendering 'template - trigger: - platform: homeassistant event: start - platform: state entity_id: - sensor.tuyathsensor2_temperature - sensor.tuyathsensor2_humidity - weather.forecast_home action: - variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: .... state: "{{ if(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}"'

Second:


Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:197
integration: Template
First occurred: July 6, 2024 at 22:35:47 (158 occurrences)
Last logged: 14:18:09

TemplateError('UndefinedError: 'config' is undefined') while processing template 'Template<template=(-variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: Test state: "{{ if(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}") renders=4>' for attribute '_attr_native_value' in entity 'None'
TemplateError('UndefinedError: 'config' is undefined') while processing template 'Template<template=(variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: Test state: "{{ if(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}") renders=4>' for attribute '_attr_native_value' in entity 'None'
TemplateError('UndefinedError: 'config' is undefined') while processing template 'Template<template=(template - trigger: - platform: homeassistant event: start - platform: state entity_id: - sensor.tuyathsensor2_temperature - sensor.tuyathsensor2_humidity - weather.forecast_home action: - variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: .... state: "{{ iif(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}") renders=4>' for attribute '_attr_native_value' in entity 'None'
TemplateError('UndefinedError: 'config' is undefined') while processing template 'Template<template=(template - trigger: - platform: homeassistant event: start - platform: state entity_id: - sensor.tuyathsensor2_temperature - sensor.tuyathsensor2_humidity - weather.forecast_home action: - variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: .... state: "{{ if(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}") renders=4>' for attribute '_attr_native_value' in entity 'None'
TemplateError('UndefinedError: 'config' is undefined') while processing template 'Template<template=(template - trigger: - platform: homeassistant event: start - platform: state entity_id: - sensor.tuyathsensor2_temperature - sensor.tuyathsensor2_humidity - weather.forecast_home action: - variables: config: inside: temperature: "{{ states('sensor.tuyathsensor2_temperature') }}" humidity: "{{ states('sensor.tuyathsensor2_humidity') }}" outside: temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}" humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}" calculation: > {% set ns = namespace(items=[]) %} {% for location, item in config.items() %} {% set calculation = (e ** ((17.67 * item.temperature) / (243.5 + item.temperature)) * 6.112 * item.humidity * 2.1674 ) / (273.15 + item.temperature) %} {% set ns.items = ns.items + [ (location, calculation) ] %} {% endfor %} {{ dict.from_keys(ns.items) }} offset: "{{ calculation.inside - calculation.outside }}" sensor: - name: .... state: "{{ if(calculation.inside < calculation.outside, 'Lüften', 'Schliessen') }}") renders=4>' for attribute '_attr_native_value' in entity 'sensor.testtemp'

The first error is unrelated.

The 2nd and 3rd tell me that you didn’t copy/paste what I wrote into your configuration. You must have copy/pasted it into the UI. This will only work in configuration.yaml.

Yeah I just noticed the first error in the original post was unrelated.

I copied your solution 1:1 in the template sensor only correcting the „if“ typo on the last line.

please read what I wrote, you cannot paste that into the UI. Do not correct the iif typo, it’s not a typo.

Thanks again for the reply.
I‘ve know copy and pasted it into the confirguration.yaml only giving it a sensor name in the second to last line.

Reloaded the configuration.yaml … while the template now shows up in the developers tab it’s state is still unknown. No related errors in the log.

It will update when your entities change state.

Thanks for your time and your patience, it appears to be working.
Is there some option to display the value of offset as some type of gauge?