Last active
          June 17, 2022 05:34 
        
      - 
      
 - 
        
Save adampetrovic/8b76363671867c5024c47e827c6ee4b7 to your computer and use it in GitHub Desktop.  
    InfluxDB (Flux): Calculating power cost from iotawatt using a time of use tariff and daily supply charge
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import "date" | |
| import "timezone" | |
| import "dict" | |
| option location = timezone.location(name: "Australia/Sydney") | |
| tariffs = dict.fromList(pairs: [{key: "peak", value: 0.4070}, {key: "shoulder", value: 0.1815}, {key: "offpeak", value: 0.1540}, {key: "evoffpeak", value: 0.0770}]) | |
| dailySupplyCharge = 1.0494 | |
| getPrice = (timeVal) => { | |
| hour = date.hour(t: timeVal) | |
| return if date.weekDay(t: timeVal) >= date.Monday and date.weekDay(t: timeVal) <= date.Friday then | |
| if hour >= 14 and hour < 20 then | |
| dict.get(dict: tariffs, key: "peak", default: 0.0) | |
| else if hour >= 20 then | |
| dict.get(dict: tariffs, key: "offpeak", default: 0.0) | |
| else if hour < 4 then | |
| dict.get(dict: tariffs, key: "evoffpeak", default: 0.0) | |
| else if hour >= 4 and hour < 7 then | |
| dict.get(dict: tariffs, key: "offpeak", default: 0.0) | |
| else if hour >=7 and hour < 14 then | |
| dict.get(dict: tariffs, key: "shoulder", default: 0.0) | |
| else | |
| 0.0 | |
| else | |
| if hour >= 7 and hour < 22 then | |
| dict.get(dict: tariffs, key: "shoulder", default: 0.0) | |
| else | |
| dict.get(dict: tariffs, key: "offpeak", default: 0.0) | |
| } | |
| from(bucket: "iotawatt") | |
| |> range(start: v.timeRangeStart, stop: v.timeRangeStop) | |
| |> filter(fn: (r) => r["_measurement"] == "GridUsage") | |
| |> aggregateWindow(every: 1h, fn: sum, createEmpty: false) | |
| |> timeShift(duration: -1h) | |
| |> map(fn: (r) => ({r with _value: (r._value / 1000.0) * getPrice(timeVal: r._time)})) | |
| |> aggregateWindow(every: 1d, fn: sum, createEmpty: false) | |
| |> timeShift(duration: -1d) | |
| |> map(fn: (r) => ({r with _value: r._value + dailySupplyCharge})) | |
| |> yield(name: "cost") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment