Monitoring Battery Health in sensors

Strange that this topic reappeared to the top…

Better late than never. I just have battery level (in percentage) for my devices, and I put them all in the gBatteries group. Then every day send an email to myself with a list of devices that are offline and those with a low battery

Then using a jruby script

DESTINATION_EMAIL = "my_email@address.com"
LOW_BATTERY_THRESHOLD = 20

def email(msg, **kwargs)
  things["mail:smtp:local"].send_mail(DESTINATION_EMAIL, kwargs[:subject] || "OpenHAB Alert", msg)
end

rule "Battery: Monitor Levels" do
  every :day, at: "10:55"
  run do 
    online, offline = gBatteries.members.partition(&:state?)

    offline_items = offline.map { |item| "#{item}: OFFLINE" }.sort

    low_batteries = online.select { |item| item.state < LOW_BATTERY_THRESHOLD }
                          .map { |item| "#{item}: #{item.state}%" }
                          .sort

    list = offline_items + low_batteries
    next if list.empty?

    email("Devices with low battery:\n\n#{list.join("\n")}", subject: "Low Battery Alert!")
  end
end

1 Like