-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtools.py
More file actions
executable file
·1196 lines (1030 loc) · 46.4 KB
/
tools.py
File metadata and controls
executable file
·1196 lines (1030 loc) · 46.4 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tools for the HEMS agent to interact with electricity pricing and appliance scheduling.
These tools are registered with the Claude Agent SDK.
"""
import json
import requests
import re
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, Any, Optional, List
# Google Calendar imports (optional - graceful degradation if not installed)
try:
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
CALENDAR_AVAILABLE = True
except ImportError:
CALENDAR_AVAILABLE = False
def get_electricity_prices(date: Optional[str] = None, use_cached_prices: bool = False) -> Dict[str, Any]:
"""
Retrieves day-ahead electricity prices for the specified date.
Args:
date: Date in YYYY-MM-DD format. If None, returns tomorrow's prices.
use_cached_prices: If True, loads from test_prices_reference.json (for benchmarking)
Returns:
Dictionary containing:
- date: The date for these prices
- unit: Price unit (EUR/kWh)
- resolution_minutes: Time resolution (15)
- timeslots: List of time labels (e.g., ["00:00", "00:15", ...])
- prices: List of 96 price values
"""
# Use cached reference prices if requested (for systematic evaluation)
if use_cached_prices:
cache_path = Path(__file__).parent / "test_prices_reference.json"
if cache_path.exists():
with open(cache_path, 'r') as f:
price_data = json.load(f)
print(f"✓ Loaded cached reference prices from {cache_path.name}")
print(f" Date: {price_data['date']}, {len(price_data['prices'])} price points")
return price_data
else:
print(f"⚠ Cached prices not found at {cache_path}, falling back to API fetch")
# Try to fetch from ENTSO-E API
try:
from entsoe_client import fetch_entsoe_prices
from config import ENTSOE_API_KEY, BIDDING_ZONE
if not ENTSOE_API_KEY:
print("⚠ ENTSO-E API key not configured. Please add ENTSOE_API_KEY to .env file.")
print(" Register at: https://transparency.entsoe.eu/")
print(" Falling back to mock data...")
else:
# If no date specified, fetch tomorrow's prices (day-ahead)
if date is None:
tomorrow = datetime.now() + timedelta(days=1)
date = tomorrow.strftime("%Y-%m-%d")
print(f"Attempting to fetch from ENTSO-E API for date: {date} (tomorrow - day-ahead)...")
else:
print(f"Attempting to fetch from ENTSO-E API for date: {date}...")
result = fetch_entsoe_prices(ENTSOE_API_KEY, date, BIDDING_ZONE)
print(f"✓ Successfully fetched {len(result['prices'])} price points from ENTSO-E")
return result
except Exception as e:
# Fallback to mock data if API fails
print(f"⚠ ENTSO-E API failed: {type(e).__name__}: {e}")
print(" Check your ENTSOE_API_KEY and BIDDING_ZONE in .env file.")
print(" Falling back to mock data...")
# Fallback: Read from mock file
data_path = Path(__file__).parent / "data" / "prices_sample.json"
with open(data_path, 'r') as f:
price_data = json.load(f)
return price_data
def get_weather_forecast(
location: str = "Vienna",
date: Optional[str] = None
) -> Dict[str, Any]:
"""
Retrieves hourly outdoor temperature forecast for the specified date.
Args:
location: City name (default: "Vienna")
date: Date in YYYY-MM-DD format. If None, returns tomorrow's forecast.
Returns:
Dictionary containing:
- date: The date for this forecast
- location: Location name
- temps_hourly: List of 24 hourly temperatures in Celsius
- temps_min: Minimum temperature
- temps_max: Maximum temperature
"""
# Vienna, Austria coordinates
LOCATIONS = {
"Vienna": {"lat": 48.2082, "lon": 16.3738}
}
if location not in LOCATIONS:
location = "Vienna"
coords = LOCATIONS[location]
# Parse target date
if date is None:
target_date = datetime.now() + timedelta(days=1)
else:
target_date = datetime.strptime(date, "%Y-%m-%d")
date_str = target_date.strftime("%Y-%m-%d")
try:
# Use Open-Meteo API (free, no API key required)
# Request 15-minute resolution to match electricity price resolution
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": coords["lat"],
"longitude": coords["lon"],
"minutely_15": "temperature_2m",
"start_date": date_str,
"end_date": date_str,
"timezone": "Europe/Vienna"
}
print(f"Fetching weather forecast (15-min resolution) for {location} on {date_str}...")
response = requests.get(url, params=params, timeout=30)
response.raise_for_status()
data = response.json()
# Extract 15-minute temperatures (should be 96 values for 24 hours)
temps_15min = data["minutely_15"]["temperature_2m"]
# Ensure we have 96 values (24h * 4 per hour)
if len(temps_15min) < 96:
temps_15min.extend([temps_15min[-1]] * (96 - len(temps_15min)))
temps_15min = temps_15min[:96]
print(f"✓ Fetched 96 temperature values (15-min): {min(temps_15min):.1f}°C - {max(temps_15min):.1f}°C")
return {
"date": date_str,
"location": location,
"temps_15min": temps_15min,
"temps_min": min(temps_15min),
"temps_max": max(temps_15min),
"unit": "Celsius",
"resolution_minutes": 15
}
except Exception as e:
print(f"⚠ Weather API failed: {type(e).__name__}: {e}")
print("Using fallback temperature data...")
# Fallback: typical winter day in Vienna (96 values at 15-min resolution)
# Pattern: gradual cooling at night, warming during day, cooling in evening
hourly_pattern = [
2, 1, 0, 0, -1, -1, 0, 1, # 00:00 - 07:00 (night, coldest)
3, 5, 7, 8, 9, 10, 10, 9, # 08:00 - 15:00 (day, warming)
7, 5, 4, 3, 2, 2, 2, 1 # 16:00 - 23:00 (evening, cooling)
]
# Expand to 15-minute resolution (repeat each hourly value 4 times)
fallback_temps = []
for temp in hourly_pattern:
fallback_temps.extend([temp] * 4)
return {
"date": date_str,
"location": location,
"temps_15min": fallback_temps,
"temps_min": min(fallback_temps),
"temps_max": max(fallback_temps),
"unit": "Celsius",
"resolution_minutes": 15,
"fallback": True
}
def calculate_heating_requirement(
outdoor_temps: List[float],
comfort_min: float = 20.0,
comfort_max: float = 22.0,
building_type: str = "old",
initial_temp: float = 19.0
) -> Dict[str, Any]:
"""
Calculates heating requirement using RC thermal model.
Args:
outdoor_temps: List of 96 outdoor temperatures (15-min resolution) in Celsius
comfort_min: Minimum comfort temperature in Celsius
comfort_max: Maximum comfort temperature in Celsius
building_type: Building type ('old', 'modern', 'passive')
initial_temp: Initial indoor temperature in Celsius
Returns:
Dictionary containing:
- building_type: The building type used
- comfort_range: [min, max] comfort temperatures
- outdoor_temp_range: [min, max] outdoor temperatures
- heating_slots_needed: Number of 15-min slots heating is needed
- heating_hours_needed: Total hours of heating needed
- slots_requiring_heat: List of slot indices where heating is needed
- estimated_total_power_kwh: Total energy needed in kWh
"""
# Building parameters from DERMOT validated model
BUILDING_PARAMS = {
# "old": {
# "wall_resistance": 1.0, # R-value (m²K/W), U = 1.0 W/m²K
# "window_resistance": 0.2, # U = 5.0 W/m²K
# "roof_resistance": 1.5, # U = 0.67 W/m²K
# "floor_resistance": 1.0, # U = 1.0 W/m²K
# "wall_capacitance": 2000000, # 2.0 MJ/K
# "air_capacitance": 210000, # 0.21 MJ/K (DERMOT validated)
# "wall_area": 100, # m²
# "window_area": 20, # m²
# "roof_area": 70, # m²
# "floor_area": 70, # m²
# "infiltration_rate": 1.5 # ACH
# },
# "modern": {
# "wall_resistance": 3.5,
# "window_resistance": 0.7,
# "roof_resistance": 5.0,
# "floor_resistance": 4.0,
# "wall_capacitance": 1500000,
# "air_capacitance": 210000,
# "wall_area": 100,
# "window_area": 20,
# "roof_area": 70,
# "floor_area": 70,
# "infiltration_rate": 0.5
# },
"passive": {
"wall_resistance": 8.0,
"window_resistance": 1.5,
"roof_resistance": 10.0,
"floor_resistance": 8.0,
"wall_capacitance": 2500000,
"air_capacitance": 210000,
"wall_area": 100,
"window_area": 20,
"roof_area": 70,
"floor_area": 70,
"infiltration_rate": 0.3
}
}
params = BUILDING_PARAMS.get(building_type, BUILDING_PARAMS["passive"])
# Physical constants
AIR_DENSITY = 1.2 # kg/m³
SPECIFIC_HEAT_AIR = 1005 # J/(kg·K)
HEAT_PUMP_POWER = 6.0 # kW (typical heat pump capacity)
def calculate_heat_loss(indoor_temp: float, outdoor_temp: float) -> float:
"""Calculate heat loss through building envelope in Watts."""
temp_diff = indoor_temp - outdoor_temp
# Conduction through each component: Q = ΔT / R * A
wall_loss = (temp_diff / params["wall_resistance"]) * params["wall_area"]
window_loss = (temp_diff / params["window_resistance"]) * params["window_area"]
roof_loss = (temp_diff / params["roof_resistance"]) * params["roof_area"]
floor_loss = (temp_diff / params["floor_resistance"]) * params["floor_area"]
# Infiltration: Q = ACH * V * ρ * cp * ΔT / 3600
# Building volume from DERMOT: (wallArea + roofArea) * ceiling_height
building_volume = (params["wall_area"] + params["roof_area"]) * 2.5 # m³
infiltration_loss = (
params["infiltration_rate"] *
building_volume *
AIR_DENSITY *
SPECIFIC_HEAT_AIR *
temp_diff
) / 3600
total_loss = wall_loss + window_loss + roof_loss + floor_loss + infiltration_loss
return total_loss # Watts
def calculate_next_temp(current_indoor: float, outdoor: float, heating_power_kw: float) -> float:
"""Calculate next temperature using RC model."""
total_capacitance = params["wall_capacitance"] + params["air_capacitance"] # J/K
heat_loss = calculate_heat_loss(current_indoor, outdoor) # Watts
heating_power_w = heating_power_kw * 1000 # Convert kW to W
# Energy balance: ΔT = (Q_heating - Q_loss) * Δt / C
time_step = 15 * 60 # 15 minutes in seconds
net_heat_flow = heating_power_w - heat_loss
temp_change = (net_heat_flow * time_step) / total_capacitance
return current_indoor + temp_change
# Simulate 24-hour period to determine heating needs
indoor_temp = initial_temp
slots_requiring_heat = []
total_energy_kwh = 0.0
print(f"Simulating thermal dynamics ({building_type} building)...")
print(f" Comfort range: {comfort_min}°C - {comfort_max}°C")
print(f" Outdoor range: {min(outdoor_temps):.1f}°C - {max(outdoor_temps):.1f}°C")
for slot in range(96):
outdoor = outdoor_temps[slot]
# Check if heating is needed to maintain comfort
# Project temperature without heating
temp_without_heating = calculate_next_temp(indoor_temp, outdoor, 0.0)
# If temperature would drop below comfort minimum, heating is needed
if temp_without_heating < comfort_min:
slots_requiring_heat.append(slot)
# Apply heating to bring temperature to comfort_max
indoor_temp = calculate_next_temp(indoor_temp, outdoor, HEAT_PUMP_POWER)
total_energy_kwh += HEAT_PUMP_POWER * 0.25 # 15 min = 0.25 hours
# Cap at comfort_max
if indoor_temp > comfort_max:
indoor_temp = comfort_max
else:
# No heating, temperature evolves naturally
indoor_temp = temp_without_heating
# Cap at comfort_max (from solar gains, etc.)
if indoor_temp > comfort_max:
indoor_temp = comfort_max
heating_hours = len(slots_requiring_heat) * 0.25
print(f" ✓ {len(slots_requiring_heat)} slots need heating ({heating_hours:.1f} hours)")
print(f" ✓ Estimated energy: {total_energy_kwh:.2f} kWh")
return {
"building_type": building_type,
"comfort_range": [comfort_min, comfort_max],
"outdoor_temp_range": [min(outdoor_temps), max(outdoor_temps)],
"heating_slots_needed": len(slots_requiring_heat),
"heating_hours_needed": heating_hours,
"slots_requiring_heat": slots_requiring_heat,
"estimated_total_power_kwh": total_energy_kwh,
"heat_pump_power_kw": HEAT_PUMP_POWER,
"building_params": {
"type": building_type,
"total_u_value_avg": round(
(params["wall_area"] / params["wall_resistance"] +
params["window_area"] / params["window_resistance"] +
params["roof_area"] / params["roof_resistance"] +
params["floor_area"] / params["floor_resistance"]) /
(params["wall_area"] + params["window_area"] +
params["roof_area"] + params["floor_area"]), 2
),
"infiltration_rate": params["infiltration_rate"]
}
}
def schedule_appliance(
appliance_id: str,
start_slot: int,
duration_slots: int,
user_info: Optional[str] = None
) -> Dict[str, Any]:
"""
Schedules an appliance to run starting at the specified timeslot.
Optionally sends API commands to actual devices if configured.
Args:
appliance_id: Identifier for the appliance (e.g., "washing_machine")
start_slot: Starting timeslot index (0-95, where 0 = 00:00)
duration_slots: Number of 15-minute slots the appliance will run
user_info: Optional context about why this schedule was chosen
Returns:
Dictionary containing:
- success: Boolean indicating if scheduling succeeded
- appliance_id: The appliance that was scheduled
- start_slot: Starting timeslot index
- start_time: Human-readable start time
- end_slot: Ending timeslot index
- end_time: Human-readable end time
- duration_minutes: Total duration in minutes
- schedule: 96-element binary array (0 = off, 1 = on for each 15-min slot)
- message: Confirmation message
- api_response: API response if device control was attempted
"""
from config import AVAILABLE_APPLIANCES
# Validate inputs
if not 0 <= start_slot < 96:
return {
"success": False,
"error": f"Invalid start_slot {start_slot}. Must be between 0 and 95."
}
end_slot = start_slot + duration_slots - 1
if end_slot >= 96:
return {
"success": False,
"error": f"Schedule exceeds 24-hour period. Start slot {start_slot} + duration {duration_slots} exceeds slot 95."
}
# Convert slot indices to human-readable times
def slot_to_time(slot: int) -> str:
hours = (slot * 15) // 60
minutes = (slot * 15) % 60
return f"{hours:02d}:{minutes:02d}"
start_time = slot_to_time(start_slot)
end_time = slot_to_time(end_slot + 1) # +1 because end_slot is inclusive
duration_minutes = duration_slots * 15
# Build schedule record
schedule_record = {
"timestamp": datetime.now().isoformat(),
"appliance_id": appliance_id,
"start_slot": start_slot,
"start_time": start_time,
"end_slot": end_slot,
"end_time": end_time,
"duration_slots": duration_slots,
"duration_minutes": duration_minutes,
"user_info": user_info
}
# Save schedule to file for record keeping
schedule_path = Path(__file__).parent / "data" / "schedules.json"
schedules = []
if schedule_path.exists():
with open(schedule_path, 'r') as f:
schedules = json.load(f)
schedules.append(schedule_record)
with open(schedule_path, 'w') as f:
json.dump(schedules, f, indent=2)
# Attempt device control via API if configured
api_response = None
if appliance_id in AVAILABLE_APPLIANCES:
appliance_config = AVAILABLE_APPLIANCES[appliance_id]
api_config = appliance_config.get("api_config", {})
if api_config.get("enabled", False):
try:
# Prepare template variables for substitution
template_vars = {
"start_time": start_time,
"end_time": end_time,
"start_slot": start_slot,
"end_slot": end_slot,
"duration_minutes": duration_minutes,
"power_rating_kw": appliance_config.get("power_rating_kw", 0),
"comfort_max": appliance_config.get("comfort_max", 22),
"comfort_min": appliance_config.get("comfort_min", 20)
}
# Build payload by substituting template variables
payload = {}
for key, value in api_config["payload_template"].items():
if isinstance(value, str) and "{" in value:
# Replace template variable
for var_name, var_value in template_vars.items():
value = value.replace(f"{{{var_name}}}", str(var_value))
payload[key] = value
# Build headers by substituting variables
headers = {}
for key, value in api_config["headers"].items():
if isinstance(value, str) and "{" in value:
# Replace environment variables or tokens
# Note: Users should replace {HASS_TOKEN} with actual token in config
headers[key] = value
else:
headers[key] = value
# Send API request
print(f" → Sending API command to {appliance_id}...")
response = requests.request(
method=api_config["method"],
url=api_config["endpoint"],
headers=headers,
json=payload,
timeout=10
)
api_response = {
"status_code": response.status_code,
"success": response.status_code in [200, 201, 202],
"response": response.text[:200] # Truncate for brevity
}
if api_response["success"]:
print(f" ✓ API command successful (status {response.status_code})")
else:
print(f" ⚠ API command failed (status {response.status_code})")
except requests.exceptions.RequestException as e:
api_response = {
"success": False,
"error": str(e)
}
print(f" ✗ API request failed: {e}")
except Exception as e:
api_response = {
"success": False,
"error": f"Unexpected error: {e}"
}
print(f" ✗ Unexpected error during API call: {e}")
# Generate 96-element binary schedule array (0 = off, 1 = on)
schedule_array = [0] * 96
for slot in range(start_slot, end_slot + 1):
schedule_array[slot] = 1
result = {
"success": True,
"appliance_id": appliance_id,
"start_slot": start_slot,
"start_time": start_time,
"end_slot": end_slot,
"end_time": end_time,
"duration_minutes": duration_minutes,
"schedule": schedule_array, # The actual 96-element binary schedule
"message": f"{appliance_id} scheduled to run from {start_time} to {end_time} ({duration_minutes} minutes)"
}
if api_response:
result["api_response"] = api_response
return result
def get_calendar_ev_constraint(hours_ahead: int = 24) -> Optional[Dict[str, Any]]:
"""
Fetch calendar events and extract EV charging deadline using LLM reasoning.
Args:
hours_ahead: How many hours ahead to fetch events
Returns:
Dict with EV constraint if found, None otherwise:
{
"deadline_time": "07:30",
"deadline_slot": 30,
"event_title": "Work",
"event_time": "07:30",
"reasoning": "User has work at 7:30am, EV must be charged before departure"
}
"""
if not CALENDAR_AVAILABLE:
print(" ℹ Google Calendar not configured - using default EV deadline")
return None
# Check if credentials exist
credentials_path = Path(__file__).parent / 'credentials.json'
token_path = Path(__file__).parent / 'token.json'
if not credentials_path.exists():
print(" ℹ Google Calendar credentials not found - using default EV deadline")
return None
try:
# Authenticate
creds = None
if token_path.exists():
creds = Credentials.from_authorized_user_file(str(token_path),
['https://www.googleapis.com/auth/calendar.readonly'])
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
# Silent skip if OAuth not completed
return None
with open(token_path, 'w') as token:
token.write(creds.to_json())
# Build calendar service
service = build('calendar', 'v3', credentials=creds)
# Fetch events
now = datetime.utcnow()
time_min = now.isoformat() + 'Z'
time_max = (now + timedelta(hours=hours_ahead)).isoformat() + 'Z'
events_result = service.events().list(
calendarId='primary',
timeMin=time_min,
timeMax=time_max,
maxResults=10,
singleEvents=True,
orderBy='startTime'
).execute()
events = events_result.get('items', [])
if not events:
return None
# Format events for LLM (title + time only)
events_text = "Calendar events:\n"
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
summary = event.get('summary', 'Untitled')
events_text += f"- {summary} at {start}\n"
# Use LLM to extract EV constraint
import os
from config import CEREBRAS_API_KEY, CEREBRAS_MODEL, TEMPERATURE
# Use model override if set (from orchestrator)
model = os.environ.get('CEREBRAS_MODEL_OVERRIDE', CEREBRAS_MODEL)
system_prompt = """You are analyzing calendar events to determine when an electric vehicle (EV) must be fully charged.
Rules:
1. If there are morning events (before 10am), the EV should be charged before the earliest event
2. Use a 30-minute buffer before the event time for the charging deadline
3. Only respond if there's a clear need for EV charging
4. Respond ONLY with valid JSON, no additional text
Output format (JSON only):
{
"needs_charging": true/false,
"deadline_time": "HH:MM",
"event_title": "Event name",
"event_time": "HH:MM",
"reasoning": "Brief explanation"
}"""
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": events_text}
]
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {CEREBRAS_API_KEY}"
}
payload = {
"model": model,
"messages": messages,
"temperature": TEMPERATURE,
"max_tokens": 200
}
response = requests.post(
"https://api.cerebras.ai/v1/chat/completions",
headers=headers,
json=payload,
timeout=30
)
response.raise_for_status()
result = response.json()
llm_response = result['choices'][0]['message']['content'].strip()
# Parse JSON response
# Remove markdown code blocks if present
if '```json' in llm_response:
llm_response = llm_response.split('```json')[1].split('```')[0].strip()
elif '```' in llm_response:
llm_response = llm_response.split('```')[1].split('```')[0].strip()
constraint_data = json.loads(llm_response)
if not constraint_data.get('needs_charging', False):
return None
# Convert deadline time to slot index
deadline_time = constraint_data['deadline_time']
hours, minutes = map(int, deadline_time.split(':'))
deadline_slot = (hours * 60 + minutes) // 15
return {
"deadline_time": deadline_time,
"deadline_slot": deadline_slot,
"event_title": constraint_data['event_title'],
"event_time": constraint_data['event_time'],
"reasoning": constraint_data['reasoning']
}
except Exception as e:
print(f" ⚠ Calendar constraint extraction failed: {e}")
return None
def calculate_window_sums(prices: List[float], window_size: int, start_slot: int = 0, end_slot: Optional[int] = None) -> Dict[str, Any]:
"""
Calculates sums for all consecutive price windows of a given size.
This enables agents to find optimal scheduling windows without arithmetic errors.
Args:
prices: List of 96 electricity prices (EUR/MWh)
window_size: Number of consecutive slots per window
start_slot: Starting slot index (default 0)
end_slot: Ending slot index (default: len(prices) - window_size)
Returns:
Dictionary containing:
- window_sums: List of sums for each window
- window_count: Number of windows calculated
- min_window_index: Index of window with minimum sum
- min_window_sum: The minimum sum value
- success: Boolean indicating if calculation succeeded
"""
try:
if not prices or len(prices) < window_size:
return {
"success": False,
"error": f"Invalid input: need at least {window_size} prices, got {len(prices)}"
}
if window_size <= 0:
return {
"success": False,
"error": f"Invalid window_size: {window_size}"
}
# Default end_slot to allow last valid window
if end_slot is None:
end_slot = len(prices) - window_size
# Calculate sums for all consecutive windows
window_sums = []
for i in range(start_slot, end_slot + 1):
window = prices[i:i+window_size]
if len(window) == window_size:
window_sums.append(round(sum(window), 4))
if not window_sums:
return {
"success": False,
"error": "No valid windows to calculate"
}
# Find minimum
min_sum = min(window_sums)
min_index = window_sums.index(min_sum) + start_slot
return {
"success": True,
"window_sums": window_sums,
"window_count": len(window_sums),
"min_window_index": min_index,
"min_window_sum": min_sum,
"window_size": window_size,
"start_slot": start_slot
}
except Exception as e:
return {
"success": False,
"error": f"Calculation error: {str(e)}"
}
def call_appliance_agent(
agent_name: str,
prices_data: Dict[str, Any],
user_request: str
) -> Dict[str, Any]:
"""
Delegates to a specialist appliance agent for scheduling optimization.
Args:
agent_name: Name of the appliance agent (e.g., "washing_machine_agent")
prices_data: Electricity price data from get_electricity_prices()
user_request: User's scheduling request with constraints
Returns:
Dictionary containing:
- recommended_slot: Optimal start slot
- duration_slots: Duration in 15-min slots
- cost: Estimated cost in EUR
- reasoning: Agent's explanation
- agent_response: Full agent response text
"""
import os
from config import AVAILABLE_APPLIANCES, CEREBRAS_API_KEY, CEREBRAS_MODEL, TEMPERATURE
# Use model override if set (from orchestrator)
model = os.environ.get('CEREBRAS_MODEL_OVERRIDE', CEREBRAS_MODEL)
# Validate agent exists
appliance_id = agent_name.replace("_agent", "")
if appliance_id not in AVAILABLE_APPLIANCES:
return {
"error": f"Agent '{agent_name}' not found. Available: {list(AVAILABLE_APPLIANCES.keys())}"
}
# Load agent's system prompt
agent_file = AVAILABLE_APPLIANCES[appliance_id]["agent_file"]
prompt_path = Path(__file__).parent / agent_file
if not prompt_path.exists():
return {
"error": f"Agent prompt file not found: {agent_file}"
}
with open(prompt_path, 'r') as f:
system_prompt = f.read()
# Format price data for agent
prices_text = (
f"Date: {prices_data['date']}\n"
f"Unit: {prices_data['unit']}\n"
f"Resolution: {prices_data['resolution_minutes']} minutes\n"
f"Prices (96 slots, slot 0=00:00):\n{prices_data['prices']}\n"
f"Min: {min(prices_data['prices']):.4f} at slot {prices_data['prices'].index(min(prices_data['prices']))}\n"
f"Max: {max(prices_data['prices']):.4f} at slot {prices_data['prices'].index(max(prices_data['prices']))}"
)
# Special handling for heat pump agent - needs weather and thermal model
additional_context = ""
if appliance_id == "heat_pump":
print(f" → Heat pump agent requires weather and thermal analysis...")
# Get weather forecast
weather_data = get_weather_forecast(location="Vienna", date=prices_data['date'])
# Get building parameters from config
building_type = AVAILABLE_APPLIANCES[appliance_id].get("building_type", "old")
comfort_min = AVAILABLE_APPLIANCES[appliance_id].get("comfort_min", 20.0)
comfort_max = AVAILABLE_APPLIANCES[appliance_id].get("comfort_max", 22.0)
# Calculate heating requirements using thermal model
thermal_data = calculate_heating_requirement(
outdoor_temps=weather_data['temps_15min'],
comfort_min=comfort_min,
comfort_max=comfort_max,
building_type=building_type
)
# Format thermal and weather data for agent
additional_context = (
f"\n\n{'='*60}\n"
f"WEATHER FORECAST:\n"
f"{'='*60}\n"
f"Location: {weather_data['location']}\n"
f"Date: {weather_data['date']}\n"
f"Temperature range: {weather_data['temps_min']:.1f}°C - {weather_data['temps_max']:.1f}°C\n"
f"Outdoor temperatures (96 slots, 15-min resolution):\n{weather_data['temps_15min']}\n"
f"\n{'='*60}\n"
f"THERMAL MODEL ANALYSIS:\n"
f"{'='*60}\n"
f"Building type: {thermal_data['building_type']}\n"
f"Comfort range: {thermal_data['comfort_range'][0]}°C - {thermal_data['comfort_range'][1]}°C\n"
f"Heating slots needed: {thermal_data['heating_slots_needed']} slots ({thermal_data['heating_hours_needed']:.1f} hours)\n"
f"Estimated energy requirement: {thermal_data['estimated_total_power_kwh']:.2f} kWh\n"
f"Heat pump power: {thermal_data['heat_pump_power_kw']} kW\n"
f"\nSlots requiring heating (slot indices):\n{thermal_data['slots_requiring_heat']}\n"
f"\nBuilding parameters:\n"
f" - Average U-value: {thermal_data['building_params']['total_u_value_avg']} W/m²K\n"
f" - Infiltration rate: {thermal_data['building_params']['infiltration_rate']} ACH\n"
)
# Call LLM with agent's system prompt
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {CEREBRAS_API_KEY}"
}
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"{user_request}\n\nElectricity Price Data:\n{prices_text}{additional_context}"}
]
# All appliances need 15k tokens to show all window calculations
# This ensures agents don't get truncated mid-calculation
max_tokens = 15000
# Define window calculation tool for agent use
tools = [
{
"type": "function",
"function": {
"name": "calculate_window_sums",
"description": "Calculates sums for all consecutive price windows. Returns array of sums and identifies minimum. Use this once at the start to get all window sums.",
"parameters": {
"type": "object",
"properties": {
"prices": {
"type": "array",
"items": {"type": "number"},
"description": "List of 96 electricity prices in EUR/MWh"
},
"window_size": {
"type": "integer",
"description": "Number of consecutive slots per window (e.g., 8 for 2-hour cycle)"
},
"start_slot": {
"type": "integer",
"description": "Starting slot index (default 0)"
},
"end_slot": {
"type": "integer",
"description": "Ending slot index (optional, defaults to last valid window)"
}
},
"required": ["prices", "window_size"]
}
}
}
]
payload = {
"model": model,
"messages": messages,
"temperature": TEMPERATURE,
"max_tokens": max_tokens,
"tools": tools,
"stream": False
}
# Make API call with tool support
response = requests.post(
"https://api.cerebras.ai/v1/chat/completions",
headers=headers,
json=payload,
timeout=60
)
response.raise_for_status()
result = response.json()
# Handle tool calls if present (with loop limit to prevent infinite calls)
message = result['choices'][0]['message']
tool_call_rounds = 0
max_tool_rounds = 2
# If agent requested tool calls, execute them and continue
while message.get('tool_calls') and tool_call_rounds < max_tool_rounds:
tool_call_rounds += 1
print(f" → Agent requested {len(message['tool_calls'])} tool call(s) (round {tool_call_rounds})")
# Add assistant message with tool calls to conversation
messages.append(message)
# Execute each tool call
for tool_call in message['tool_calls']:
function_name = tool_call['function']['name']
function_args = json.loads(tool_call['function']['arguments'])
if function_name == "calculate_window_sums":
tool_result = calculate_window_sums(**function_args)
result_content = json.dumps(tool_result)
if tool_result.get('success'):
print(f" calculate_window_sums(window_size={function_args['window_size']}) → {tool_result['window_count']} windows, min at slot {tool_result['min_window_index']} = {tool_result['min_window_sum']}")
else:
print(f" calculate_window_sums() ERROR: {tool_result.get('error')}")
else:
result_content = json.dumps({"error": f"Unknown tool: {function_name}"})
# Add tool result to messages
messages.append({
"role": "tool",
"tool_call_id": tool_call['id'],
"content": result_content
})
# Re-call LLM with tool results
payload['messages'] = messages
response = requests.post(
"https://api.cerebras.ai/v1/chat/completions",
headers=headers,
json=payload,
timeout=60
)
response.raise_for_status()
result = response.json()
message = result['choices'][0]['message']
print(f" Agent response after tool call received")
agent_response = message.get('content', '')
if not agent_response:
# If agent didn't provide text response but we have tool results, construct response
if tool_call_rounds > 0:
# Agent called the tool but didn't formulate final text response
# Extract the tool result from conversation history and use it
print(f" ⚠ Agent didn't provide final text after {tool_call_rounds} tool calls")
print(f" → Constructing response from tool results")
# Find the last tool result in messages
for msg in reversed(messages):