MyElectricalData

repository·main·Indexed 41 days ago

https://github.com/myelectricaldata/myelectricaldata_import

Official client for the MyElectricalData platform, designed for users in France with Linky electric meters to import electricity consumption data from Enedis APIs. It supports data storage in SQLite or PostgreSQL and exports to MQTT, Home Assistant, InfluxDB, and VictoriaMetrics. The tool is available as a Docker image and Hassio Addon, featuring a FastAPI web server and automated background jobs for data import and export.

Tokens
11.1K
Snippets
30
Records
58
Agent score
50%

What's inside MyElectricalData

  1. Overview of MyElectricalData

    main

    MyElectricalData is the official client for the MyElectricaData platform. It is designed specifically for users in France with a Linky electric meter to import electricity consumption data from official Enedis APIs.

    Data Storage and Export

    Data is imported into either a local cache (SQLite) or an external backend (PostgreSQL). Once stored, the data can be exported to the following destinations:

    • MQTT
    • Home Assistant (via MQTT auto-discovery)
    • InfluxDB / VictoriaMetrics
    • PostgreSQL

    The tool also provides APIs for data access.

  2. Install the enedisgateway2mqtt Helm Chart

    main

    To install the enedisgateway2mqtt gateway (an Enocean to MQTT Gateway and Control Panel) on Kubernetes, use the helm upgrade --install command. You can provide configuration via a values.yaml file or using the --set flag.

    Requirements:

    • Kubernetes version: >=1.16.0-0
    • Dependency: common (version 4.0.1) from https://library-charts.k8s-at-home.com
    # Install using a values file
    helm upgrade --install enedisgateway2mqtt Chart/ -f values.yaml
    
    # Install using --set flags
    helm install enedisgateway2mqtt \
      --set env.TZ="America/New York" \
        m4dm4rtig4n/enedisgateway2mqtt
    
    # Install using a values file with a remote repository
    helm upgrade --install enedisgateway2mqtt m4dm4rtig4n/enedisgateway2mqtt -f values.yaml
  3. Configure enedisgateway2mqtt

    main

    Configuration is managed via Helm values. You can customize the deployment in two ways:

    1. Using values.yaml: Edit the local Chart/values.yaml file. It contains several commented-out suggested values. Note that this chart also supports values from the common library.
    2. Using --set flags: Pass parameters directly via the CLI during installation.

    Example of setting the timezone via CLI: --set env.TZ="America/New York"

  4. Automated background jobs and exports

    main

    The application runs several background tasks using fastapi_utils.tasks.repeat_every upon startup. These tasks are managed by the Job class:

    1. Import Job: Runs every CYCLE seconds (defaulting to 14400s unless configured in config.yaml, with a minimum of cycle_minimun). It calls Job().boot().
    2. Home Assistant Export: Runs every 3600 seconds. It calls Job().export_home_assistant(target="ecowatt").
    3. Gateway Status: Runs every 600 seconds. It calls Job().get_gateway_status().
  5. Run MyElectricalData via uvicorn

    main

    The application is a FastAPI web server that can be started by running main.py. It uses uvicorn to serve the APP instance. By default, it listens on 0.0.0.0 at the port specified in your config.yaml.

    If the DEV or DEBUG environment variables are set to true, the server will run in reload mode. SSL configuration can also be applied if defined in the application configuration.

    python src/main.py
  6. Deploy MyElectricalData using Docker Compose

    main

    You can deploy the myelectricaldata service using Docker Compose. The service runs the m4dm4rtig4n/myelectricaldata:latest image and exposes the application on port 5000.

    Configuration Details

    • Volumes: The service maps a local ./data directory to the /data directory inside the container to persist data.
    • Environment Variables:
      • TZ: Sets the timezone for the container (defaults to Europe/Paris in the example).
    • Ports:
      • 5000:5000: Maps host port 5000 to container port 5000.
    version: "3.9"
    services:
      myelectricaldata:
        image: m4dm4rtig4n/myelectricaldata:latest
        restart: unless-stopped
        volumes:
          - ./data:/data
        environment:
          TZ: Europe/Paris
        ports:
          - 5000:5000
  7. Configure logging and debug mode

    main

    The application uses a config.yaml file located in the application data path to manage logging behavior.

    • Debug Mode: Set the DEBUG environment variable to true (using str2bool logic) to set the logging level to DEBUG. Otherwise, the level defaults to INFO.
    • File Logging: Enable file logging by setting log2file: true in config.yaml. When enabled, logs are written to {APPLICATION_PATH_LOG}/myelectricaldata.log and are also mirrored to the console.
    • Log Format: The format and date patterns are controlled by internal constants LOG_FORMAT and LOG_FORMAT_DATE.