A professional library for PI Web API integration
Project description
piwebapiconnect
A professional Python library for OSIsoft PI Web API integration. Built cleanly with httpx and asyncio to be purely asynchronous and incredibly robust.
Installation
pip install piwebapiconnect
Setup & Connecting
1. Using a .env file (Recommended)
Create a .env file in your root folder:
PI_WEB_API_URL=https://your-pi-server/piwebapi
PI_WEB_API_USERNAME=domain\user
PI_WEB_API_PASSWORD=your-password
PI_SERVER_NAME=PISERVER01
PI_WEB_API_VERIFY_SSL=False # Set to True in production
from piwebapiconnect import get_service
service = get_service()
2. Passing credentials directly
import asyncio
from piwebapiconnect import get_service
async def main():
service = get_service(
url="https://your-pi-server/piwebapi",
username="domain\\user",
password="your-password",
server_name="PISERVER01",
verify_ssl=False
)
print(await service.test_connection())
await service.close()
if __name__ == "__main__":
asyncio.run(main())
Detailed Example Usage
All methods in this library are asynchronous and should be used within an async context.
Initialization
import asyncio
from piwebapiconnect import get_service
async def main():
# Initialize service with demo credentials
service = get_service(
url="https://demo-pi-server/piwebapi",
username="domain\\user",
password="password123",
server_name="PISERVER01",
verify_ssl=False
)
# ... use service methods here ...
await service.close()
if __name__ == "__main__":
asyncio.run(main())
1. Fetch Current Data
Retrieve the latest value for a single tag.
# Both methods are identical
val = await service.get_current_value("SINUSOID")
val = await service.get_real_time_data("SINUSOID")
print(f"Value: {val['value']} at {val['timestamp']}")
2. Fetch Multiple Current Values
Retrieve values for multiple tags in a single efficient batch request.
tags = ["SINUSOID", "CDT158", "BA:LEVEL.1"]
results = await service.get_multiple_current_values(tags)
for tag, data in results.items():
print(f"{tag}: {data['value']}")
3. Historical Data (Recorded & Interpolated)
# Actual stored points
history = await service.get_recorded_values("SINUSOID", "*-24h", "*")
# Interpolated points at specific intervals
interpolated = await service.get_interpolated_values("SINUSOID", "*-24h", "*", interval="1h")
4. Summary Statistics
Get calculated values like Average, Min, or Max directly from the PI Server.
summary = await service.get_summary_data("SINUSOID", "*-24h", "*", summary_type="Average")
print(f"Average: {summary['value']}")
5. Live Data Stream
Yields continuous updates for one or more tags.
# Stream for multiple tags
async for batch in service.get_live_data_stream(["SINUSOID", "CDT158"], interval_seconds=5):
for update in batch:
print(f"Update - {update['tag']}: {update['value']}")
# Stream for a single tag (yields Dict instead of List)
async for update in service.get_live_data_stream("SINUSOID", interval_seconds=5):
print(f"Single Update: {update['value']}")
6. Data Manipulation (Write & Delete)
# Write a value
await service.write_value("TEST_TAG", 123.4)
# Delete a value at a specific timestamp
await service.delete_value("TEST_TAG", "2023-10-01T12:00:00Z")
7. Metadata & Hierarchy Browsing
# Search for tags
tags = await service.search_tags("*pressure*")
# Get tag configuration/attributes
attrs = await service.get_tag_attributes("SINUSOID")
# Browse Asset Servers and Databases
servers = await service.get_asset_servers()
databases = await service.get_organizations("AF_SERVER_DEMO")
Error Handling
The library provides a PIWebAPIError for handling API-specific issues gracefully.
from piwebapiconnect.service import PIWebAPIError
try:
data = await service.get_current_value("INVALID_TAG")
except PIWebAPIError as e:
print(f"Caught PI Error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file piwebapiconnect-0.1.8.tar.gz.
File metadata
- Download URL: piwebapiconnect-0.1.8.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d931782a19fa7b59ba7a5044367d40c89fa04940b72d17da84c1a8dd5fbc923e
|
|
| MD5 |
706f83920b0587e2b7b889e2cc4e5138
|
|
| BLAKE2b-256 |
80e697d8eb23535e0fc6680461f3a9a3f31dbfa528aa297a0aa941adf83f3d17
|
File details
Details for the file piwebapiconnect-0.1.8-py3-none-any.whl.
File metadata
- Download URL: piwebapiconnect-0.1.8-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b68e8f8c5a0df5fcaff7a150b726d62a8d33b368ea16dc40a8230916b7cb97a
|
|
| MD5 |
3a6fba567cfe324abe39b8b78f49060e
|
|
| BLAKE2b-256 |
dba09e63a40cdd37e53632d5a34ecb6d640d9a92e20cc6faf8ac7734d4af0551
|