すでに、やりたい事5:色々と改良で売買電力の計測は行っているのですが、金額換算で見てみたくなり、さくっと金額レポートがメールで届くように修正しました。
変更内容はこんな感じです
requests.py
def send_email(): try: #================================= # 昨日の時間範囲 #================================= post_datetime = datetime.datetime.now() post_datetime = post_datetime + timedelta(hours=9) post_datetime_s = datetime.datetime(post_datetime.year, post_datetime.month, post_datetime.day,0,0,0,0) post_datetime_s = post_datetime_s - timedelta(hours=24) post_datetime_e = post_datetime_s + timedelta(hours=24) post_datetime_s = post_datetime_s - timedelta(hours=9) post_datetime_e = post_datetime_e - timedelta(hours=9) sql = ("SELECT * FROM ListHour WHERE date_time >= DATETIME('%s') AND date_time < DATETIME('%s') ORDER BY date_time ASC" % (post_datetime_s, post_datetime_e)) power_consumption = db.GqlQuery(sql) power_consumption = power_consumption.fetch(60) out_power = [] gen_watt_total = 0 use_watt_total = 0 sell_watt_total = 0 buy_watt_total = 0 txt = "" for fav in power_consumption: gen_watt = fav.watt_value1 use_watt = fav.watt_value1 + fav.watt_value2 gen_watt_total = gen_watt_total + gen_watt use_watt_total = use_watt_total + use_watt if fav.watt_sell != None: sell_watt_total = sell_watt_total + fav.watt_sell if fav.watt_buy != None: buy_watt_total = buy_watt_total + fav.watt_buy txt = txt + (fav.date_time + timedelta(hours=9)).strftime("%Y/%m/%d %H:%M") + ',' + str(gen_watt / fav.write_cnt) + ',' + str(use_watt / fav.write_cnt) + '\r\n' gen_watt_total = float(gen_watt_total) / (30.0 * 1000.0) use_watt_total = float(use_watt_total) / (30.0 * 1000.0) sell_watt_total = float(sell_watt_total) / (30.0 * 1000.0) buy_watt_total = float(buy_watt_total) / (30.0 * 1000.0) sender_address = "you <?????????@????.????>" subject = u"電力計測結果" + post_datetime_s.strftime(" %Y/%m/%d") body = u'昨日の電力量結果を送信します。\n\n' body += u'総発電電力:' + str.format('{0:03.2f}', gen_watt_total) + u'kWh\n' body += u'総使用電力:' + str.format('{0:03.2f}', use_watt_total) + u'kWh\n' body += u'--------------------\n' body += u'買電電力(金額):' + str.format('{0:03.2f}', buy_watt_total) + u'kWh(' + str.format('{0:03.2f}', buy_watt_total * 24) + u'円)\n' body += u'売電電力(金額):' + str.format('{0:03.2f}', sell_watt_total) + u'kWh(' + str.format('{0:03.2f}', sell_watt_total * 37) + u'円)\n' body += u'--------------------\n' body += u'買電金額は24円/kWhで計算しています\n' body += u'売電金額は37円/kWhで計算しています\n' mail.send_mail(sender=sender_address, to=sender_address, subject=subject, body=body, attachments=[post_datetime_s.strftime("watt_%Y_%m_%d") + '.txt', txt]) except: logging.error("send mail error") logging.error(traceback.format_exc())こんな感じで毎日メールが届きます