Battery life calculation

I have a device with a non-rechargeable battery reporting the amount of milliamp-hours consumed since its last transmission. I understand how to add in a cumulative manner but how do you subtract? Am I able to set the beginning known value of the battery (say 13000mAH), subtract the amount used (0.4mAH), store value (12,999.6), then subtract the next reading (0.9) from the stored reading to display the remaining battery life? I’m new to the world of IoT and would very much appreciate some examples.

Hi @Warren_Moik, and welcome to the Losant Forums!

You could calculate the remaining battery life as a separate number attribute on your device using an application workflow. For example, assuming your existing attribute is called “consumedBattery” and given a new attribute called “remainingBattery” …

  1. Set up a Device: State Trigger that fires whenever your devices report data for “consumedBattery”.
  2. Use a Device: Get Node to fetch the reporting device and its last reported value for “remainingBattery”.
  3. Using a Device: State Node, record state for the device’s “remainingBattery” attribute with the value from a simple Handlebars helper: {{subtract remainingBattery consumedBattery}}.

You can then visualize this value in our dashboards using a number of different blocks - such as a Gauge Block to prominently display the remaining mAH, or a Time Series Graph to visualize its usage over time.

This approach will require you manually setting an initial value of “remainingBattery” (13000 in your case, assuming your unit is mAH), and it also does not account for when you put new batteries in the device - at which point you would again have to manually set that “remainingBattery” value.

Let us know if you have any questions, and again, welcome to Losant!