-- Local functions local kelvin_to_fahrenheit local celsius_to_fahrenheit -- Start the scripts running -- Allows us to have multiple scripts running at once. function start_my_scripts() thread.run(monitor_temperature_and_humidity, "Monitor Environment", "MonitorEnvironment") end -- Log temperature and humidity every 2 hours function monitor_temperature_and_humidity() thread.limit(1) while true do local now=os.date("*t") if now.hour%2 == 0 and now.min == 0 then log_temperature() log_humidity() end delay(60) end end -- Log the temperature for the past day function log_temperature(sensor) sensor = sensor or "sensors.0.temperature" local now=os.time() log.notice(sensor) local temperature=meter.values[sensor] or nil if temperature.value then log.notice("Current temperature is %1.1fºF, current time is %s",kelvin_to_fahrenheit(temperature.value),os.date("%H:%M:%S",now)) else log.notice("No temperature meter found!") end end -- Log Humidity function log_humidity(sensor) sensor = sensor or "sensors.0.relative_humidity" local now=os.time() log.notice(sensor) local humidity=meter.values[sensor] or nil if humidity.value then log.notice("Current relative humidity is %1.1f%%, current time is %s",humidity.value*100,os.date("%H:%M:%S",now)) else log.notice("No humidity meter found!") end end --[[ ----------------------------- Some local functions ----------------------------- --]] -- local function kelvin_to_fahrenheit(t) if not tonumber(t) then log.err("Invalid temperature") return 0 end return (t - 273.16) * 9/5 + 32 end -- local function celsius_to_fahrenheit(t) if not tonumber(t) then log.err("Invalid temperature") return 0 end return t * 9 / 5 + 32 end