homebridge-sense-energy-monitor
Version:
Enhanced Homebridge plugin for Sense Home Energy Monitor with comprehensive API integration and real-time monitoring
484 lines (390 loc) ⢠17.3 kB
Markdown
# Homebridge Sense Energy Monitor
<!-- [](https://github.com/homebridge/homebridge/wiki/Verified-Plugins) -->
[](https://www.npmjs.com/package/homebridge-sense-energy-monitor)
[](https://www.npmjs.com/package/homebridge-sense-energy-monitor)
**Dynamic Platform** plugin for the Sense Home Energy Monitor that provides basic device status monitoring in HomeKit. **Version 2.4.0** - Enhanced MFA Support with TOTP Secret Keys.
## ā” Key Features
- **š Dynamic Platform**: Automatically discovers and manages device status accessories
- **š Device Status Tracking**: Shows when Sense devices are actively consuming power
- **š Threshold-Based Detection**: Configurable power threshold for device "on" detection
- **š± Eve App Support**: Historical power data with fakegato-history integration
- **š Nuclear Reset System**: Eliminates callback conflicts with smart accessory management
- **āļø Verification Ready**: Meets all Homebridge verification requirements
- **š¾ Smart Caching**: Authentication and data caching for improved performance
- **š MFA Support**: Multi-factor authentication for Sense accounts with 2FA enabled
## š **What's New in v2.4.0**
### š **Multi-Factor Authentication Support**
- **MFA/2FA Support** for Sense accounts with multi-factor authentication enabled
- **Configuration options** for `mfaEnabled` and `mfaSecret` settings
- **Enhanced error messaging** with clear guidance for MFA setup
- **Test utility** included for validating MFA authentication
### š **Documentation Cleanup**
- **Honest feature descriptions** that accurately reflect HomeKit limitations
- **Clear HomeKit warnings** about power data display limitations
- **Realistic comparison table** showing actual vs claimed functionality
- **Removed misleading claims** about comprehensive energy monitoring in HomeKit
### š§ **Technical Improvements**
- **Enhanced authentication flow** with better MFA error handling
- **Improved user guidance** for configuration troubleshooting
- **Updated configuration schema** with MFA field validation
## š¦ Installation
### ā ļø **Breaking Changes Notice**
**Upgrading from v2.0.x?** This is a **major breaking change** that requires configuration updates:
- **Plugin type changed**: From `accessory` to `platform`
- **Configuration format**: Update your `config.json` (see below)
- **Accessories will be recreated**: You may need to re-add them to HomeKit rooms/scenes
### Via Homebridge Config UI X (Recommended)
1. Search for **"homebridge-sense-energy-monitor"** in the Homebridge UI
2. Install the plugin (v2.3.0+)
3. Configure using the settings form
4. **Update your configuration** to platform format (see Configuration section)
5. Restart Homebridge
4. Restart Homebridge
### Manual Installation
```bash
npm install -g homebridge-sense-energy-monitor
```
## āļø Configuration
Add the platform to your Homebridge config:
```json
{
"platforms": [
{
"platform": "SenseEnergyMonitor",
"name": "Sense Energy Monitor",
"username": "your@email.com",
"password": "your_sense_password",
"mfaEnabled": false,
"mfaCode": "123456",
"pollingInterval": 60,
"useWebSocket": true,
"includeDevices": true,
"enableHistory": true,
"verbose": false
}
]
}
```
### Configuration Options
| Field | Required | Default | Description |
|-------|----------|---------|-------------|
| `platform` | ā
| - | Must be "SenseEnergyMonitor" |
| `name` | ā
| - | Platform name in HomeKit |
| `username` | ā
| - | Your Sense account email |
| `password` | ā
| - | Your Sense account password |
| `mfaEnabled` | ā | false | Enable if your Sense account has MFA/2FA enabled |
| `mfaSecret` | ā | - | TOTP secret key from your authenticator app setup (base32 encoded) |
| `monitor_id` | ā | Auto-detect | Specific monitor ID |
| `pollingInterval` | ā | 60 | Data refresh interval (30-3600 seconds) |
| `useWebSocket` | ā | true | Enable real-time WebSocket data |
| `includeSolar` | ā | true | Include solar power monitoring (if available) |
| `includeDevices` | ā | true | Track individual device power usage |
| `maxDevices` | ā | 20 | Maximum number of devices to track (1-50) |
| `devicePowerThreshold` | ā | 10 | Minimum watts to consider device "active" |
| `enableHistory` | ā | true | Enable Eve app historical data |
| `verbose` | ā | true | Enable detailed debug logging |
### Multi-Factor Authentication (MFA/2FA) Configuration
If your Sense account has multi-factor authentication enabled, you'll need to provide your TOTP secret key:
```json
{
"platforms": [
{
"platform": "SenseEnergyMonitor",
"name": "Sense Energy Monitor",
"username": "your@email.com",
"password": "your_sense_password",
"mfaEnabled": true,
"mfaSecret": "YOUR_BASE32_SECRET_KEY"
}
]
}
```
**Important MFA Notes:**
- Set `mfaEnabled` to `true` if your Sense account has 2FA enabled
- Enter your TOTP secret key in `mfaSecret` (the base32 encoded secret, not the 6-digit code)
- The secret key is shown when you first set up 2FA (looks like: JBSWY3DPEHPK3PXP)
- The plugin will automatically generate fresh TOTP codes as needed
- The plugin uses Sense's two-step MFA flow: initial auth ā MFA token ā TOTP validation
- The plugin will provide clear error messages if MFA authentication fails
### Child Bridge Configuration
For improved performance and isolation, you can run this plugin as a child bridge:
```json
{
"platforms": [
{
"platform": "SenseEnergyMonitor",
"name": "Sense Energy Monitor",
"username": "your@email.com",
"password": "your_sense_password",
"_bridge": {
"username": "CC:22:3D:E3:CE:31",
"port": 51827
}
}
]
}
```
## š HomeKit Features
### What You'll See in Apple Home App
- **Main Energy Monitor**: Shows as an "outlet" that's on/off based on power threshold
- **Device Outlets**: Individual Sense devices appear as outlets (on when power > threshold)
- **Basic Status**: Only shows if devices are consuming power, not actual power amounts
- **Simple Automation**: Can trigger automations when devices turn on/off
ā ļø **Important HomeKit Limitations:**
- Power consumption amounts (watts) are **NOT** displayed in Apple Home app
- Energy costs and usage data are **NOT** visible in HomeKit
- Solar power generation is **NOT** shown in HomeKit
- For actual power data, you must use the Eve app
### Eve App Integration
When `enableHistory` is enabled and fakegato-history is installed:
- **Power History**: Historical consumption graphs with actual watt values
- **Voltage/Current**: Technical measurements for analysis
- **Data Export**: Historical data export capabilities
## š§ Advanced Features
### Real-time WebSocket Streaming
- Live device status updates from Sense API
- Automatic reconnection with exponential backoff
- Rate limiting to prevent API abuse
- Powers the on/off threshold detection
### Smart Authentication
- Token caching for improved performance
- Automatic token refresh
- MFA/2FA support for secured accounts
- Graceful authentication failure handling
- Secure credential storage
### Error Resilience
- Comprehensive error handling and logging
- Automatic recovery from network issues
- Graceful degradation when services unavailable
- No unhandled exceptions
## š ļø API Integration
This plugin integrates with the comprehensive Sense API providing:
### Data Sources
- **Real-time Stream**: Live power data via WebSocket
- **Trend Data**: Historical consumption and production metrics
- **Device Detection**: Automatic smart device discovery
- **Monitor Status**: System health and connectivity
### Rate Limiting
- Respects Sense API rate limits
- Configurable polling intervals
- WebSocket connection management
- Smart caching to reduce API calls
## š Data Collection & Monitoring
### What the Plugin Collects from Sense API
- **Active Power**: Current total consumption in watts
- **Device Status**: Individual device power usage
- **Voltage**: Line voltage measurements
- **Device Detection**: Automatic device discovery
### What's Available in HomeKit (Apple Home App)
- **Device On/Off Status**: Based on configurable power thresholds only
- **Basic Automation**: Trigger when devices start/stop using power
### What's Available in Eve App Only
- **Power History**: Historical consumption graphs with actual watt values
- **Voltage/Current**: Technical measurements
- **Consumption Data**: Detailed power usage over time
ā ļø **Important**: Detailed energy data (watts, kWh, costs) is only accessible through the Eve app, not Apple's Home app.
## š Troubleshooting
### šØ **Critical Issues**
#### Plugin Not Loading / Callback Errors
```
This callback function has already been called by someone else
```
**Nuclear Reset Solution**:
1. **Stop Homebridge**: `sudo systemctl stop homebridge`
2. **Clear cache**: `sudo rm -rf ~/.homebridge/accessories/ ~/.homebridge/persist/`
3. **Set config**: `"individualDevices": false` (temporarily)
4. **Restart**: `sudo systemctl start homebridge`
#### Authentication Problems
```
Error: Authentication failed - invalid credentials
```
**Solution**:
- Verify Sense account credentials in Sense mobile app
- Ensure account has access to the monitor
- Check for typos in username/password
- Wait 15 minutes if rate limited
#### Configuration Format Errors
```
No plugin was found for the accessory "SensePowerMeter"
```
**Solution**: Update config from accessory to platform format:
```json
// OLD (v2.0.x) - Remove this:
"accessories": [{"accessory": "SensePowerMeter", ...}]
// NEW (v2.3.0+) - Use this:
"platforms": [{"platform": "SenseEnergyMonitor", ...}]
```
### āļø **Common Issues**
#### Authentication Problems
```
Error: Authentication failed - invalid credentials
```
**Solution**: Verify Sense account credentials and ensure account has monitor access.
#### WebSocket Connection Issues
```
WebSocket error: Connection refused
```
**Solutions**:
- Check network connectivity and firewall settings
- Try disabling WebSocket: `"useWebSocket": false`
- Verify Sense service status
#### Rate Limiting
```
Rate limited - skipping realtime update
```
**Solution**: Increase polling interval: `"pollingInterval": 120`
#### Callback Function Errors
```
This callback function has already been called by someone else
```
**Solution**:
- **Disable individual device accessories**: Set `"individualDevices": false`
- **Clear Homebridge cache**: Remove `~/.homebridge/accessories/` and `~/.homebridge/persist/`
- **Restart Homebridge** completely
#### Missing Devices
```
No devices found
```
**Solutions**:
- Ensure Sense monitor has completed device detection
- Check device detection in Sense mobile app
- Wait for detection cycle to complete
### Debug Mode
Enable verbose logging for troubleshooting:
```json
{
"verbose": true
}
```
### Log Analysis
Key log messages:
- `ā
Authentication successful` - Login working
- `š WebSocket connected` - Real-time stream active
- `š Power: XXXXw` - Regular power updates
- `š± Retrieved X devices` - Device detection working
## šļø Development
### Requirements
- Node.js v20.0.0 or higher
- Homebridge v1.8.0 or higher
### Setup
```bash
git clone https://github.com/seeloesix/homebridge-sense-energy-monitor.git
cd homebridge-sense-energy-monitor
npm install
npm link
```
### Testing
```bash
# Run integration tests
node test-integration.js <username> <password>
# Or use environment variables
SENSE_USERNAME=your@email.com SENSE_PASSWORD=password npm test
```
### Linting
```bash
npm run lint
```
## š Performance
### System Requirements
- **Memory**: ~50MB RAM usage
- **CPU**: Minimal impact
- **Network**: WebSocket connection + periodic API calls
- **Storage**: <1MB for caching and history
### Optimization Features
- Authentication token caching
- Rate-limited API calls
- Efficient WebSocket connection management
- Smart polling intervals
- Minimal HomeKit characteristic updates
## š Privacy & Security
### Data Handling
- ā
**No Analytics**: No user tracking or data collection
- ā
**Local Storage**: All data stored locally in Homebridge directory
- ā
**Secure Caching**: Encrypted credential storage
- ā
**Minimal Data**: Only necessary data cached locally
### Network Security
- HTTPS API connections
- WSS WebSocket encryption
- No external data transmission
- Homebridge network isolation support
## š Comparison with Existing Plugins
| Feature | This Plugin (v2.4.0) | homebridge-sense-power-meter |
|---------|----------------------|------------------------------|
| Plugin Type | ā
Dynamic Platform | ā Static Accessory |
| Real-time Updates | ā
WebSocket + Polling | ā Polling Only |
| Device Status Tracking | ā
50+ Devices | ā No Support |
| HomeKit Power Display | ā On/Off Status Only | ā Limited |
| Error Handling | ā
Nuclear Reset System | ā Basic |
| MFA Support | ā
2FA/MFA Support | ā No Support |
| Configuration GUI | ā
User-Friendly | ā Basic |
| Eve App Support | ā
Historical Data | ā No Support |
| Verification Status | ā
Verification Ready | ā Abandoned (5+ years) |
| Node.js Support | ā
v20+ (Latest LTS) | ā Outdated |
| HomeKit Energy Data | ā Requires Eve App | ā Limited |
**Note**: No HomeKit plugin can display actual power consumption in Apple's Home app due to platform limitations.
## š¤ Contributing
1. Fork the repository
2. Create a feature branch: `git checkout -b feature/amazing-feature`
3. Make your changes and add tests
4. Commit changes: `git commit -m 'Add amazing feature'`
5. Push to branch: `git push origin feature/amazing-feature`
6. Open a Pull Request
### Development Guidelines
- Follow existing code style
- Add appropriate error handling
- Update documentation
- Test thoroughly
- No breaking changes without major version bump
## š License
MIT License - see [LICENSE](LICENSE) file for details.
## š Credits
- **API Integration**: Based on [tadthies/sense](https://github.com/tadthies/sense)
- **Original Inspiration**: [Cisien/homebridge-sense-power-meter](https://github.com/Cisien/homebridge-sense-power-meter)
- **WebSocket Implementation**: Inspired by [brbeaird/sense-energy-node](https://github.com/brbeaird/sense-energy-node)
## š Support
- **Issues**: [GitHub Issues](https://github.com/seeloesix/homebridge-sense-energy-monitor/issues)
- **Discussions**: [GitHub Discussions](https://github.com/seeloesix/homebridge-sense-energy-monitor/discussions)
- **Homebridge Discord**: #plugin-support channel
- **Documentation**: [Plugin Wiki](https://github.com/seeloesix/homebridge-sense-energy-monitor/wiki)
## š
Changelog
### v2.1.0 - Verification Release
- šļø **BREAKING**: Converted from accessory to dynamic platform
- ā
**Verification**: Meets all Homebridge verification requirements
- š **Performance**: Improved authentication caching and error handling
- š± **Features**: Enhanced individual device support and configuration options
- š§ **Stability**: Comprehensive error handling and graceful degradation
- š **Monitoring**: Extended power monitoring and historical data features
- šļø **Configuration**: Enhanced configuration schema with validation
### v2.0.0 - Major Rewrite
- Complete rewrite with comprehensive API integration
- Added WebSocket support for real-time data
- Integrated tadthies/sense API methods
- Added solar power support
- Enhanced error handling and reconnection
- Added fakegato-history support
- Improved HomeKit characteristics
- Added device monitoring capabilities
### v1.0.0 - Initial Release
- Basic functionality (accessory-based)
- Simple power monitoring
- Basic Sense API integration
## š **Current Status & Limitations**
### ā
**What Works (v2.4.0)**
- **Device Status Detection**: Shows when devices are on/off in HomeKit
- **Sense API Integration**: Reliable data collection from Sense monitors
- **Eve App Integration**: Historical power data visualization
- **WebSocket Streaming**: Real-time device status updates
- **MFA Support**: Works with 2FA-enabled Sense accounts
- **Verification Compliance**: Meets all Homebridge requirements
### ā ļø **Known Limitations**
- **HomeKit Display**: Apple Home app only shows on/off status, not power amounts
- **No Energy Costs**: Cannot display energy costs or consumption totals in HomeKit
- **No Solar Display**: Solar power data collected but not shown in HomeKit
- **Threshold-Based**: Only detects devices above configurable power threshold
### š® **HomeKit Platform Limitations**
- HomeKit has no native support for energy monitoring
- Power consumption data requires third-party apps like Eve
- Apple's Home app ignores custom energy characteristics
- Energy automation limited to basic on/off triggers
---
**ā If this plugin helps you monitor your home energy usage, please consider giving it a star on GitHub!**