Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions apps/hassgraph/hass_graph.star
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ MAX_TIME_PERIOD = 24

TIME_FORMAT = "2006-01-02T15:04:05Z"

def parse_state_value(state):
if state == None or state in ("unavailable", "unknown", "none", ""):
return None
s = state.strip()
if not s:
return None
if s.startswith("-"):
s = s[1:]
if not s:
return None
parts = s.split(".")
if len(parts) not in [1, 2]:
return None
for part in parts:
if not part.isdigit():
return None
return float(state)

def main(config):
timezone = None
location = config.get("location")
Expand Down Expand Up @@ -91,9 +109,9 @@ def main(config):
if len(parts) == 2:
min_val = float(parts[0])
max_val = float(parts[1])
if current_value == "unavailable" or current_value == "unknown":
val = parse_state_value(current_value)
if val == None:
return []
val = float(current_value)
if val < min_val or val > max_val:
return []
stats = calc_stats(timezone, data)
Expand All @@ -105,9 +123,10 @@ def calculate_hourly_average(config, data):
if config.bool("use_raw_data"):
points = []
for i, entry in enumerate(data):
if entry["state"] == "unavailable" or entry["state"] == "unknown":
value = parse_state_value(entry["state"])
if value == None:
continue
points.append((i, float(entry["state"])))
points.append((i, value))
return points

hourly_averages = {}
Expand All @@ -117,12 +136,12 @@ def calculate_hourly_average(config, data):
index = 0

for entry in data:
if entry["state"] == "unavailable" or entry["state"] == "unknown":
value = parse_state_value(entry["state"])
if value == None:
continue

timestamp = entry["last_changed"]
hour = int(timestamp.split("T")[1].split(":")[0])
value = float(entry["state"])

if hour != current_hour:
if current_hour != None:
Expand Down Expand Up @@ -152,10 +171,9 @@ def calc_stats(timezone, data):
count = 0

for entry in data:
if entry["state"] == "unavailable" or entry["state"] == "unknown":
value = parse_state_value(entry["state"])
if value == None:
continue

value = float(entry["state"])
total_value += value
count += 1
if value < lowest_value:
Expand Down Expand Up @@ -252,7 +270,10 @@ def render_graph_column(config, current_value, points, unit, label):
))
if (config.str("round_to") in ["0", "1", "2"]):
decimal = int(config.get("round_to"))
val = round(float(current_value), decimal)
val = parse_state_value(current_value)
if val == None:
val = 0
val = round(val, decimal)
current_value = str(int(val)) if val == int(val) else str(val)
Comment on lines +273 to 277

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Converting a non-numeric state (like "unavailable") to 0 when rounding is enabled is misleading. Skip rounding and preserve the original string if parse_state_value returns None.

Suggested change
val = parse_state_value(current_value)
if val == None:
val = 0
val = round(val, decimal)
current_value = str(int(val)) if val == int(val) else str(val)
val = parse_state_value(current_value)
if val != None:
val = round(val, decimal)
current_value = str(int(val)) if val == int(val) else str(val)

children.append(render.Text(content = current_value + unit, font = "6x13"))
align = "space_between" if label else "end"
Expand Down
2 changes: 1 addition & 1 deletion apps/hassgraph/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ tags:
- sports
- utility
published: '2024-06-19T15:18:22Z'
updated: '2026-07-06T22:24:02Z'
updated: '2026-07-10T15:44:48Z'