Reading multiple rows from csv

Here’s how i read multiple rows from a csv file and write them into a variables attribute with a python script.
The csv file with daily PV energy values:

date;pv_total;to_net;from_net;house;to_battery;from_battery
20210724;89.75;60.58;1.11;30.74;10.65;11.07
20210725;114.83;87.21;3.95;28.29;10.69;7.43
20210726;93.48;56.91;0.29;37.93;10.70;11.76
20210727;102.52;73.59;1.32;28.91;10.67;9.31
20210728;96.00;67.65;0.75;27.23;10.82;8.95
20210729;129.42;90.80;0.46;40.26;10.36;11.52
20210730;153.06;120.73;2.32;32.20;10.67;8.22
20210731;123.30;95.01;0.13;28.35;10.28;10.30
20210801;93.31;64.61;3.00;31.04;10.45;9.79
20210802;94.50;55.34;2.20;45.91;12.02;16.53

The python script (Trial and error, i’m no python expert) :slight_smile:

import csv
import requests

csv_file = '/home/homeassi/.homeassistant/daily_energy.csv'
url = 'http://localhost:8123/api/states/variable.'
days = 7

headers = {
    'Authorization': 'Bearer abcdefg',
    'content-type': 'application/json'
}

# Read the csv_file
csv_reader = csv.DictReader(open(csv_file), delimiter=';')
# Take the last (days) rows
the_list = [dict(d) for d in csv_reader][-days:]
# Revert the list, we want the last row at the top
the_list.reverse()

errors = ''

variable = 'daily_pv_energy'
state = '1' # a dummy state
attributes = '{"entries": ' + str(the_list) + '}'

data = '{"state": "' + str(state) + '", "attributes": ' + attributes + '}'
data = data.replace("'",'"')
#print(data)
r = requests.post(url+variable, data=data, headers=headers)
if r.status_code != 200 and r.status_code != 201:
    errors = errors + 'ERROR:' + variable + ' - ' + str(r.status_code)

if errors != '':
    print(errors)

It’s called from cron if a new day starts.

# Daily PV energy to variable for list feed
1 0 * * *	/usr/bin/python3 /home/homeassi/scripts/variable_set_daily_pv_energy.py

Here’s how the variables attribute is created.

And here how i show the entries in a list card:
Auswahl_469

          - type: custom:list-card
            entity: variable.daily_pv_energy
            title: Letzte 7 Tage (kWh)
            feed_attribute: entries
            columns:
              - title: Datum
                field: date
                style:
                  - text-align: left
              - title: PV
                field: pv_total
                style:
                  - text-align: right
              - title: Haus
                field: house
                style:
                  - text-align: right
              - title: Netz
                field: from_net
                style:
                  - text-align: right
              - title: Einsp.
                field: to_net
                style:
                  - text-align: right
              - title: Batt.
                field: to_battery
                style:
                  - text-align: right
              - title: Entl.
                field: from_battery
                style:
                  - text-align: right

Hope that helps.

8 Likes