HA is updating the wrong sensors

Dear all,

I created an own custom component.
The configuration is done via configuarion.yaml as sensor

So far it is working, but sometimes HA is updating the wrong sensors.
The integration is showing the times of the munich public transportations.
I created several entries for different stations.

E.g. I have a sensor for the station Pasing
sensor.pasing
and one for busstops in my hometown
sensor.busabfahrten
and some more entries

Sometimes it happens, that the sensor.busabfahrten will be updated with the content on the sensor.pasing also other sensors got the wrong content. Its not always the same sensor.

It looks like that it only happens when the API from the public transportation server is not available.

Maybe someone can have a lock to the sensor.py located here:

I just started with HA and Python around 7 weeks ago, so I am not the best programmer. But this behaviour is crazy.
It looks like, that HA mixed up the different threads for the integration.

I still have this problem.
I cant see any problem in my code, so I assume its a bug in HA.

Post a configuration that exibits this behaviour and i’ll have a look at it for you.

I see that in e.g. in line 262 you’re using self._hass.custom_attributes = connectioninfos – I can’t see how this would work correctly when instantiated multiple times, i.e. for more than one stop. Aren’t the sensors overwriting each other’s data here?

Just a “cold reading” of your code…

-Volker

Here is a sample config with 3 stations

sensor:
  - platform: another_mvg
    name: "Pasing"
    globalid: "de:09162:10"
    onlyline: "S3,S4,S20"
    limit: 20
    transporttypes: "SBAHN"
    hidedestination: "Deisenhofen,Holzkirchen,Grafing Bahnhof, Trudering, Ostbahnhof,Haar,Ebersberg, München Hbf, Höllriegelskreuth,Zorneding"
  - platform: another_mvg
    name: "Pasing - alle Abfahrten"
    globalid: "de:09162:10"
    limit: 20
    doublestationnumber: "2"
    transporttypes: "SBAHN,BUS,REGIONAL_BUS,TRAM"
    timezone_from : "Europe/Berlin"
    timezone_to   : "Europe/Berlin"
  - platform: another_mvg
    name: "Olching und Eichenau"
    globalid: "de:09179:6110"
    globalid2: "de:09179:6180"
    hidedestination: "Mammendorf,Maisach,Geltendorf,Buchenau,Grafrath"
    limit: 20
    transporttypes: "SBAHN"
    doublestationnumber: "3"

You have to create 3 custom cards

type: custom:content-card-another-mvg
entity: sensor.pasing_alle_abfahrten
type: custom:content-card-another-mvg
entity: sensor.pasing
type: custom:content-card-another-mvg
entity: sensor.olching_und_eichenau

This is how it should look

But it will be a little bit hard to run in the problem, because most of the time its working. Only if the API is busy or not reachable. And also not always.

Its generating HTML code in a loop and I read the HTML code in the card.
This means connectioninfos['connections'] contains the whole connections as HTML.

You’re right, you’re only instantiating a single ConnectionInfo-sensor, not multiples.

At this point I can only give some suggestions: can you reduce the code that you have to deal with by using the existing library mvg-api · PyPI instead of making the requests your own? Less code might make the problem more obvious.

Since you assume it’s due to connection problems, you could set up a test-case that mocks a failing HTTP request, so that you can reliably debug this instead of relying on random observations.

Maybe it’s also possible to split off the non-HA-part of the Python, like the HTML-generation, and debug this in a plain Python-project instead of the HA-context, which would make it easier for others to reproduce – you’ll not find many people who’d be willing to install your code just to debug it if they don’t need the integration themselves.

The MVG-Api project repository has been archived by the owner and was also using the legacy API.

Maybe (as you wrote) its a good idea, to make a Python only code for debugging.
But I am pretty sure it is not the wrong response from the API, because the title of the Stop is generated in the HA sensor.py for each Sensor. And this title from a different sensor (station) is in the HTML Code for the expected station.

you’ll not find many people who’d be willing to install your code just to debug it if they don’t need the integration themselves.

You are absolute right. But maybe someone had similar problems (with other projects) in the past and can give me a hint.

1 Like

So had chance to have a look at this for you and i think i agree with @VolkerStolz abiut the issue. You are setting self._hass.custom_attributes = connectioninfos, which updates a variable on the hass object which exists on every instance of your sensor. As such, when one of them fails, it is possible that the card populates from data for the previous card.

Take the hass bit out and just do self.custom_attributes = connectioninfos

Then this will only exist in your instance.

I have a number of recommendations to help you make this easier to read/manage and will send you a PR soon with those changes.

Edit: PR sent

1 Like

Hey Mark, thank you very much for the time you spend. :slight_smile:

Locally I had already a newer version running, so I had to merge your code with my changes.

So far its working fine in the backend. Currently I have only some issues in the frontend with the new CSS Table.

I want no line break in the right column (with class time)
For this I inserted white-space: nowrap; in the class

        /* Column widths */
        .label {
          width: 15%;
          padding-left: 5px;
        }
        .destination {
          width: 45%;
        }
        .track {
          width: 10%;
        }
        .time {
          width: 30%;
		  white-space: nowrap;
        }

So far, this is working fine but if I resize the browser window (short before HA puts the 3. card in a new line) the time column (Abfahrt) is going in “overflow”

In only want a line break (if needed) in the 2nd column (Ziel) with the css class destination. In my old HTML table version I put the right column to white-space: nowrap; without any further size settings for the other rows and it was working.

In your version you set a fixed with for the different columns , but if I remove the width, its not working any more.

What I want
Column Line (class line) → only as big as the label (like “S3”) is
Column Ziel (class destination) → line break is allowed
Column Gleis (class track) → only as big as the content is, normally not longer than the word “Gleis”
Column Abfahrt (class time) → only as bis as the content is, no line break and no overflow

I know, its no longer related to the initial problem and you already spent a lot of time, but If you have an idea, feel free to push me in the correct direction. :see_no_evil: If not, no problem.

Now we will see if the initial problem is solved.
At least, I learned some more about HA. :slight_smile: