Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoneType Error in trips.py #993

Open
AbramVanHouse opened this issue Nov 14, 2024 · 0 comments
Open

NoneType Error in trips.py #993

AbramVanHouse opened this issue Nov 14, 2024 · 0 comments

Comments

@AbramVanHouse
Copy link

Describe the bug
The plugin encounters a TypeError when calculating trip durations due to missing timestamp data. Specifically, when either the start["Timestamp"] or end["Timestamp"] values are None, the plugin fails to compute the duration, resulting in a NoneType error and a 500 Internal Server Error. This prevents updates from the API and causes the plugin to stop functioning as expected.

To Reproduce
Steps to reproduce the behavior:
1. Run the plugin and attempt to retrieve vehicle information with the command curl -X GET "http://<server_address>:5000/get_vehicleinfo/"
2. The bug occurs when attempting to process trip data, particularly if either the start or end timestamp is missing (i.e., has a None value).
3. Yes, the issue persists after restarting the program.
4. Yes, the issue also persists after recreating the config by accessing http://server_address:port/config.

Config file
Below is the configuration file used in Home Assistant for the Opel Astra:

`# RESTful Sensor for Opel Astra with matched keys in json_attributes

sensor:

  • platform: rest
    name: Opel Astra Vehicle Info
    resource: "http://<server_address>:5000/get_vehicleinfo/"
    method: GET
    scan_interval: 300
    value_template: "OK" # Force JSON processing
    json_attributes:

    • battery
    • doors_state
    • energy
    • environment
    • ignition
    • kinetic
    • last_position
    • preconditionning
    • privacy
    • safety
    • service
    • timed_odometer
  • platform: template
    sensors:
    opel_astra_battery_voltage:
    friendly_name: "Battery Voltage"
    unit_of_measurement: "V"
    value_template: "{{ state_attr('sensor.opel_astra_vehicle_info', 'battery').voltage }}"

    opel_astra_trunk_status:
    friendly_name: "Trunk Status"
    value_template: >
    {% for door in state_attr('sensor.opel_astra_vehicle_info', 'doors_state').opening %}
    {% if door.identifier == "Trunk" %}
    {{ door.state }}
    {% endif %}
    {% endfor %}

    opel_astra_driver_door_status:
    friendly_name: "Driver Door Status"
    value_template: >
    {% for door in state_attr('sensor.opel_astra_vehicle_info', 'doors_state').opening %}
    {% if door.identifier == "Driver" %}
    {{ door.state }}
    {% endif %}
    {% endfor %}

    opel_astra_fuel_range:
    friendly_name: "Fuel Range"
    unit_of_measurement: "km"
    value_template: >
    {% for energy in state_attr('sensor.opel_astra_vehicle_info', 'energy') %}
    {% if energy.type == "Fuel" %}
    {{ energy.autonomy }}
    {% endif %}
    {% endfor %}

    opel_astra_air_temperature:
    friendly_name: "Air Temperature"
    unit_of_measurement: "°C"
    value_template: "{{ state_attr('sensor.opel_astra_vehicle_info', 'environment').air.temp }}"

    opel_astra_mileage:
    friendly_name: "Mileage"
    unit_of_measurement: "km"
    value_template: "{{ state_attr('sensor.opel_astra_vehicle_info', 'timed_odometer').mileage }}"

    opel_astra_ignition_status:
    friendly_name: "Ignition Status"
    value_template: "{{ state_attr('sensor.opel_astra_vehicle_info', 'ignition').type }}"

    opel_astra_last_location:
    friendly_name: "Vehicle Location"
    value_template: >
    "Lat: {{ state_attr('sensor.opel_astra_vehicle_info', 'last_position').geometry.coordinates[1] }},
    Lon: {{ state_attr('sensor.opel_astra_vehicle_info', 'last_position').geometry.coordinates[0] }}"

    opel_astra_privacy_state:
    friendly_name: "Privacy State"
    value_template: "{{ state_attr('sensor.opel_astra_vehicle_info', 'privacy').state }}"

    opel_astra_service_status:
    friendly_name: "Service Status"
    value_template: "{{ state_attr('sensor.opel_astra_vehicle_info', 'service').type }}"`

Environment:
• OS: Home Assistant OS running in Docker container
• Brand and model of car: Opel Astra
• Application version: 3.5.3
• Log from http://server_address:port/log and browser console log:
TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType' 2024-11-14 12:02:09,386 :: ERROR :: refresh_vehicle_info: Traceback (most recent call last): File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/psa_client.py", line 128, in __refresh_vehicle_info self.get_vehicle_info(car.vin) File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/psa_client.py", line 111, in get_vehicle_info self.record_info(car) File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/application/psa_client.py", line 197, in record_info Database.record_position(self.weather_api, car.vin, mileage, latitude, longitude, altitude, date, level, File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/db.py", line 276, in record_position conn.close() File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/db.py", line 50, in close self.execute_callbacks() File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/db.py", line 46, in execute_callbacks callback() File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/web/view/views.py", line 189, in update_trips trips_by_vin = Trips.get_trips(Cars([car])) File "/usr/local/lib/python3.9/dist-packages/psa_car_controller/psacc/repository/trips.py", line 82, in get_trips duration = (end["Timestamp"] - start["Timestamp"]).total_seconds() / 3600 TypeError: unsupported operand type(s) for -: 'NoneType' and 'NoneType'

Additional context
The error seems to stem from missing timestamp data in the API response for trip details, resulting in NoneType values for either start["Timestamp"] or end["Timestamp"]. Adding a conditional check to ensure both timestamps are present before calculating the duration (e.g., setting duration to 0 or another default value if either timestamp is missing) resolves the issue locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant