Humidex Calculations

Hey all,
Been converting my complex smart home from openhab to homeassistant. One of the missing pieces for me was a humidex rating. My fans set to OFF, LOW, MEDIUM and HIGH and I map this to humidex rating for each room. This was one of the best features in my last setup. So to get the same thing going in homeassisstant I created this module. Any feedback, suggestions or improvements please let me know.

Configuration:

sensor humidex:
        - platform: humidex
          name: "Study humidex"
          temperature: sensor.study_temperature
          humidity: sensor.study_relative_humidity

Custom component:

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (CONF_NAME, ATTR_ICON)
from homeassistant.helpers.entity import Entity

import homeassistant.helpers.config_validation as cv

import voluptuous as vol
import math

DOMAIN = 'humidex'

DEFAULT_NAME = 'Humidex'

CONF_TEMPERATURE = 'temperature'
CONF_HUMIDITY = 'humidity'


PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
    vol.Required(CONF_TEMPERATURE): cv.entity_id,
    vol.Required(CONF_HUMIDITY): cv.entity_id ,
    vol.Optional(ATTR_ICON, default='mdi:wifi-strength-outline'): cv.string
})

def setup_platform(hass, config, add_devices, discovery_info=None):
    name = config.get(CONF_NAME)
    temp = config.get(CONF_TEMPERATURE)
    humid = config.get(CONF_HUMIDITY)
    icon = config.get(ATTR_ICON)

    add_devices([HumidexSensor(hass, name, temp, humid, icon)])




class HumidexSensor(Entity):
    """Representation of a Sensor."""

    def __init__(self, hass, name, temp, humid, icon):
        """Initialize the sensor."""
        self._hass = hass
        self._name = name
        self._temp = temp
        self._humid = humid
        self._icon = icon
        self._state = None
        self._humidex = None
        self._attributes = None

    @property
    def name(self):
        """Return the name of the sensor."""
        return self._name

    @property
    def state(self):
        """Return the state of the sensor."""
        return self._state

    @property
    def icon(self):
        """Return the icon to use in the frontend, if any."""
        return self._icon

    @property
    def device_state_attributes(self):
        """Return the state attributes of the monitored installation."""
        if self._attributes is not None:
            return self._attributes

    def update(self):
        """Fetch new state data for the sensor.

        This is the only method that should fetch new data for Home Assistant.
        """
        temperature = self._hass.states.get(self._temp)
        humidity = self._hass.states.get(self._humid)
    
        if temperature and humidity:
            # Setup varaibles
            temp = float(temperature.state)
            humid = float(humidity.state)

            # Calculate dewpoint
            A = 17.27
            B = 237.7
            alpha = ((A * temp) / (B + temp)) + math.log(humid/100.0)
            d = (B * alpha) / (A - alpha)

            # Calculate humidex
            kelvin = 273.15
            temperature = temp + kelvin
            dewpoint = d+kelvin
            # Calculate vapor pressure in mbar.
            e = 6.11 * math.exp(5417.7530 * ((1 / kelvin) - (1 / dewpoint)))
            # Calculate saturation vapor pressure
            es = 6.11 * math.exp(5417.7530 * ((1 / kelvin) - (1 / temperature)))
            humidity = e / es
            h = 0.5555 * (e - 10.0)
            humidex = temperature + h - kelvin
            self._attributes = {}
            self._attributes.update({"humidex": humidex})

            if humidex < temp:
                humidex = temp
            
            if humidex < 20:
                self._state = 'No significant'
                self._icon = 'mdi:wifi-strength-outline'
            elif humidex < 30:
                self._state = 'Comfortable'
                self._icon = 'mdi:wifi-strength-1'
            elif humidex < 40:
                self._state = 'Some discomfort'
                self._icon = 'mdi:wifi-strength-2'
            elif humidex < 46:
                self._state = 'Avoid exertion'
                self._icon = 'mdi:wifi-strength-3'
            elif humidex < 54:
                self._state = 'Dangerous'
                self._icon = 'mdi:wifi-strength-4'
            else:
                self._state = 'Heat stroke imminent'
                self._icon = 'mdi:wifi-strength-4-alert'
1 Like

Welcome to the community and thanks for your contribution!

I have a question about the calculations performed by your humidex component. I see this:

            temp = float(temperature.state)

            kelvin = 273.15
            temperature = temp + kelvin

and get the impression it assumes temperature.state is always provided in Celsius units. There are many Home Assistant users who use Fahrenheit units. Perhaps the humidex component needs to examine the unit_system option (metric or imperial) and adjust its calculations accordingly?

Sorry I should have stated in my original post that is isnt 100% tested. It is working in my home but it doesn’t handle a few things such as non-Celsius readings. I was concerned that it wouldn’t updated when the temp or humidity changes but appears to be working. After some more testing and patching a few things like check measurement unit I will start to look at doing a PR to the main branch if there is any interest.

1 Like

Yes very interested! As the humidity swings drive me nuts.
Have you published the code?
Could we use it?
Thanks!

Havent published, but shoot me your email address in a private message and I can send you a copy.

Hi @HardlyThere , have you published your code?

I will try to get around to updating the code to work with HACs and get it published there soon.

I just published the code to https://github.com/geckom/ha-humidex which should you some easy to install instructions for HACS. It is pretty untested outside of my environment, so if you have any issues please submit them to github and I will try to fix them as quickly as possible.

1 Like