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

TypeError: the JSON object must be str, bytes or bytearray, not NoneType #27

Open
Tuumke opened this issue Feb 12, 2020 · 6 comments
Open

Comments

@Tuumke
Copy link

Tuumke commented Feb 12, 2020

Was requested to create an issue here about the NoneType error i had in HomeAssistant.

The error is starting to shop up more and more. Will look into if we can turn on extra logging

Yeah, it looks like it does not get any data back, and then wants to convert it to json (Python dict), which of course results in a crash (it talks about NoneType, which means data was None). The library can try to catch that, but will likely then throw a custom error of its own, as it is not an answer we expect.

Wed Feb 12 2020 07:30:05 GMT+0100 (Midden-Europese standaardtijd)
Update for sensor.amersfoort_harderwijk fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 279, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 461, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 240, in wrapper
    result = method(*args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/components/nederlandse_spoorwegen/sensor.py", line 208, in update
    2,
  File "/usr/local/lib/python3.7/site-packages/ns_api.py", line 932, in get_trips
    return self.parse_trips(raw_trips, requested_time)
  File "/usr/local/lib/python3.7/site-packages/ns_api.py", line 834, in parse_trips
    obj = json.loads(data)
  File "/usr/local/lib/python3.7/json/__init__.py", line 341, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType
@aquatix
Copy link
Owner

aquatix commented Feb 12, 2020

It might be that the NS API is not functioning well because it is overworked. I'll look into getting better feedback. Could you share your sensor config for this one (of course without the API key and such)?

@Tuumke
Copy link
Author

Tuumke commented Feb 12, 2020

platform: nederlandse_spoorwegen
api_key: !secret ns_api
routes:
  - name: Harderwijk-Amersfoort
    from: Hd
    to: Amf
  - name: Amersfoort-Harderwijk
    from: Amf
    to: Hd

And my automation:

id: 'Check NS in de Middag'
alias: 'Check NS in de Middag'
trigger:
  - platform: template
    value_template: "{% if is_state('sensor.amersfoort_harderwijk', 'departure_delay') %}true{% endif %}"
condition:
  - condition: time
    after: '13:00:00'
    before: '18:00:00'
action:
  - service: notify.telegram
    data_template:
      title: "Trein naar Harderwijk heeft vertraging"
      message: |
        Vertrektijd {{ state_attr('sensor.amersfoort_harderwijk', 'departure_time_actual') }}
        vanaf spoor {{ state_attr('sensor.amersfoort_harderwijk', 'departure_platform_actual') }}
        aankomst om {{ state_attr('sensor.amersfoort_harderwijk', 'arrival_time_actual') }}
        Volgende trein vertrekt om {{ state_attr('sensor.amersfoort_harderwijk', 'next') }}
        Going {{ state_attr('sensor.amersfoort_harderwijk', 'going') }}
        departure_delay {{ state_attr('sensor.amersfoort_harderwijk', 'departure_delay') }}
        departure_time_planned {{ state_attr('sensor.amersfoort_harderwijk', 'departure_time_planned') }}
        departure_time_actual {{ state_attr('sensor.amersfoort_harderwijk', 'departure_time_actual') }}
        status {{ state_attr('sensor.amersfoort_harderwijk', 'status') }}"

Not sure which of the 2 makes the checks to the NS api? I guess the sensor?
Is there a reason why we have 2 API keys in the portal? Maybe there is an API limit?
-edit-

Het gebruik van de NS API’s is gratis, maar er zit wel een limiet op het aantal calls dat uitgevoerd mag worden. Het limiet verschilt per product. NS bepaald dit limiet en dit kan aangepast worden indien dit door NS gewenst is.

@aquatix
Copy link
Owner

aquatix commented Feb 12, 2020

Oy, the typo in 'bepaald' is a nice cringe.
As far as I understand HA, the sensor does the actual calls/checking, as it is responsible for setting its boolean to true or false according to whatever you are measuring. Your automation then uses that state to trigger the Telegram notification (or not).
I'm curious to how often HA runs the sensor check; if it is run multiple times every minute, you might run out of your API quotum. Maybe you can see it in your history?

@aquatix
Copy link
Owner

aquatix commented Feb 12, 2020

Oh, the API portal has an overview too: https://apiportal.ns.nl/Developer/Analytics

@YarmoM
Copy link
Contributor

YarmoM commented Feb 28, 2020

I've been getting the same issue lately and it's not related to quotas (have lived without HA for a few days, so usage is sub-any-quota). Shall we set up a custom exception to handle this one?

@aquatix
Copy link
Owner

aquatix commented Feb 2, 2024

I'm currently taking a new look at the library, also because of #34 so I'll implement this exception handler there too.

Thanks for your patience, ns-api hasn't been a real focus for me lately 😅

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

3 participants