New calculated Attribute in Humidity Sensor Object

Hi everyone,

for a simple window open/close decision/action I would like to calculate the absolute water content of air at two wifi hudity sensors (inside and out). I do have the formula in excel, however no clue how and where to attack this… any ideas where those objects are defined?

The outcome would be an additional object attribute of the sensor showing the water content in g/m³ with which we can decide to open the window for drying a basement or room for example or if it’s better to keep it shut.

Kind regards,

The attributes of any given entity are defined by their integration. AFAIK, there isn’t an easy way to add attributes to existing entities. Instead, use a Template Sensor to perform your calculations.

If the integration would ESPhome and the device reading the sensorvalue an ESP the code should be implemented on the ESP, since the calculation becomes useless in case the input sensor readings aren’t available (powercut, connectionloss, …)
I’d say Tobias must pass on more information.

Basically it is an Tuya/Smart Life ESP device, however a standard Ali-one… guess it should be fine though if the calculation is done off-sensor. As long as both temperature and humidity are available that should work out. It doesn’t have a standard port for flashing, so it’d be great if Drews proposal works, will try this evening getting it implemented.

Here’s the HA YAML for both the dew-point and the absolute humidity for off-sensor calculation:

  - platform: template
        friendly_name: "PIR3 Attic Dewpoint"
        value_template: >-
          {% set H = states('sensor.multisensor_gen5_humidity_3') | float(default=0) %}
          {% set T = states('sensor.multisensor_gen5_air_temperature_3') | float(default=0) %}
          {% set b = 18.678 %}
          {% set c = 257.14 %}
          {% set d = 234.5 %}
          {% set gamma = log(H / 100 * e ** ((b - T / d) * (T / (c + T)))) %}
          {% set Tdp = ((c * gamma / (b - gamma))) | round(2) %}
          {{ Tdp |float |round(1) }}
        unit_of_measurement: "C"
        device_class: temperature

        friendly_name: "PIR1 Absolute Humidity"
        value_template: >-
          {% set H = states('sensor.multisensor_gen5_humidity') | float(default=0) %}
          {% set T = states('sensor.multisensor_gen5_air_temperature') | float(default=0) %}
          {% set mw = 18.01534 %}
          {% set r = 8.31447215 %}
          {% set AHgm3 = (6.112 * e** ((17.67 * T) / (T +243.5)) * H * mw) / ((273.15 + T) * r) %}
          {% if AHgm3 |float > 0 %}
          {{ AHgm3 |float |round(1) }}
          {%- else -%}
          {%- endif -%}
        unit_of_measurement: "g/m3"

And here’s the equivalent for ESPHome devices which I borrowed from another posting:

  - platform: bme280
      name: "Shed-Sensor Temperature"
      id: bme280_temperature
      accuracy_decimals: 2
      name: "Shed-Sensor Pressure"
      id: bme280_pressure
      accuracy_decimals: 2
      name: "Shed-Sensor Relative Humidity"
      id: bme280_humidity
      accuracy_decimals: 2
    address: 0x76
    update_interval: 5s
  - platform: template
    name: "Shed-Sensor Absolute Humidity"
    lambda: |-
      const float mw = 18.01534;    // molar mass of water g/mol
      const float r = 8.31447215;   // Universal gas constant J/mol/K
      return (6.112 * powf(2.718281828, (17.67 * id(bme280_temperature).state) /
        (id(bme280_temperature).state + 243.5)) * id(bme280_humidity).state * mw) /
        ((273.15 + id(bme280_temperature).state) * r); // in grams/m^3
    accuracy_decimals: 2
    update_interval: 5s
    icon: 'mdi:water'
    unit_of_measurement: 'g/m3'
  - platform: template
    name: "Shed-Sensor Dew Point"
    lambda: |-
      return (243.5*(log(id(bme280_humidity).state/100)+((17.67*id(bme280_temperature).state)/
    unit_of_measurement: °C
    update_interval: 5s
    icon: 'mdi:thermometer-alert'


Wow, thanks Ronnie! You rock :slight_smile:

1 Like

@Ronnie and you could complete that list with the lambda versions for

heat index (requiring T / H as inputs

return ((id(t).state)+5/9*(6.112*pow(10,7.5*(id(t).state)/(237.7+(id(t).state)))*(id(h).state)/100-10));

the humi index (also requiring T / H as inputs

return (-8.784695 + 1.61139411*(id(t).state) + 2.338549*(id(h).state) - 0.14611605*(id(t).state)*(id(h).state) - 0.012308094*(id(t).state)*(id(t).state) - 0.016424828*(id(h).state)*(id(h).state) + 0.002211732*(id(t).state)*(id(t).state)*(id(h).state) + 0.00072546*(id(t).state)*(id(h).state)*(id(h).state) - 0.000003582*(id(t).state)*(id(t).state)*(id(h).state)*(id(h).state));

and last not least the summersimmer index (also T / H required)

return ((1.98 * ((id(t).state * 1.8 + 32) - (0.55 - 0.0055 * id(h).state) * ((id(t).state * 1.8 + 32) - 58)) - 56.83) - 32) * 5/9;

what’s missing though is that some of these are limited means they don’t should b calculated across the most common temperature ranges since they only do make sense within a limited bandwidth.

have I missed one? Of course the “feels-like temperature”, but this does require wind-speed which isn’t given by the bme sensors.

1 Like

That’s very interesting @justone. Where I am we don’t use such descriptive terms for the weather - we just have a combination of wet, dry, cold and mild, with the wet being the predominant observation. :rofl:

I’ll have a play around with them and see if I can integrate them into both HA and ESPHome.

The constant b (molar mass of water) is probably affected by the temperature & pressure, but it’s commonly stated as 18.0153 g/mol. I think it’s fine for what we need.

Well this all ends up in a value where you surely finally visualise it using wording such as to cold, slightly cold, ideal, slightly warm reachin for unconcious, danger, extreme danger … if I translate the terms I’m aware of into english. (just one of a dozend links)

In the end it was more sort of fun to see if it’s just me feeling a bit deplaced in summer upstairs if trying to be care for a greener planet not switching on my air conditioner, or if the combination of temperature and humidity created a mix humans aren’t designed for. :slight_smile:
I simply tried to find out if all these indexes match my personal indisposition last summer when making up my mind about turning the air conditioner off for a greener planet to keep it running for a tolerable living.

1 Like