dnguyen800 / air-visual-card

A Lovelace card showing air quality data from airvisual.com. Requires the AirVisual component.
MIT License
103 stars 35 forks source link

Rendering of dashboard in HA fails when one of sensors is not given #30

Closed artospaj closed 3 years ago

artospaj commented 4 years ago

I noticed hat bug in HA v108.9, I believe it existed before but didn't stop the rendering of the whole HA dashboard.

AirVisualCard - TypeError - HA v106 6

My Lovelace configuration for card:

      - type: custom:air-visual-card
        air_quality_index: sensor.purifier_pro_aqi
        main_pollutant: sensor.purifier_pro_aqi
        hide_title: true
        icons: /local/air-visual-card/

As you can see there is no air_pollution_level sensor provided (it's optional in docs), but looking at the code if any of sensors is not provided, will make JS fail in the same place.

(fix in the way, I'll reference this issue)

dnguyen800 commented 4 years ago

I tested your change but it caused a crash. Can you double-check that this change works on your end?

artospaj commented 4 years ago

Hey @dnguyen800 , sorry I couldn't verify that once again because currently I'm doing a reconfiguration of my HA instance.

What I can say is that the fix I proposed just solved my problem. In my case the sensor.config property was null because I didn't pass on of sensors in Lovelace configuration.

Can you please give me some details of error and an use case to reproduce it? To be honest I'm a backend developer and I didn't care much about browser compatibility :roll_eyes: See here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining Maybe that's the reason?

dnguyen800 commented 4 years ago

I'm not a programmer and never actually learned Javascript (besides looking at other people's examples, so I have a lot of gaps in knowledge unfortunately...

What I wanted to understand was in this line of code (see below), you added a question mark '?', but I wasn't sure what it does. It's hard to search online for what a ? means in code, haha. I tried using the fix in my instance and it crashed the UI, though maybe I should double-check again if it works on your end.

if (sensor.config?.split('.')[0] == 'sensor')