trend¶
Trends of tile values: rising or falling over up to three hours.
The board keeps a short history per source itself, so trends work with any data source and cost no extra request: at most one sample every SAMPLE_INTERVAL_S, dropped once older than SPAN_S. It does so for every number tile, so an arrow can appear as soon as a tile’s trend is switched on. A trend compares the current value with the oldest sample: in steady operation one from three hours ago, after a restart the oldest there is, so a trend shows up within half an hour.
The history is a dict {source ID: [[time s, value], …]}, oldest first, stored as JSON on flash between wakes (HISTORY_FILE); it only changes when a sample is added, so the file is written at most every half hour.
Attributes¶
Functions¶
|
Add the current values of |
|
Return UP, DOWN or None (steady, or no older value yet). |
|
Source IDs to keep a history for: those of number tiles, each once. |
|
Set "arrow" (UP, DOWN or None) on layout tiles that have a "trend". |
|
Return the saved history, or an empty one. |
|
Store the history; a power cut leaves the previous file intact. |
Module Contents¶
- trend.HISTORY_FILE = 'trend.json'¶
- trend.SAMPLE_INTERVAL_S = 1800¶
- trend.SPAN_S = 10800¶
- trend.SLACK_S = 60¶
- trend.UP = 'up'¶
- trend.DOWN = 'down'¶
- trend.record(history, readings, sources, now_s)¶
Add the current values of
sourcestohistory.- Parameters:
history – The previous history, or None.
readings – Current readings {source ID: {“value”: …}}.
sources – Source IDs to keep a history for.
now_s – Current time in seconds.
- Returns:
The new history; sources no longer asked for are dropped.
- trend.direction(samples, value, now_s, threshold)¶
Return UP, DOWN or None (steady, or no older value yet).
- Parameters:
samples – The history of one source (see
record).value – The current value.
now_s – Current time in seconds.
threshold – Smallest change that counts as a trend.
- trend.sources(tile_config)¶
Source IDs to keep a history for: those of number tiles, each once.
- trend.annotate(layout_tiles, history, readings, now_s)¶
Set “arrow” (UP, DOWN or None) on layout tiles that have a “trend”.
- Parameters:
layout_tiles – Tiles as tiles.to_layout returns them; changed in place.
history – The history (see
record).readings – Current readings {source ID: {“value”: …}}.
now_s – Current time in seconds.
- trend.load(path=HISTORY_FILE)¶
Return the saved history, or an empty one.
- trend.save(history, path=HISTORY_FILE)¶
Store the history; a power cut leaves the previous file intact.