How to Write Your First Script
Create your first Profinity script to automate tasks and interact with your CAN bus system.
Prerequisites
- Profinity V2 installed and running
- An active profile with components configured
- Basic understanding of JavaScript, Python, or C#
- Scripting enabled in Profinity configuration
Steps
Step 1: Enable Scripting
- Navigate to ADMIN → System Configuration
- Find Scripting section
- Enable Enable Scripting
- Click Save
Step 2: Choose Your Script Type
For your first script, use Run Script: - Run Scripts - Execute on-demand or on a schedule - Receive Scripts - Triggered when CAN messages are received - Service Scripts - Long-running background services
Step 3: Open the Script Editor
- Navigate to ADMIN → Scripts
- Click New Script or Add Script
- Select Run Script
- Script editor opens
Step 4: Write Your First Script
JavaScript Example:
async function main() {
const components = await profinity.getComponents();
for (const component of components) {
const data = await profinity.getComponentData(component.id);
console.log(`Component: ${component.name}`);
}
}
main().catch(console.error);
Python Example:
import profinity
def main():
components = profinity.get_components()
for component in components:
data = profinity.get_component_data(component.id)
print(f"Component: {component.name}")
if __name__ == "__main__":
main()
Step 5: Configure Script Settings
- Set Script Name
- Select Language (JavaScript, Python, or C#)
- Add Description
- Configure Trigger:
- On Demand - Run manually
- Scheduled - Run on schedule (cron)
- On Startup - Run when Profinity starts
Step 6: Test Your Script
- Click Run or Test
- Check console output for results
- Review error messages
- Fix issues and test again
Step 7: Save and Deploy
- Click Save to save your script
- Script appears in your scripts list
- Run on demand or according to schedule
Next Steps: More Advanced Scripts
Reading Component Data
// Read specific component data
const bmuData = await profinity.getComponentData('Prohelion BMU');
const voltage = bmuData.PackMeasurement.PackVoltage;
console.log(`Battery Voltage: ${voltage}V`);
Sending CAN Messages
// Send a CAN message
await profinity.canBus.sendMessage({
id: 0x123,
data: [0x01, 0x02, 0x03, 0x04],
extended: false
});
Accessing System State
// Access system properties
const activeProfile = await profinity.profiles.getActive();
const systemInfo = await profinity.system.getInfo();
Tips
- Start Simple: Begin with basic scripts that read data
- Use Console Logging: Use
console.log()to debug your scripts - Test Incrementally: Test small parts of your script as you build it
- Review Examples: Look at example scripts in the documentation
Troubleshooting
- Script Not Running: Check that scripting is enabled
- No Data Available: Ensure components are active and receiving data
- Syntax Errors: Validate your code syntax before running
- Permission Issues: Verify your script has necessary permissions
Related Documentation
- Scripting Overview - Complete scripting guide
- Supported Languages - Language-specific documentation
- Script Types - Different script types explained
- Script Operations - Available operations and APIs