Instantly share code, notes, and snippets.

Embed
What would you like to do?
Automatically operate the bath light and fan
require "influxdb"
def current_value(field,location,device="none")
host = 'hostname.local'
database = 'home-sensor'
influxdb = InfluxDB::Client.new database,:host => host
# query
result = influxdb.query "select value from #{field} where location='#{location}' order by desc limit 1 tz('Japan')"
begin
time, value = result[0]["values"][0]["time"], result[0]["values"][0]["value"]
rescue Exception => e
puts "[ERROR] Not found data in influxdb"
puts "result:"
puts result
exit!
end
return time, value
end
# Bath conditions
def continuous_using?
# post query
time, door_open_percent = current_value("ocaction", "5")
# last door open or close time
y, m, day, hur, min, sec = time.split(/-|T| |:|\./)
last_oc_time = Time.new(y, m, day, hur, min, sec)
if (Time.now - last_oc_time) > ($suspend_min * 60)
# more than 5 minutes passed since the last action
# door open or close
return door_open_percent == 0 ? true : false
else
# less than 5 minutes from last action
return false
end
end
def not_continuous_using?
# post query
time, door_open_percent = current_value("ocaction", "5")
# last door open or close time
y, m, day, hur, min, sec = time.split(/-|T| |:|\./)
last_oc_time = Time.new(y, m, day, hur, min, sec)
if (Time.now - last_oc_time) > ($suspend_min * 60)
# more than 5 minutes passed since the last action
# door open or close
return door_open_percent == 0 ? false : true
else
# less than 5 minutes from last action
return false
end
end
def using?
door_open_percent = `#{CMD} Get Door_5`.to_i
return door_open_percent == 0 ? true : false
end
def dark?
lux = `#{CMD} Get LightSensor_5`.to_i
return lux < 200 ? true : false
end
def bright?
lux = `#{CMD} Get LightSensor_5`.to_i
return lux > 700 ? true : false
end
def hot?
temperature = `#{CMD} Get Temperature_5`.to_i
return temperature >= 30 ? true : false
end
def cold?
temperature = `#{CMD} Get Temperature_5`.to_i
return temperature <= 15 ? true : false
end
def damp?
humidity = `#{CMD} Get Humidity_5`.to_i
return humidity >= 70 ? true : false
end
def dry?
humidity = `#{CMD} Get Humidity_5`.to_i
return humidity <= 47 ? true : false
end
def fan_enable?
fan = `#{CMD} Get Fan_5`.to_i
return fan == 1 ? true : false
end
def light_enable?
light = `#{CMD} Get Light_5`.to_i
return light == 1 ? true : false
end
def conditions_test
print "using?\t"
puts using?
print "continuous using?\t"
puts continuous_using?
print "not continuous using?\t"
puts not_continuous_using?
print "dark?\t"
puts dark?
print "bright?\t"
puts bright?
print "hot?\t"
puts hot?
print "cold?\t"
puts cold?
print "damp?\t"
puts damp?
print "dry?\t"
puts dry?
print "fan_enable?\t"
puts fan_enable?
print "light_enable?\t"
puts light_enable?
end
# Bath device
def light (a=true, rule_id)
if a && !light_enable?
`#{CMD} Set Light_5 On true`
log(rule_id, "LIGHT_ON")
elsif !a && light_enable?
`#{CMD} Set Light_5 On false`
log(rule_id, "LIGHT_OFF")
end
end
def fan (a=true, rule_id)
if a && !fan_enable?
`#{CMD} Set Fan_5 On true`
log(rule_id, "FAN_ON")
elsif !a && fan_enable?
`#{CMD} Set Fan_5 On false`
log(rule_id, "FAN_OFF")
end
end
# Log
def log (rule_id, operation)
m = Time.now.to_s + ", " + rule_id.to_s + ", " + operation
puts m
open("#{WD}/auto_bath.log", "a"){|f|f.puts m}
inflxu_send(rule_id, operation)
end
def inflxu_send (rule_id, operation)
`curl -i -XPOST '#{INFLUX_ADD}/write?db=home-sensor' --data-binary 'automation,operation=#{operation},location=5 value=#{rule_id}'`
end
WD = "~/.homebridge"
CMD = "ruby #{WD}/cmd4.rb"
VERSION = "20181017"
INFLUX_ADD = "http://hostname.local:8086"
$suspend_min = 5
conditions_test if ARGV[0] == "c"
# Automation rule
light(true, 1) if using? && dark?
light(false, 2) if not_continuous_using? && light_enable?
fan(true, 3) if using? && hot?
fan(false, 4) if using? && !hot?
fan(true, 5) if !using? && damp?
fan(false, 6) if !using? && dry?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment