Previous
Movement Sensor
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 Name | Description |
---|---|
GetVoltage | Return the voltage reading of a specified device and whether it is AC or DC. |
GetCurrent | Return the current of a specified device and whether it is AC or DC. |
GetPower | Return the power reading in watts. |
GetReadings | Get the measurements or readings that this power sensor provides. |
Reconfigure | Reconfigure this resource. |
DoCommand | Execute model-specific commands that are not otherwise defined by the component API. |
GetResourceName | Get the ResourceName for this power sensor with the given name. |
Close | Safely shut down the resource and prevent further use. |
To get started using Viam’s SDKs to connect to and control your power sensor and the rest of 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.
To show your machine’s API key in the sample code, toggle Include API key.
We strongly recommend that you add your API key and machine address as an environment variable. Anyone with these secrets can access your machine, and the computer running your machine.
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:
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:
Example:
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:
true
) or DC (false
).Example:
For more information, see the Go SDK Docs.
Parameters:
extra
(None) (optional)callOptions
(CallOptions) (optional)Returns:
Example:
For more information, see the TypeScript SDK Docs.
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:
Example:
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:
true
) or DC (false
).Example:
For more information, see the Go SDK Docs.
Parameters:
extra
(None) (optional)callOptions
(CallOptions) (optional)Returns:
Example:
For more information, see the TypeScript SDK Docs.
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:
Example:
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:
Example:
For more information, see the Go SDK Docs.
Parameters:
extra
(None) (optional)callOptions
(CallOptions) (optional)Returns:
Example:
For more information, see the TypeScript SDK Docs.
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:
Example:
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:
Example:
For more information, see the Go SDK Docs.
Parameters:
extra
(None) (optional)callOptions
(CallOptions) (optional)Returns:
Example:
For more information, see the TypeScript SDK Docs.
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:
For more information, see the Go SDK Docs.
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:
Raises:
Example:
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.cmd
(map[string]interface{}): The command to execute.Returns:
Example:
For more information, see the Go SDK Docs.
Parameters:
command
(Struct) (required): The command to execute.callOptions
(CallOptions) (optional)Returns:
Example:
For more information, see the TypeScript SDK Docs.
Get the ResourceName
for this power sensor with the given name.
Parameters:
name
(str) (required): The name of the Resource.Returns:
Example:
For more information, see the Python SDK Docs.
Safely shut down the resource and prevent further use.
Parameters:
ctx
(Context): A Context carries a deadline, a cancellation signal, and other values across API boundaries.Returns:
Example:
For more information, see the Go SDK Docs.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!