Skip to content

Rqbln/TempoBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

95 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ TempoBot

Smart electricity consumption management with EDF Tempo calendar

Python Swift iOS Firebase License Platform


πŸ“‹ Table of Contents


🎯 About

TempoBot is a complete solution for intelligently managing your electrical device consumption by leveraging the EDF Tempo calendar. The project combines:

  • πŸ€– A Python bot/server : Automates control of smart plugs via Tasmota, MQTT, and Firebase
  • πŸ“± A native iOS application : SwiftUI interface to configure and monitor your plugs in real-time

The system automatically detects the Tempo day color (blue, white, red) and adjusts electricity consumption based on pricing, while allowing fine-grained schedule customization.


✨ Features

πŸ”„ Automatic EDF Tempo Calendar Tracking

  • βœ… Automatic detection of the day's color (blue, white, red)
  • βœ… J+1 forecast for optimal planning
  • βœ… Remaining days counter per color for the year

βš™οΈ Smart Plug Control

  • βœ… Automatic on/off based on Tempo color
  • βœ… Peak/off-peak hours management per color
  • βœ… Custom schedules by day of the week
  • βœ… Manual override with one click from the iOS app

πŸ”Œ Tasmota Integration

  • βœ… Remote control via MQTT
  • βœ… Real-time synchronization with Firebase
  • βœ… Simplified configuration from the iOS app

πŸ“± Native iOS Application

  • βœ… Modern and intuitive SwiftUI interface
  • βœ… Secure Firebase authentication
  • βœ… Simplified Wi-Fi and MQTT configuration
  • βœ… Real-time plug status
  • βœ… Complete schedule management

πŸ”” Notifications

  • βœ… Discord integration for alerts and notifications
  • βœ… Error and crash handling with retry system

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      TempoBot Ecosystem                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   iOS App    │◄─────►│   Firebase   │◄─────►│  Python  β”‚  β”‚
β”‚  β”‚  (SwiftUI)   β”‚      β”‚  Realtime DB β”‚      β”‚   Bot    β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜  β”‚
β”‚         β”‚                      β”‚                    β”‚        β”‚
β”‚         β”‚                      β”‚                    β”‚        β”‚
β”‚         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚
β”‚                                β”‚                              β”‚
β”‚                         β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”                      β”‚
β”‚                         β”‚   Tasmota   β”‚                      β”‚
β”‚                         β”‚  (MQTT)     β”‚                      β”‚
β”‚                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                      β”‚
β”‚                                                               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Data Flow

  1. EDF API β†’ Retrieves the day's Tempo color
  2. Python Bot β†’ Processes and updates Firebase
  3. Firebase β†’ Synchronizes with the iOS app
  4. MQTT β†’ Controls Tasmota plugs
  5. Discord β†’ Notifications and alerts (optional)

πŸ› οΈ Technologies Used

Backend (Python)

Technology Version Usage
Python 3.x Main language
Firebase Admin SDK Latest Database management
Discord.py Latest Discord notifications
Requests Latest EDF Tempo API
MQTT - Communication with Tasmota
miniupnpc Latest UPnP port forwarding

Frontend (iOS)

Technology Version Usage
Swift 5.0+ Main language
SwiftUI Latest User interface
Firebase iOS SDK 9.6.0 Authentication & Database
CocoaAsyncSocket 7.6.5 Network communication

Services

  • πŸ”₯ Firebase : Authentication, Realtime Database, Analytics
  • πŸ“‘ MQTT : Communication with Tasmota devices
  • 🌐 EDF API : Tempo calendar retrieval

πŸ“¦ Prerequisites

For Python Backend

  • Python 3.x installed
  • Access to an MQTT server (Mosquitto)
  • Firebase account with Realtime Database enabled
  • (Optional) Discord bot for notifications

For iOS Application

  • Xcode 14.2+ installed
  • iOS 16.0+ (simulator or physical device)
  • Apple developer account (for device deployment)
  • Configured GoogleService-Info.plist file

Hardware

  • Tasmota-compatible plug(s)
  • Local network access (Wi-Fi)
  • UPnP-compatible router (optional, for port forwarding)

πŸš€ Installation

1. Clone the Repository

git clone https://github.com/votre-user/TempoBot.git
cd TempoBot

2. Python Backend Setup

cd Python

# Create a virtual environment (recommended)
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install requests firebase-admin discord.py miniupnpc

3. Firebase Configuration

  1. Create a Firebase project on Firebase Console

  2. Enable services :

    • βœ… Authentication (Email/Password)
    • βœ… Realtime Database
    • βœ… Analytics (optional)
  3. Download configuration files :

    • Python : Service Account JSON β†’ Place in Python/tempobot-406fc-firebase-adminsdk-....json
    • iOS : GoogleService-Info.plist β†’ Add to Xcode project

4. MQTT Configuration

Ensure an MQTT server (Mosquitto) is accessible on your network:

# Install Mosquitto (example on Ubuntu/Debian)
sudo apt-get install mosquitto mosquitto-clients

# Verify the service is active
sudo systemctl status mosquitto

5. iOS Application Setup

  1. Open the project in Xcode :

    open iOS/TempoBot.xcodeproj
  2. Configure the Bundle Identifier in project settings

  3. Add GoogleService-Info.plist to the app :

    • Drag and drop the file into the Xcode project
    • Check "Copy items if needed"
  4. Install dependencies (automatically managed by Swift Package Manager)


βš™οΈ Configuration

Python Bot Configuration

Edit Python/tasmota.py and adjust parameters:

# MQTT server IP address (if different)
# By default, uses mosquitto_pub command line

# Discord configuration (optional)
DISCORD_TOKEN = "your_discord_token"
CHANNEL_ID = 1120669437528379464  # Your Discord channel ID

Firebase Realtime Database Configuration

Database structure:

{
  "data": {
    "date": "2024/01/15",
    "dateJ1": "2024/01/16",
    "couleurJ": "TEMPO_BLEU",
    "couleurJ1": "TEMPO_BLANC",
    "Jrest_bleu": 300,
    "Jrest_blanc": 43,
    "Jrest_rouge": 22,
    "Pleines_creuses": "pleines",
    "users": {
      "<uid>": {
        "prises": {
          "<id_prise>": {
            "nom": "Living Room Plug",
            "isOn": false,
            "isManualOn": null,
            "creuses_bleu": true,
            "creuses_blanc": false,
            "creuses_rouge": false,
            "pleines_bleu": true,
            "pleines_blanc": true,
            "pleines_rouge": false,
            "Lundi": "08:00-12:00",
            "Mardi": null,
            ...
          }
        }
      }
    }
  }
}

Firebase Security Rules

Configure security rules in Firebase Console:

{
  "rules": {
    "data": {
      ".read": "auth != null",
      ".write": "auth != null",
      "users": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
        }
      }
    }
  }
}

πŸ’» Usage

Start the Python Bot

cd Python
python tasmota.py

The bot will:

  1. Connect to Firebase
  2. Retrieve the day's Tempo color from the EDF API
  3. Update the Firebase database
  4. Control Tasmota plugs according to configured rules
  5. Send Discord notifications (if configured)

Using the iOS App

  1. Launch the application from Xcode (⌘+R) or install on a device
  2. Create an account or sign in with Firebase Auth
  3. Add a plug :
    • Detect the tasmota-* SSID
    • Connect to the plug's Wi-Fi
    • Configure IP address and MQTT settings
  4. Configure schedules :
    • Choose peak/off-peak periods per Tempo color
    • Define custom schedules by day
    • Activate manual override if needed

Example Scripts

Port Forwarding (UPnP)

cd Python
python port_forwarding.py

Firebase Connection Test

cd Python
python testdtata.py

πŸ“ Project Structure

TempoBot/
β”‚
β”œβ”€β”€ πŸ“± iOS/
β”‚   β”œβ”€β”€ TempoBot/
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ Authentification/
β”‚   β”‚   β”‚   β”œβ”€β”€ Login.swift
β”‚   β”‚   β”‚   β”œβ”€β”€ SignUp.swift
β”‚   β”‚   β”‚   └── CompteView.swift
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ Connexion/
β”‚   β”‚   β”‚   β”œβ”€β”€ ConnexionReseauView.swift
β”‚   β”‚   β”‚   β”œβ”€β”€ ConnexionReseau2View.swift
β”‚   β”‚   β”‚   └── NouvellePriseView.swift
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ Prise/
β”‚   β”‚   β”‚   β”œβ”€β”€ HeuresPickerView.swift
β”‚   β”‚   β”‚   β”œβ”€β”€ IsManualOnView.swift
β”‚   β”‚   β”‚   └── InfoPriseView.swift
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ Reseau/
β”‚   β”‚   β”‚   β”œβ”€β”€ NetworkDiscovery.swift
β”‚   β”‚   β”‚   └── ConnectionStateChecker.swift
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ ContentView.swift
β”‚   β”‚   β”œβ”€β”€ MainPage.swift
β”‚   β”‚   β”œβ”€β”€ PriseList.swift
β”‚   β”‚   β”œβ”€β”€ ReadDatas.swift
β”‚   β”‚   └── TempoBotApp.swift
β”‚   β”‚
β”‚   └── TempoBot.xcodeproj/
β”‚
β”œβ”€β”€ 🐍 Python/
β”‚   β”œβ”€β”€ tasmota.py              # Main bot script
β”‚   β”œβ”€β”€ port_forwarding.py      # UPnP port forwarding script
β”‚   β”œβ”€β”€ test.py                 # Automation example
β”‚   β”œβ”€β”€ testdtata.py            # Firebase read/write example
β”‚   └── tempobot-406fc-firebase-adminsdk-....json
β”‚
β”œβ”€β”€ πŸ“„ README.md
└── πŸ“„ .deepsource.toml

Key Files

File Description
Python/tasmota.py Main script handling Tempo & Tasmota logic
iOS/TempoBot/TempoBotApp.swift iOS application entry point
iOS/TempoBot/ContentView.swift Authentication and navigation management
iOS/TempoBot/MainPage.swift Main page after authentication
Python/port_forwarding.py UPnP port forwarding script

πŸ“š Documentation

EDF Tempo API

The bot uses EDF's public API to retrieve Tempo information:

  • Day color : https://particulier.edf.fr/services/rest/referentiel/searchTempoStore?dateRelevant=YYYY-MM-DD
  • Remaining days : https://particulier.edf.fr/services/rest/referentiel/getNbTempoDays?TypeAlerte=TEMPO

Tasmota MQTT Topics

The bot uses standard Tasmota MQTT topics:

  • Command : cmnd/<device_id>/power
  • Status : stat/<device_id>/POWER

Firebase Architecture

See the Firebase Configuration section for the complete database structure.


🀝 Contributing

Contributions are welcome! To contribute:

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Guidelines

  • Follow existing code conventions
  • Add comments for complex code
  • Test your changes before submitting a PR
  • Update documentation if necessary

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for more details.


πŸ™ Acknowledgments

  • Tasmota for the open-source firmware
  • EDF for the Tempo API
  • Firebase for backend services
  • The open-source community

Made with ❀️ to optimize your electricity consumption

⬆ Back to top

About

πŸ”Œβš‘ Smart electricity consumption management with EDF Tempo calendar

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors