--json = require "json" json = require "JSON" tibber_rates = 'tibber_rates_20230318.json' power_profile = 'bosch_program_1.power_profile' function fmt( t ) res = '' if t >= 24 then t = t - 24 res = '+' end return res .. t end -- function fmt function to_eur( x ) return math.floor( x * 100 + 0.5 ) / 100 end -- function fmt file = io.open( tibber_rates, 'rb' ) txt = file:read '*a' --res = json.decode( txt ) res = json:decode( txt ) prices = {} for x, y in pairs( res["data"]["viewer"]["homes"][1]["currentSubscription"]["priceInfo"][ "today" ] ) do prices[ x - 1 ] = y[ "total" ] end for x, y in pairs( res["data"]["viewer"]["homes"][1]["currentSubscription"]["priceInfo"][ "tomorrow" ] ) do prices[ x - 1 + 24 ] = y[ "total" ] end min_start = math.huge min_price = math.huge max_start = -math.huge max_price = -math.huge for h = os.date("*t").hour * 10, 460 -- search from now, interval: 1/10 h = 6 min do i=0 cost = 0 last_p = 0 last_t = 0 for l in io.lines( power_profile ) do if i > 2 -- skip header then t = tonumber( string.sub( l, 1, 10 ) ) p = tonumber( string.sub( l, 60, 99 ) ) if i == 3 then profile_start_time = t end -- shift power_profile to t = 0 t = t - profile_start_time + h / 10 * 3600 if last_t ~= 0 then h_temp = math.floor( t / 3600 ) cost = cost + ( p + last_p ) / 2 * ( t - last_t ) * prices[ math.floor( t / 3600 ) ] / 1000 / 3600 end last_p = p last_t = t end i = i +1 end -- for l in io.lines if cost < min_price then min_price = cost min_start = h end if cost > max_price then max_price = cost max_start = h end end -- for h print( '', 'hour', 'total cost' ) print( 'max: ', fmt( max_start/10 ), to_eur( max_price ) ) print( 'min: ', fmt( min_start/10 ), to_eur( min_price ) ) print ( 'max-min: ', to_eur( max_price - min_price ) )