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'