Power Sensor Component

A power sensor is a device that reports measurements of the voltage, current, and power consumption in your machine’s system. Integrate this component to monitor your power levels.

Supported models

To use your power sensor component, check whether one of the following models supports it.

For configuration information, click on the model name:

Model
Description

Control your power sensor with Viam’s client SDK libraries

To get started using Viam’s SDKs to connect to and control your machine, go to your machine’s page on the Viam app, navigate to the CONNECT tab’s Code sample page, select your preferred programming language, and copy the sample code generated.

When executed, this sample code will create a connection to your machine as a client. Once connected, you can control your machine programmatically by adding API method calls as shown in the following examples.

These examples assume you have a power sensor called "my_power_sensor" configured as a component of your machine. If your power sensor has a different name, change the name in the code.

Import the power sensor package for the SDK you are using:

from viam.components.power_sensor import PowerSensor
import (
  "go.viam.com/rdk/components/powersensor"
)

API

The power sensor component supports the following methods:

Method NameDescription
GetVoltageReturn the voltage reading of a specified device and whether it is AC or DC.
GetCurrentReturn the current of a specified device and whether it is AC or DC.
GetPowerReturn the power reading in watts.
GetReadingsGet the measurements or readings that this power sensor provides.
GetGeometriesGet all the geometries associated with the power sensor in its current configuration, in the frame of the power sensor.
ReconfigureReconfigure this resource.
DoCommandExecute model-specific commands that are not otherwise defined by the component API.
FromRobotGet the resource from the provided robot with the given name.
GetResourceNameGet the ResourceName for this power sensor with the given name.
CloseSafely shut down the resource and prevent further use.

GetVoltage

Return the voltage reading of a specified device and whether it is AC or DC.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Tuple[float, bool]): A float representing the voltage reading in V. A bool indicating whether the voltage is AC (true) or DC (false).

Example:

my_power_sensor = PowerSensor.from_robot(robot=robot, name='my_power_sensor')

# Get the voltage reading from the power sensor
voltage, is_ac = await my_power_sensor.get_voltage()
print("The voltage is", voltage, "V, Is AC:", is_ac)

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (float64): The measurement of the voltage, represented as a 64-bit float number.
  • (bool): Indicate whether voltage is AC (true) or DC (false).
  • (error): An error, if one occurred.

Example:

// Get the voltage from device in volts.
voltage, isAC, err := myPowerSensor.Voltage(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var voltageObject = await myPowerSensor.voltage();
double voltageInVolts = voltageObject.volts;
bool isItAC = voltageObject.isAc;

For more information, see the Flutter SDK Docs.

GetCurrent

Return the current of a specified device and whether it is AC or DC.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Tuple[float, bool]): A tuple which includes a float representing the current reading in amps, and a bool indicating whether the current is AC (true) or DC (false).

Example:

my_power_sensor = PowerSensor.from_robot(robot=robot, name='my_power_sensor')

# Get the current reading from the power sensor
current, is_ac = await my_power_sensor.get_current()
print("The current is ", current, " A, Is AC: ", is_ac)

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (float64): The measurement of the current, represented as a 64-bit float number.
  • (bool): Indicate whether current is AC (true) or DC (false).
  • (error): An error, if one occurred.

Example:

// Get the current reading from device in amps.
current, isAC, err := myPowerSensor.Current(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var currentObject = await myPowerSensor.current();
double amps = currentObject.amperes;
bool isItAC = currentObject.isAc;

For more information, see the Flutter SDK Docs.

GetPower

Return the power reading in watts.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (float): The power reading in watts.

Example:

my_power_sensor = PowerSensor.from_robot(robot=robot, name='my_power_sensor')

# Get the power reading from the power sensor
power = await my_power_sensor.get_power()
print("The power is", power, "Watts")

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (float64): The measurement of the power, represented as a 64-bit float number.
  • (error): An error, if one occurred.

Example:

// Get the power measurement from device in watts.
power, err := myPowerSensor.Power(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var power = await myPowerSensor.power();

For more information, see the Flutter SDK Docs.

GetReadings

Get the measurements or readings that this power sensor provides. If a sensor is not configured to have a measurement or fails to read a piece of data, it will not appear in the readings dictionary.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Mapping[str, viam.utils.SensorReading]): The readings for the PowerSensor. Can be of any type. Includes voltage in volts (float), current inamperes (float), is_ac (bool), and power in watts (float). .

Example:

my_power_sensor = PowerSensor.from_robot(robot=robot, name='my_power_sensor')

# Get the readings provided by the sensor.
readings = await my_power_sensor.get_readings()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • extra (map[string]interface{}): Extra options to pass to the underlying RPC call.

Returns:

  • (map[string]interface{}): A map containing the measurements from the sensor. Contents depend on sensor model and can be of any type.
  • (error): An error, if one occurred.

Example:

// Get the readings provided by the sensor.
readings, err := mySensor.Readings(context.Background(), nil)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

var readings = await myPowerSensor.readings();

For more information, see the Flutter SDK Docs.

GetGeometries

Get all the geometries associated with the power sensor in its current configuration, in the frame of the power sensor. The motion and navigation services use the relative position of inherent geometries to configured geometries representing obstacles for collision detection and obstacle avoidance while motion planning.

Parameters:

  • extra (Mapping[str, Any]) (optional): Extra options to pass to the underlying RPC call.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

Example:

geometries = await component.get_geometries()

if geometries:
    # Get the center of the first geometry
    print(f"Pose of the first geometry's centerpoint: {geometries[0].center}")

For more information, see the Python SDK Docs.

Reconfigure

Reconfigure this resource. Reconfigure must reconfigure the resource atomically and in place.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.
  • deps (Dependencies): The resource dependencies.
  • conf (Config): The resource configuration.

Returns:

  • (error): An error, if one occurred.

For more information, see the Go SDK Docs.

DoCommand

Execute model-specific commands that are not otherwise defined by the component API. If you are implementing your own power sensor and add features that have no built-in API method, you can access them with DoCommand.

Parameters:

  • command (Mapping[str, ValueTypes]) (required): The command to execute.
  • timeout (float) (optional): An option to set how long to wait (in seconds) before calling a time-out and closing the underlying RPC call.

Returns:

  • (Mapping[str, viam.utils.ValueTypes]): Result of the executed command.

Raises:

  • (NotImplementedError): Raised if the Resource does not support arbitrary commands.

Example:

command = {"cmd": "test", "data1": 500}
result = component.do(command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

// This example shows using DoCommand with an arm component.
myArm, err := arm.FromRobot(machine, "my_arm")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myArm.DoCommand(context.Background(), command)

For more information, see the Go SDK Docs.

Parameters:

Returns:

Example:

// Example using doCommand with an arm component
const command = {'cmd': 'test', 'data1': 500};
var result = myArm.doCommand(command);

For more information, see the Flutter SDK Docs.

FromRobot

Get the resource from the provided robot with the given name.

Parameters:

Returns:

For more information, see the Flutter SDK Docs.

GetResourceName

Get the ResourceName for this power sensor with the given name.

Parameters:

  • name (str) (required): The name of the Resource.

Returns:

Example:

# Can be used with any resource, using an arm as an example
my_arm_name = my_arm.get_resource_name("my_arm")

For more information, see the Python SDK Docs.

Parameters:

Returns:

For more information, see the Flutter SDK Docs.

Close

Safely shut down the resource and prevent further use.

Parameters:

  • None.

Returns:

  • None.

Example:

await component.close()

For more information, see the Python SDK Docs.

Parameters:

  • ctx (Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.

Returns:

  • (error): An error, if one occurred.

Example:

// This example shows using Close with an arm component.
myArm, err := arm.FromRobot(machine, "my_arm")

err = myArm.Close(ctx)

For more information, see the Go SDK Docs.

Troubleshooting

You can find additional assistance in the Troubleshooting section.

You can also ask questions on the Viam Community Slack and we will be happy to help.