Automatic bathroom exhaust fan rule in JRuby

Tags: #<Tag:0x00007f174ea7c738>

With the latest updates on JRuby Library for OpenHAB I’ve simplified my bathroom exhaust fan to this:

rule 'Humidity: Control ExhaustFan' do
  updated MasterBathRoom_Humidity, MainBathRoom_Humidity
  triggered do |humidity|
    exhaust_fan = items[humidity.name.sub '_Humidity', '_ExhaustFan']
    evo_rate = humidity.evolution_rate 4.minutes, :influxdb
    logger.info("#{humidity.name} #{humidity} evolution_rate: #{evo_rate}")
    if humidity > 70 && evo_rate > 15
      exhaust_fan.on unless exhaust_fan.on?
    elsif humidity < 70 || evo_rate < -5
      exhaust_fan.off unless exhaust_fan.off?
    end
  end
end

Notes:

  • It is based on the rate of change in humidity levels. When it increased more than 15% (or any setting of your choice) in the past 4 minutes, it would turn on the exhaust fan.
  • Humidity levels are stored in influxdb persistence service on every change. This makes for easy retrieval using evolution_rate or any other persistence functions.
  • Persistence calls are attached to each item, e.g. humidity.last_update, MainBathRoom_Humidity.average_since, etc.
  • Simplified duration - instead of calling the cumbersome ZonedDateTime.now.minusMinutes(4), simply specify 4.minutes
  • Number items can be directly compared against numeric e.g. MainBathRoom_Humidity > 70 instead of MainBathRoom_Humidity.state.intValue etc.
1 Like