Shelly Solar Control
Selfmade Python-Tool – schaltet dein Shelly-Relais automatisch, sobald deine PV-Anlage genug Strom ins Netz einspeist.
Self-made Python tool – automatically triggers your Shelly relay as soon as your PV system feeds enough power into the grid.
🔆 Was macht das Tool?
🔆 What does the tool do?
Das Python-Script pvshelly.py fragt alle 30 Sekunden die SolarEdge Monitoring API ab (currentPowerFlow: PV, GRID, LOAD, STORAGE), berechnet daraus die aktuelle Netzeinspeisung und liest zusätzlich den Batterie-Ladestand (USOC) über eine Batterie-REST-API (z. B. sonnenBatterie, per Auth-Token).
The Python script pvshelly.py polls the SolarEdge Monitoring API every 30 seconds (currentPowerFlow: PV, GRID, LOAD, STORAGE), computes the current grid feed-in and additionally reads the battery charge (USOC) from a battery REST API (e.g. sonnenBatterie, via auth token).
Liegt die Einspeisung mindestens 5 Minuten am Stück über 6000 W (und läuft gerade keine Sperrzeit), schaltet das Script dein Shelly per HTTP-RPC ein (Switch.Set) und 5 Sekunden später wieder aus – perfekt als Impuls für Boiler, Poolpumpe & Co. Danach greift eine Sperrzeit von 60 Minuten. Fällt die Einspeisung unter die Schwelle, wird der Timer zurückgesetzt. Alles landet im Log shelly_log.txt, das täglich um 01:00 Uhr automatisch geleert wird.
If the feed-in stays above 6000 W for at least 5 minutes straight (and no lockout is active), the script switches your Shelly on via HTTP RPC (Switch.Set) and off again 5 seconds later – a perfect impulse for boilers, pool pumps etc. A 60-minute lockout follows. If the feed-in drops below the threshold, the timer resets. Everything is written to shelly_log.txt, which is cleared automatically every day at 1:00 AM.
- 6000 W SchwelleThreshold
- 5 min HaltezeitHold time
- 60 min SperrzeitLockout
- 30 s Poll-IntervallPoll interval
- 01:00 Log-ResetLog reset
📌 Voraussetzungen
📌 Requirements
- Python 3.8 oder höher ist installiertPython 3.8 or later is installed
- Das Python-Paket
requests(Installation in Schritt 1)The Python packagerequests(installed in step 1) - SolarEdge-Monitoring-Konto mit API-Schlüssel (Admin → Zugriffssteuerung → API-Schlüssel erstellen) und deiner Site ID aus dem DashboardSolarEdge Monitoring account with API key (Admin → Access Control → Create API Key) and your Site ID from the dashboard
- Shelly-Gerät (z. B. Shelly Plus 1 oder Shelly Pro 2) mit bekannter IP-Adresse und aktivierter Web-APIShelly device (e.g. Shelly Plus 1 or Shelly Pro 2) with a known IP address and the Web API enabled
- Optional: Batterie-REST-API (z. B. sonnenBatterie) mit URL und Auth-Token für den USOC-WertOptional: battery REST API (e.g. sonnenBatterie) with URL and auth token for the USOC value
Python & requests installieren
Install Python & requests
Du brauchst Python 3.8+ und das requests-Modul:
You need Python 3.8+ and the requests module:
pip install requests
Repo holen
Get the repo
Das Script liegt open source auf GitHub – einfach klonen:
The script is open source on GitHub – just clone it:
git clone https://github.com/Secoolioo/shelly-solar-control.git
cd shelly-solar-control
Konfiguration anpassen
Adjust the configuration
Öffne pvshelly.py und trage oben deine Werte ein:
Open pvshelly.py and enter your values at the top:
# pvshelly.py – Konfiguration / configuration
SHELLY_IP = "192.168.1.100" # IP deines Shelly / your Shelly's IP
SOLAREDGE_API_KEY = "DEIN_API_KEY" # SolarEdge Monitoring API-Key
SOLAREDGE_SITE_ID = "DEINE_SITE_ID" # Site ID aus dem Dashboard / from dashboard
USOC_URL = "http://192.168.1.50/api/v2/status" # Batterie-REST-API / battery REST API
HEADERS = {"Auth-Token": "DEIN_AUTH_TOKEN"} # Auth-Token der Batterie / battery auth token
| Variable | BedeutungMeaning |
|---|---|
SHELLY_IP |
IP-Adresse deines Shelly im lokalen NetzIP address of your Shelly on the local network |
SOLAREDGE_API_KEY |
API-Schlüssel aus dem SolarEdge-Monitoring (Admin → Zugriffssteuerung)API key from SolarEdge Monitoring (Admin → Access Control) |
SOLAREDGE_SITE_ID |
Deine Anlagen-ID aus dem SolarEdge-DashboardYour site ID from the SolarEdge dashboard |
USOC_URL |
REST-URL der Batterie für den Ladestand (USOC), z. B. sonnenBatterieREST URL of the battery for the state of charge (USOC), e.g. sonnenBatterie |
HEADERS |
Auth-Token-Header für die Batterie-APIAuth-Token header for the battery API |
Schwellwerte anpassen
Adjust the thresholds
Direkt darunter steuerst du, wann geschaltet wird – die Defaults sind ein guter Start:
Right below you control when switching happens – the defaults are a good start:
TRIGGER_THRESHOLD = 6000 # Einspeise-Schwelle in Watt / feed-in threshold (W)
HOLD_TIME = 300 # Mindestdauer in Sekunden (5 min) / hold time (s)
LOCKOUT_TIME = 3600 # Sperrzeit in Sekunden (60 min) / lockout (s)
Starten
Run it
Script starten – ab jetzt wird alle 30 Sekunden gepollt und alles in shelly_log.txt protokolliert:
Start the script – from now on it polls every 30 seconds and logs everything to shelly_log.txt:
python pvshelly.py
Dauerbetrieb: systemd-Service (Raspberry Pi/Server)
24/7 operation: systemd service (Raspberry Pi/server)
Damit das Script rund um die Uhr läuft und nach einem Neustart automatisch wieder startet, richtest du es als systemd-Service ein. Unit-Datei anlegen:
To keep the script running around the clock and restart it automatically after a reboot, set it up as a systemd service. Create the unit file:
[Unit]
Description=Shelly Solar Control (pvshelly)
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi/shelly-solar-control
ExecStart=/usr/bin/python3 /home/pi/shelly-solar-control/pvshelly.py
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Dann aktivieren und starten:
Then enable and start it:
sudo systemctl daemon-reload
sudo systemctl enable --now pvshelly.service
systemctl status pvshelly.service
Troubleshooting
- API-Key falsch? Dann stehen Fehlermeldungen wie „Error retrieving PV data“ im Log – Key und Site ID prüfen.
- Shelly nicht erreichbar? IP-Adresse checken und sicherstellen, dass die Web-API am Shelly aktiviert ist.
- Log live ansehen:
tail -f shelly_log.txt
- Wrong API key? The log will show errors like "Error retrieving PV data" – check key and Site ID.
- Shelly not reachable? Check the IP address and make sure the Web API is enabled on the Shelly.
- Watch the log live:
tail -f shelly_log.txt
🐙 Open Source auf GitHub
🐙 Open source on GitHub
Das komplette Script ist open source – Code lesen, forken, verbessern:
The complete script is open source – read the code, fork it, improve it:
Shelly Solar Control auf GitHub Shelly Solar Control on GitHub