Script for input voltage awareness - ROS 7.19.4 - Tested with RB2011UiAS-RM

# This script is developed by LJB due to a lack of decimal number support when calling the volts value from system health. It is only tested and verified with ROS 7.19.4 on an RB2011UiAS-RM

# Battery volts offset measured in dV (deciVolts) - do not add a period or comma
:local offset "22"

#define variable names
:local rawVoltage [/system/health get [find where name=voltage] value];
:local searchChar "."
:local replaceChar ""
:local Voltage ""
:local length [:len $rawVoltage]
:global "Battery-dV" ""

# strip the period character from the volts string provided by /system/health
:if (:len $rawVoltage > 3) do={
:for i from=0 to=([:len $rawVoltage] - 1) do={
  :local currentChar [:pick $rawVoltage $i]
  :if ($currentChar = $searchChar) do={
:set Voltage ($Voltage . $replaceChar)
} else={
:set Voltage ($Voltage . $currentChar);
}}}

:if ($length < 3) do={ :set Voltage ($Voltage * 10) }

:set "Battery-dV" (($Voltage + $offset)." dV")

23.45

dot pos [find dot]

part 1 = pick from start to dot pos = 23
part 2 = pick after dot pos to end = 45

final= part1 + part2 = 2345

done.

My initial try I failed several times to pick part two as referenced in your reply hence the different approach above.

Ok, I have to ask.

What (the heck) is the initially set voltage offset?

A difference (measured with a multimeter or oscilloscope or whatever) between the voltage reading of the board and the actual voltage?

Why decivolt?

The board measures in volts - dot - hundreths of volts, doesn't it?

The "length 3" is because the reading can be something like 24.2 (and not 24.20)?

In a scripting language I would rather (personal habits, not necessarily good or better):

  1. remove the dot
  2. pad with one or two 00's at the end
  3. get the 4 leftmost characters (assuming that the full volts are two characters, i.e. more than 10 and less than 100)

from this point on everything is hundredth of volts and can be managed, summed, subtracted, etc..

To display with the dot (or comma in some languages) you can get the two leftmost characters + separator + the two rightmost characters.

Function: deciVolt + offset to Volt string

:global dV2str do={
    :if ([:typeof $1] = "nothing") do={ :return 0 }
    :local dot   "."
    :local offst [:tonum $2]
    :local input ([:tonum $1] + $offst)
    :local sign  ""
    :if ($input < 0) do={:set sign "-" ; :set input ($input * -1)}
    :local int   ($input / 10)
    :local dec   ($input % 10)
    :return "$sign$int$dot$dec"
}
[] > :put [$dV2str "240" 22]
26.2
[] > :put [$dV2str "221" -10]
21.1
[] > :put [$dV2str "-135" 22]
-11.3

Hi @jaclaz

The offset specified is to compensate for the volt difference I have between the router and the battery source due to a long-distance cable. Perhaps one can start with a zero-rated offset. I am updating the original post.

deciVolt is a tenth of a single Volt, not a hundredth. Perhaps, in the spur of the moment it was easier to convert to deciVolt rather than Volts to avoid working with decimals.

I did not feel a need for going smaller than a 10th of a volt as this is good enough for an email and/or internal buzzer alert script to create awareness.

I appreciate your interest.

@rextended

Great, with magic you make it appear so simply and easily. I am just a beginner. Perhaps one day I'll be at your level.

The :tonum function sounds interesting. I will have to note that down for next time.

Everyone started walking, some started running, but the difference is made by those who stop...

(Don't get me wrong, I'll explain what it means later, but it doesn't mean you have to stop.)