Skip to content

Instantly share code, notes, and snippets.

@pdcastro
Created April 17, 2023 21:33
Show Gist options
  • Save pdcastro/961f7ef775a4aaac5a9bf5a7c5b0f482 to your computer and use it in GitHub Desktop.
Save pdcastro/961f7ef775a4aaac5a9bf5a7c5b0f482 to your computer and use it in GitHub Desktop.
Sample Python script to collect temperature and humidity data through the Tado cloud API
import asyncio
from dataclasses import dataclass
from datetime import datetime, timezone
from PyTado.interface import Tado
TADO_USERNAME = "xxxx"
TADO_PASSWORD = "xxxx"
@dataclass
class PollHandle:
handle: asyncio.Handle
def poll_tado() -> PollHandle:
tado = Tado(TADO_USERNAME, TADO_PASSWORD)
zones: list[dict] = tado.getZones()
zone: dict = next(filter(lambda x: x.get("type") == "HEATING", zones))
zone_id: int = zone["id"]
iso = datetime.fromisoformat
loop = asyncio.get_running_loop()
t0 = loop.time()
interval_sec = 60
count = 0
last_ts = ""
def callback():
nonlocal count, last_ts, poll_handle
now_utc = datetime.now(timezone.utc).strftime("%H:%M:%S")
data = tado.getState(zone_id)["sensorDataPoints"]
humidity = data["humidity"]["percentage"]
celsius = data["insideTemperature"]["celsius"]
ts = data["insideTemperature"]["timestamp"]
delta = (iso(ts) - iso(last_ts)).total_seconds() / 60 if last_ts else 0.0
print(f"{count:04} {now_utc=} {ts=} {delta=:.2F}minutes {celsius=} {humidity=}%")
last_ts = ts
count += 1
poll_handle.handle = loop.call_at(t0 + count * interval_sec, callback)
poll_handle = PollHandle(loop.call_soon(callback))
return poll_handle
async def main():
poll_handle = poll_tado()
while not poll_handle.handle.cancelled():
try:
await asyncio.sleep(10)
except asyncio.CancelledError: # CTRL-C
break
asyncio.run(main())
@pdcastro
Copy link
Author

pdcastro commented Apr 17, 2023

Sample script output:

0000 now_utc='20:05:26' ts='2023-04-17T19:54:07.445Z' delta=0.00minutes celsius=18.16 humidity=61.2%
0001 now_utc='20:06:26' ts='2023-04-17T19:54:07.445Z' delta=0.00minutes celsius=18.16 humidity=61.2%
0002 now_utc='20:07:26' ts='2023-04-17T19:54:07.445Z' delta=0.00minutes celsius=18.16 humidity=61.2%
...
0008 now_utc='20:13:26' ts='2023-04-17T20:12:44.557Z' delta=18.62minutes celsius=18.06 humidity=61.0%
0009 now_utc='20:14:26' ts='2023-04-17T20:12:44.557Z' delta=0.00minutes celsius=18.06 humidity=61.0%
...
0026 now_utc='20:31:26' ts='2023-04-17T20:31:07.500Z' delta=18.38minutes celsius=17.99 humidity=60.9%
...
0044 now_utc='20:49:26' ts='2023-04-17T20:48:52.970Z' delta=17.76minutes celsius=17.91 humidity=60.8%
...
0059 now_utc='21:04:26' ts='2023-04-17T21:04:12.696Z' delta=15.33minutes celsius=17.86 humidity=60.8%
...
0077 now_utc='21:22:26' ts='2023-04-17T21:21:56.369Z' delta=17.73minutes celsius=17.86 humidity=61.2%
...
0096 now_utc='21:41:26' ts='2023-04-17T21:41:24.756Z' delta=19.47minutes celsius=17.81 humidity=60.7%
...

@pdcastro
Copy link
Author

This gist was shared in support of an issue comment: home-assistant/core#88926 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment