home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected token N in JSON

timmo001 opened this issue · comments

Problem

I keep getting these errors and am unable to resolve the issue. I'm not sure if there is an issue in home-assistant itself, sending bad JSON or bad config, but it is causing issues with HASS and my webapp. I end up getting a ton of logs in HASS and console errors in my app. Occasionally I have to reload the page as the error is very inconsistent.

Unexpected token N in JSON at position SOME_POSTION

Screenshots

image

image

image

image

In Chrome, on the network tab, select the websocket connection and see what data is being send and if that's valid JSON

image

Ah. Looks like this data occasionally gets sent:

{
   "id":20,
   "type":"result",
   "success":true,
   "result":null
}

This could be caused by errors somewhere else along the lines but perhaps we need a check to see if there is the data object before calling update?

Oh! Looks like my climate component is sending NaN for the current_temperature:

{
   "id":2,
   "type":"event",
   "event":{
      "event_type":"state_changed",
      "data":{
         "entity_id":"climate.central_heating",
         "old_state":{
            "entity_id":"climate.central_heating",
            "state":"off",
            "attributes":{
               "current_temperature":21.9,
               "min_temp":10.0,
               "max_temp":30.0,
               "temperature":22.0,
               "operation_mode":"off",
               "operation_list":[
                  "heat",
                  "off"
               ],
               "away_mode":"off",
               "friendly_name":"Central Heating",
               "supported_features":1153
            },
            "last_changed":"2018-11-25T14:14:16.009505+00:00",
            "last_updated":"2018-11-25T15:07:46.923499+00:00",
            "context":{
               "id":"13f5cc7225794966a98f352c458cf6e8",
               "user_id":null
            }
         },
         "new_state":{
            "entity_id":"climate.central_heating",
            "state":"off",
            "attributes":{
               "current_temperature":NaN,
               "min_temp":10.0,
               "max_temp":30.0,
               "temperature":22.0,
               "operation_mode":"off",
               "operation_list":[
                  "heat",
                  "off"
               ],
               "away_mode":"off",
               "friendly_name":"Central Heating",
               "supported_features":1153
            },
            "last_changed":"2018-11-25T14:14:16.009505+00:00",
            "last_updated":"2018-11-25T15:07:57.207862+00:00",
            "context":{
               "id":"d2e1be20ebbd47289de299865253f333",
               "user_id":null
            }
         }
      },
      "origin":"LOCAL",
      "time_fired":"2018-11-25T15:07:57.207894+00:00",
      "context":{
         "id":"d2e1be20ebbd47289de299865253f333",
         "user_id":null
      }
   }
}

Going to look into this and open up a PR over at the main repo if there is a bug in the generic_thermostat component or not. I guess the above gets caused by this line:

               "current_temperature":NaN,

Looks like it might be more the fault of my MQTT sensor sending null data every now and then. Not exactly sure why. It might be down to the sensor updating the data as NaN which might need handling!

Thanks for the pointer! I'll close this since it's not related to the WebSocket 👍

Update: Yup that was it. I fixed my sensors to not send any null data to the MQTT server and no more errors 🎉

@timmo001 you want to open a PR to prevent the MQTT integration from even publishing a NaN number in Home Assistant ?