Follow along with the video below to see how to install our site as a web app on your home screen.
Huomio: This feature may not be available in some browsers.
python
on ajettavissa komentorivilä. Windowsilla se pitää asentaa. Skripti testattu versiolla 3.10porssisahko.py
process_args
) ja hinnan raja-arvo (price_threshold
) haluttuun arvoonpython porssisahko.py
# Mikä prosessi käynnistetään. Muokkaa tähän haluamasi
process_args = ["boinc.exe"]
# Prosessi pysäytetään jos pörssisähkön hinta ylittää raja-arvon
price_threshold = 5 # snt/kWh
import subprocess
import time
from datetime import datetime, timedelta, timezone
# Jos tästä tulee virhe, aja komentorivillä 'pip install requests'
import requests
def get_latest_prices():
try:
response = requests.get("https://api.porssisahko.net/v1/latest-prices.json", timeout=60)
response.raise_for_status()
prices = response.json()["prices"]
for p in prices:
p["startDate"] = datetime.fromisoformat(p["startDate"].replace("Z", "+00:00"))
p["endDate"] = datetime.fromisoformat(p["endDate"].replace("Z", "+00:00"))
return prices
except requests.RequestException as e:
print(f"{e}: {e.response.text if e.response else None}")
def get_price(prices: list, at: datetime):
if not prices:
return None
return next((p["price"] for p in prices if p["startDate"] <= at < p["endDate"]), None)
def main():
print(f"Ajetaan {process_args} kun pörssisähkön hinta alittaa {price_threshold} snt/kWh")
prices = []
proc: subprocess.Popen = None
while True:
utcnow = datetime.now(timezone.utc)
# Nord Pool julkaisee seuraavan päivän tiedot klo 14 eli 10 tuntia ennen edellisten tietojen loppua.
if not prices or (prices[0]["endDate"] - utcnow) < timedelta(hours=9):
print(f"{utcnow.astimezone()} Haetaan pörssisähkön hinnat")
prices = get_latest_prices()
if prices:
print(f"{utcnow.astimezone()} Hinnat haettu {prices[0]['endDate'].astimezone()} asti")
price_now = get_price(prices, utcnow)
if price_now and price_now <= price_threshold:
if not proc:
print(f"{utcnow.astimezone()} Ajetaan {process_args} koska pörssisähkö on {price_now} snt/kWh")
proc = subprocess.Popen(process_args)
else:
if proc:
print(f"{utcnow.astimezone()} Pysäytetään {proc.args} koska pörssisähkö on {price_now} snt/kWh")
proc.terminate()
try:
exitcode = proc.wait(60)
except TimeoutError:
proc.kill()
exitcode = proc.wait(10)
proc = None
print(f"Exited with {exitcode}")
time.sleep(60)
if __name__ == "__main__":
main()
systemctl start boinc
systemctl stop boinc