@@ -846,15 +846,23 @@ def _map_variable_names(
846846 ) -> list | None :
847847 """Helper to map old variable names to new ones (if renaming occurred)."""
848848 if not target_list :
849- self .logger .warning (f"Unable to find all the sensors in { param_name } parameter" )
849+ # Dynamic Warning Message
850+ # Don't hardcode "sensor_power_photovoltaics". Use the actual parameter name.
851+ self .logger .warning (f"The list of sensors for parameter '{ param_name } ' is empty." )
850852 self .logger .warning (
851- f"Confirm sure all sensors in { param_name } are sensor_power_photovoltaics and/or sensor_power_load_no_var_loads"
853+ f"Please verify that the sensors defined in '{ param_name } ' match "
854+ "the sensors connected to EMHASS."
852855 )
853856 return None
854857 new_list = []
855858 for string in target_list :
856859 if not skip_renaming :
857- new_list .append (string .replace (var_load , var_load + "_positive" ))
860+ # Exact Match Logic
861+ # Prevent dangerous substring replacements (e.g. 'sensor.power' inside 'sensor.power_meter')
862+ if string == var_load :
863+ new_list .append (var_load + "_positive" )
864+ else :
865+ new_list .append (string )
858866 else :
859867 new_list .append (string )
860868 return new_list
@@ -896,8 +904,13 @@ def prepare_data(
896904 # Instead of calling self._validate_sensor_list (which warns),
897905 # we silently drop sensors that are not in the current fetched data.
898906 if var_replace_zero :
907+ # Optional: Log if we are dropping items to help debugging
908+ missing_sensors = [x for x in var_replace_zero if x not in self .var_list ]
909+ if missing_sensors :
910+ self .logger .debug (
911+ f"Sensors in 'sensor_replace_zero' not found in data: { missing_sensors } "
912+ )
899913 var_replace_zero = [x for x in var_replace_zero if x in self .var_list ]
900-
901914 if var_interp :
902915 var_interp = [x for x in var_interp if x in self .var_list ]
903916 # Rename Load Columns (Handle sign change)
@@ -908,17 +921,19 @@ def prepare_data(
908921 self .df_final .clip (lower = 0.0 , inplace = True , axis = 1 )
909922 self .df_final .replace (to_replace = 0.0 , value = np .nan , inplace = True )
910923 # Map Variable Names (Update lists to match new column names)
911- # Note: We still use _map_variable_names to handle the renaming logic
912- # (e.g. adding '_positive' suffix), but the input lists are now clean.
913- new_var_replace_zero = self ._map_variable_names (
914- var_replace_zero , var_load , skip_renaming , "sensor_replace_zero"
915- )
916- new_var_interp = self ._map_variable_names (
917- var_interp , var_load , skip_renaming , "sensor_linear_interp"
918- )
924+ # Only call mapping if the list is not empty to avoid spurious warnings
925+ new_var_replace_zero = None
926+ if var_replace_zero :
927+ new_var_replace_zero = self ._map_variable_names (
928+ var_replace_zero , var_load , skip_renaming , "sensor_replace_zero"
929+ )
930+ new_var_interp = None
931+ if var_interp :
932+ new_var_interp = self ._map_variable_names (
933+ var_interp , var_load , skip_renaming , "sensor_linear_interp"
934+ )
919935 # Apply Data Cleaning (FillNA / Interpolate)
920936 if new_var_replace_zero :
921- # Intersection check to ensure columns exist before assigning
922937 cols_to_fix = [c for c in new_var_replace_zero if c in self .df_final .columns ]
923938 if cols_to_fix :
924939 self .df_final [cols_to_fix ] = self .df_final [cols_to_fix ].fillna (0.0 )
0 commit comments