Skip to content

Instantly share code, notes, and snippets.

@randomcamel
Created August 31, 2022 23:34
Show Gist options
  • Save randomcamel/e30a7fcd0f8750e6bdd2ff92d236ac88 to your computer and use it in GitHub Desktop.
Save randomcamel/e30a7fcd0f8750e6bdd2ff92d236ac88 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'logger'
log = Logger.new(STDOUT)
TEST_MODE = ARGV[0] == "--test"
fields = %w{ current_humidity current_temp_f aqi pressure }
%w{inside outside}.each do |sensor|
url = "#{sensor}-air/json"
json = `curl --silent #{url}`
data = JSON.parse(json)
# outside device has two sensors.
if data["pm2.5_aqi_b"].nil?
data["pm2.5_aqi_b"] = data["pm2.5_aqi"]
end
data["aqi"] = (data["pm2.5_aqi"] + data["pm2.5_aqi_b"]) / 2
# convert millibars to inches.
data["pressure"] = (data["pressure"] / 33.864).round(2)
fields.each do |field|
msg = %Q{snugglehaus.air.#{sensor}.#{field}:#{data[field]}|g}
log.info "metric msg: #{msg}"
if !TEST_MODE
`echo "#{msg}" | nc -w 1 -u 127.0.0.1 8125`
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment