PyrokineticDarkElf / Add-the-next-Spot-the-Station-event-to-Home-Assistant-sensor

This Package and Script will add a sensor to Home Assistant which includes data about the next Spot the Station event in your area.
0 stars 0 forks source link

Sensor stopped working #2

Open liakjim opened 3 days ago

liakjim commented 3 days ago

Hello, this sensor was working great for some months now and recently i discovered that there was an error and the sensor stayed at unknown state.

When i manually do an update from actions, i get this error in logs

Logger: homeassistant.components.command_line.utils
Source: components/command_line/utils.py:56
integration: Command Line (documentation, issues)
First occurred: 6:28:35 PM (1 occurrences)
Last logged: 6:28:35 PM

Command failed (with return code 1): python3 ./python_scripts/spot_the_next_station.py next

and this warning

Logger: homeassistant.components.command_line
Source: components/command_line/sensor.py:172
integration: Command Line (documentation, issues)
First occurred: 6:28:35 PM (1 occurrences)
Last logged: 6:28:35 PM

Empty reply found when expecting JSON data
liakjim commented 3 days ago

From some research i discovered that it was the xml file. I am using this one -> https://spotthestation.nasa.gov/sightings/xml_files.cfm?filename=Greece_None_Athens.xml When i am running at my pc , it gives this error -> C:\Users\liakj\Desktop>python spot_the_next_station.py Traceback (most recent call last): File "C:\Users\liakj\Desktop\spot_the_next_station.py", line 60, in <module> next_event = extract_spot_the_station_info() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\liakj\Desktop\spot_the_next_station.py", line 46, in extract_spot_the_station_info duration_minutes = int(duration_str.split()[0]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: 'less'

If i use another xml like this one -> https://spotthestation.nasa.gov/sightings/xml_files.cfm?filename=Greece_None_Thessaloniki.xml everything is working fine. Strange.

liakjim commented 3 days ago

Ok the issue is at line 46. In the Athens xml the duration is less than 1 minute that gives the error. I tried the chatgpt and it gave me the following solution ->

# Calculate end time by adding duration to start time
duration_str = info_dict.get("Duration", "").lower().strip()

try:
    # Handle special cases like "less than 1 minute"
    if "less than" in duration_str:
        duration_minutes = 1  # Assume minimum duration of 1 minute
    else:
        # Extract the numeric value from duration_str
        duration_minutes = int(duration_str.split()[0])
except (ValueError, IndexError):
    # Default to 0 minutes if duration_str is invalid
    print(f"Warning: Invalid duration format '{duration_str}'")
    duration_minutes = 0

event_end = event_start + timedelta(minutes=duration_minutes)

This code works fine.