From 145cc4967e1066673ed6c70159735676991009ce Mon Sep 17 00:00:00 2001 From: davidusb-geek Date: Sat, 4 Jan 2025 11:47:15 +0100 Subject: [PATCH 1/2] Debugging issue of list index out of range, solution proposed --- src/emhass/utils.py | 8 ++++- tests/test_command_line_utils.py | 3 +- tests/test_utils.py | 60 ++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/emhass/utils.py b/src/emhass/utils.py index 99674b23..cea50bfa 100644 --- a/src/emhass/utils.py +++ b/src/emhass/utils.py @@ -181,7 +181,13 @@ def update_params_with_ha_config( if 'unit_system' not in ha_config.keys(): ha_config['unit_system'] = {'temperature': '°C'} - for k in range(params["optim_conf"]["number_of_deferrable_loads"]): + number_of_deferrable_loads = params["optim_conf"]["number_of_deferrable_loads"] + if 'num_def_loads' in params['passed_data'].keys(): + number_of_deferrable_loads = params['passed_data']['num_def_loads'] + if 'number_of_deferrable_loads' in params['passed_data'].keys(): + number_of_deferrable_loads = params['passed_data']['number_of_deferrable_loads'] + + for k in range(number_of_deferrable_loads): params['passed_data']['custom_predicted_temperature_id'][k].update( {"unit_of_measurement": ha_config['unit_system']['temperature']} ) diff --git a/tests/test_command_line_utils.py b/tests/test_command_line_utils.py index d9341aa0..0368ec69 100644 --- a/tests/test_command_line_utils.py +++ b/tests/test_command_line_utils.py @@ -35,8 +35,7 @@ emhass_conf["defaults_path"] = emhass_conf["root_path"] / "data/config_defaults.json" emhass_conf["associations_path"] = emhass_conf["root_path"] / "data/associations.csv" - -# create loggerW +# create logger logger, ch = utils.get_logger(__name__, emhass_conf, save_to_file=False) diff --git a/tests/test_utils.py b/tests/test_utils.py index 40d7a5e8..cfdd7e8a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -459,16 +459,62 @@ def test_update_params_with_ha_config(self): 'currency': 'USD', 'unit_system': {'temperature': '°F'} } - params_json = utils.update_params_with_ha_config( + params_with_ha_config_json = utils.update_params_with_ha_config( params, ha_config, ) - params = json.loads(params_json) - self.assertTrue(params["passed_data"]["custom_predicted_temperature_id"][0]["unit_of_measurement"] == "°F") - self.assertTrue(params["passed_data"]["custom_predicted_temperature_id"][1]["unit_of_measurement"] == "°F") - self.assertTrue(params["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$') - self.assertTrue(params["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh') - self.assertTrue(params["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh') + params_with_ha_config = json.loads(params_with_ha_config_json) + self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][0]["unit_of_measurement"] == "°F") + self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][1]["unit_of_measurement"] == "°F") + self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$') + self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh') + self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh') + + def test_update_params_with_ha_config_special_case(self): + # Test special passed runtime params + runtimeparams = { + 'prediction_horizon': 28, + 'pv_power_forecast': [523, 873, 1059, 1195, 1291, 1352, 1366, 1327, 1254, 1150, 1004, 813, 589, 372, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 228, 301, 363, 407, 438, 456, 458, 443, 417, 381, 332, 269, 195, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'num_def_loads': 2, 'P_deferrable_nom': [0, 0], 'def_total_hours': [0, 0], 'treat_def_as_semi_cont': [1, 1], 'set_def_constant': [1, 1], + 'def_start_timestep': [0, 0], 'def_end_timestep': [0, 0], 'soc_init': 0.64, 'soc_final': 0.9, + 'load_cost_forecast': [0.2751, 0.2751, 0.2729, 0.2729, 0.2748, 0.2748, 0.2746, 0.2746, 0.2815, 0.2815, 0.2841, 0.2841, 0.282, 0.282, 0.288, 0.288, 0.29, 0.29, 0.2841, 0.2841, 0.2747, 0.2747, 0.2677, 0.2677, 0.2628, 0.2628, 0.2532, 0.2532], + 'prod_price_forecast': [0.1213, 0.1213, 0.1192, 0.1192, 0.121, 0.121, 0.1208, 0.1208, 0.1274, 0.1274, 0.1298, 0.1298, 0.1278, 0.1278, 0.1335, 0.1335, 0.1353, 0.1353, 0.1298, 0.1298, 0.1209, 0.1209, 0.1143, 0.1143, 0.1097, 0.1097, 0.1007, 0.1007], + 'alpha': 1, 'beta': 0, + 'load_power_forecast': [399, 300, 400, 600, 300, 200, 200, 200, 200, 300, 300, 200, 400, 200, 200, 400, 400, 400, 300, 300, 300, 600, 800, 500, 400, 400, 500, 500, 2400, 2300, 2400, 2400, 2300, 2400, 2400, 2400, 2300, 2400, 2400, 200, 200, 300, 300, 300, 300, 300, 300, 300]} + params_ = json.loads(self.params_json) + params_['passed_data'].update(runtimeparams) + + runtimeparams_json = json.dumps(runtimeparams) + params_json = json.dumps(params_) + + retrieve_hass_conf, optim_conf, plant_conf = utils.get_yaml_parse( + params_json, logger + ) + set_type = "dayahead-optim" + params, retrieve_hass_conf, optim_conf, plant_conf = utils.treat_runtimeparams( + runtimeparams_json, + params_json, + retrieve_hass_conf, + optim_conf, + plant_conf, + set_type, + logger, + emhass_conf, + ) + ha_config = { + 'currency': 'USD', + 'unit_system': {'temperature': '°F'} + } + params_with_ha_config_json = utils.update_params_with_ha_config( + params, + ha_config, + ) + params_with_ha_config = json.loads(params_with_ha_config_json) + self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][0]["unit_of_measurement"] == "°F") + self.assertTrue(params_with_ha_config["passed_data"]["custom_predicted_temperature_id"][1]["unit_of_measurement"] == "°F") + self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$') + self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh') + self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh') def test_build_secrets(self): # Test the build_secrets defaults from get_test_params() From fea14881ed452451382cd89db151a906ce807069 Mon Sep 17 00:00:00 2001 From: davidusb-geek Date: Sat, 4 Jan 2025 16:58:38 +0100 Subject: [PATCH 2/2] Added test to check when number of deferrable loads is 0 --- tests/test_utils.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index cfdd7e8a..d9dae94d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -515,6 +515,46 @@ def test_update_params_with_ha_config_special_case(self): self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$') self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh') self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh') + # Test with 0 deferrable loads + runtimeparams = { + 'prediction_horizon': 28, + 'pv_power_forecast': [523, 873, 1059, 1195, 1291, 1352, 1366, 1327, 1254, 1150, 1004, 813, 589, 372, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 228, 301, 363, 407, 438, 456, 458, 443, 417, 381, 332, 269, 195, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + 'num_def_loads': 0, + 'def_start_timestep': [0, 0], 'def_end_timestep': [0, 0], 'soc_init': 0.64, 'soc_final': 0.9, + 'load_cost_forecast': [0.2751, 0.2751, 0.2729, 0.2729, 0.2748, 0.2748, 0.2746, 0.2746, 0.2815, 0.2815, 0.2841, 0.2841, 0.282, 0.282, 0.288, 0.288, 0.29, 0.29, 0.2841, 0.2841, 0.2747, 0.2747, 0.2677, 0.2677, 0.2628, 0.2628, 0.2532, 0.2532], + 'prod_price_forecast': [0.1213, 0.1213, 0.1192, 0.1192, 0.121, 0.121, 0.1208, 0.1208, 0.1274, 0.1274, 0.1298, 0.1298, 0.1278, 0.1278, 0.1335, 0.1335, 0.1353, 0.1353, 0.1298, 0.1298, 0.1209, 0.1209, 0.1143, 0.1143, 0.1097, 0.1097, 0.1007, 0.1007], + 'alpha': 1, 'beta': 0, + 'load_power_forecast': [399, 300, 400, 600, 300, 200, 200, 200, 200, 300, 300, 200, 400, 200, 200, 400, 400, 400, 300, 300, 300, 600, 800, 500, 400, 400, 500, 500, 2400, 2300, 2400, 2400, 2300, 2400, 2400, 2400, 2300, 2400, 2400, 200, 200, 300, 300, 300, 300, 300, 300, 300]} + params_ = json.loads(self.params_json) + params_['passed_data'].update(runtimeparams) + runtimeparams_json = json.dumps(runtimeparams) + params_json = json.dumps(params_) + retrieve_hass_conf, optim_conf, plant_conf = utils.get_yaml_parse( + params_json, logger + ) + set_type = "dayahead-optim" + params, retrieve_hass_conf, optim_conf, plant_conf = utils.treat_runtimeparams( + runtimeparams_json, + params_json, + retrieve_hass_conf, + optim_conf, + plant_conf, + set_type, + logger, + emhass_conf, + ) + ha_config = { + 'currency': 'USD', + 'unit_system': {'temperature': '°F'} + } + params_with_ha_config_json = utils.update_params_with_ha_config( + params, + ha_config, + ) + params_with_ha_config = json.loads(params_with_ha_config_json) + self.assertTrue(params_with_ha_config["passed_data"]["custom_cost_fun_id"]["unit_of_measurement"] == '$') + self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_load_cost_id"]["unit_of_measurement"] == '$/kWh') + self.assertTrue(params_with_ha_config["passed_data"]["custom_unit_prod_price_id"]["unit_of_measurement"] == '$/kWh') def test_build_secrets(self): # Test the build_secrets defaults from get_test_params()