Power sensor API

The power sensor API allows you to give commands to your power sensor components for getting measurements of voltage, current, and power consumption.

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.
ReconfigureReconfigure this resource.
DoCommandExecute model-specific commands that are not otherwise defined by the component API.
GetResourceNameGet the ResourceName for this power sensor with the given name.
CloseSafely shut down the resource and prevent further use.

Establish a connection

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.

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.

The following 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

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.

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 = await my_power_sensor.do_command(command)

For more information, see the Python SDK Docs.

Parameters:

Returns:

Example:

myPowerSensor, err := power_sensor.FromRobot(machine, "my_power_sensor")

command := map[string]interface{}{"cmd": "test", "data1": 500}
result, err := myPowerSensor.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.

GetResourceName

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

Parameters:

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

Returns:

Example:

my_power_sensor_name = PowerSensor.get_resource_name("my_power_sensor")

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 my_power_sensor.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:

myPowerSensor, err := powersensor.FromRobot(machine, "my_power_sensor")

err = myPowerSensor.Close(context.Background())

For more information, see the Go SDK Docs.

Have questions, or want to meet other people working on robots? Join our Community Discord.

If you notice any issues with the documentation, feel free to file an issue or edit this file.