...
Damit sieht man immer den Rang des aktuellen Preises in den nächsten X Stunden (bzw. den Rang in allen kommenden vorhandenen Stunden (bis 24:00 bzw. ab 14:00 bis nächster Tag 24:00) bzw. den Rang am heutigen Tag).
Möchte man das Auto z.B. 5h bis zum gewünschten Zeitpunkt laden, dann einfach dann, wenn der Rang <= 5 ist.
...
Codeblock | ||
---|---|---|
| ||
// This pico-c program returns the rank of the current energy price in the next nrHours. // This is useful if you want to e.g. charge your car for 5 hours over night until 7AM: // just call this program with the number of hours until 7AM and check if the result is <= 5 // // Note: Change "api.awattar.at" to "api.awattar.de" when you are in Germany // // Apache License 2.0 // v0.13 // input0 = number of hours to check rank of current price // output0 = current price // output1 = rank of current price in given numbers of hours to check (1-based) // output2 = rank of current price in all available prices (1-based // output3 = rank of current price in todays prices (1-based // NOTE: change "api.awattar.at" to "api.awattar.de" if you are in Germany //get the rank of a given price in an array of prices up to maxRank int getRank(float price, int maxRank, float *prices) { int n; // counter for hours int newn; int i; // counter float tmpPrice; int rank; //bubble sort the first nHours prices for (n = maxRank-1; n>0; n = newn) { newn = 0; for (i=0; i<n; i++) { if (prices[i] > prices[i+1]) { tmpPrice = prices[i+1]; prices[i+1] = prices[i]; prices[i] = tmpPrice; newn = i; } } } //determine the rank of the current price amongst the first nHours of the sorted prices rank = maxRank; for (i=0; i<maxRank; i++) { if (price == prices[i]) { rank =return i+1; break; } } return rankmaxRank; } //declaration of all variables outside of loop to prevent out-of-memory errors int nrHours; // number of hours to take into account char* webContent; //content of httpshttp://api.awattar.at/v1/marketdata/current.yaml?tomorrow=include char* marketprice; //marketprice content of httpshttp://api.awattar.at/v1/marketdata/current.yaml?tomorrow=include char hournum[4233]; //string to search for in webContent to get the marketprice of a specific hour float prices[3435]; // prices according to aWATTar API - up to 3435 hours including current hour (API returns prices until EOD up to 14:00, later until end of next day) float currentPrice; // the current price i.e. the price of the 0th hour int n; // counter for hours //for testing with pico-c interpreter see https://loxwiki.atlassian.net/wiki/spaces/LOX/pages/1520762981/PicoC+Interpreter //though most functions will not work in interpreter getinputevent(); //to clear buffer of changed inputs (to not run the loop twice) while(TRUE) { sleeps(30); //sleep some seconds to not fetch data again while input still changes and get correct data when aWATTar is late if (getinput(0)<0 || getinput(0)>34) { errorprintf("ERROR: PicoC aWATTar price rank - illegal number of hours to check (%d)\n", getinput(0)); break; } webContent = httpget("api.awattar.at", "/v1/marketdata/current.yaml?tomorrow=include"); //fetch all available data //fill marketprice array for (n=0; n<34n<35; n++) { sprintf(hournum, "data_price_hour_rel_+%02d_amount: ", n); marketprice = strstrstrstrskip(webContent, hournum); if (marketprice == 0) break; prices[n] = atof(marketprice+32); //fetch value after search-string "date_price_..." } free(webContent); currentPrice = prices[0]; setoutput(0, currentPrice); if (getinput(0) < n) { nrHours = getinput(0)+1; } else { nrHours = n; } setoutput(1, getRank(currentPrice, nrHours, prices)); setoutput(2, getRank(currentPrice, n, prices)); //find rank of currentPrice on this day for (n=0; n<24; n++) { sprintf(hournum, "price_threshold_%02d: ", n); marketprice = strstrskip(webContent, hournum); if (marketprice == 0) break; if (atof(marketprice) == currentPrice) { setoutput(3, n+1); } } free(webContent); //sleep until next hour (or nrHours changes) so we need to fetch only once an hour while(getcurrenttime()%3600 && !getinputevent()) { sleeps(1); } } |
...