File: export_csv.py

package info (click to toggle)
python-aranet4 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 232 kB
  • sloc: python: 1,845; makefile: 5
file content (45 lines) | stat: -rw-r--r-- 914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import csv

import aranet4

# Aranet4 MAC address
device_mac = "XX:XX:XX:XX:XX:XX"

# Selection filter. Will export last 25 records
entry_filter = {
    "last": 25
}

# Fetch results
records = aranet4.client.get_all_records(
    device_mac,
    entry_filter,
    remove_empty=True # This will remove blank records, if range parameters (start,end,last) are specified
)

# Write CSV file
with open(file="aranet_history.csv", mode="w", encoding="utf-8") as csv_file:
    writer = csv.writer(csv_file)

    header = [
        "date",
        "co2",
        "temperature",
        "humidity",
        "pressure"
    ]

    # Write CSV header
    writer.writerow(header)

    # Write CSV rows
    for line in records.value:
        row = [
            line.date.isoformat(),
            line.co2,
            line.temperature,
            line.humidity,
            line.pressure
        ]

        writer.writerow(row)