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

Better value for state #27

Open
hille721 opened this issue Jan 2, 2025 · 7 comments
Open

Better value for state #27

hille721 opened this issue Jan 2, 2025 · 7 comments

Comments

@hille721
Copy link

hille721 commented Jan 2, 2025

Hi @Nisbo,

great integration! Just switched to it and really like. But what I don't understand is this state value: "Please use the project lovelace card to show your stop: ..."

Why not simply using minutes to next departure as state, with that it could be e.g. also easily used by Voice Assistant.

@Nisbo
Copy link
Owner

Nisbo commented Jan 2, 2025

@hille721
Great idea, I will put it on my todo list. :)

@Nisbo
Copy link
Owner

Nisbo commented Jan 3, 2025

I am playing around.

grafik

at the moment it only takes the value from the departure on the top

grafik

which is not the next physical departure

@hille721
Copy link
Author

hille721 commented Jan 3, 2025

Imo this is still to much information for a state value. I would keep it completely simple and only put in the either the actual departure time (so incl delay) as a date time stamp (like 2025-01-03T03:32:00+00:00) or even better the minutes till departure. I used https://github.com/danielpotthast/mvg before and there it was like that.

For the problem you are describing it would be probably best to sort the departures by actual and not planned departure.

@Nisbo
Copy link
Owner

Nisbo commented Jan 3, 2025

I already solved the issue with the sorting. Currently it looks like this:

grafik

Only the time or minutes is not enough, because you also have to know if it is a S4 or an U5 or a Bus as there could be more than one transportation_type. And I am pretty sure you also want to know if its the correct destination.

I think I will make a kind of template-scripting, so that every one can decide what he want to see.
E.g. {planned_departure} will be replaced by the Planned Departure Time, {label} the line like S4 and maybe {timeToDeparture} with the minutes to the next departure and {timestamp} with 2025-01-03T03:32:00+00:00

Do you need something else / special ?

@hille721
Copy link
Author

hille721 commented Jan 3, 2025

But this is not required as you already provide the great card and further more everyone can easily template it themselves, e.g:

image

Also do you really want to provide a state in German? What is if non German speaking HA enthusiasts wants to use it? And there are a lot in Munich I would guess :)
Furthermore imo it is an anti pattern of providing such a long string as state value, no other entity in my HA has such. I would also assume that this could negatively impact the size of the database, as the history of state changes is stored.

What I need are the minutes till the next departure. Currently it's a bit hard, I hacked this together:

{% set time_str = state_attr('sensor.garching', 'departures')[0].planned_departure %}
{% set date_str = states("sensor.date") %}
{% set datetime_str = date_str ~ " " ~ time_str %}
{% set planned_departure = strptime(datetime_str, '%Y-%m-%d %H:%M') | as_timestamp | timestamp_custom('%Y-%m-%d %H:%M:%S', true) %}
{% set current_time = now() | as_timestamp | timestamp_custom('%Y-%m-%d %H:%M:%S', true) %}
{% set minutes = ((as_timestamp(planned_departure) - as_timestamp(current_time)) / 60) | int %}
{% set minutes_plus_delay = minutes + state_attr('sensor.garching', 'departures')[0].delay %}

Thus just using the state ({{ states('sensor.garching') }}) would be much easier :)

@Nisbo
Copy link
Owner

Nisbo commented Jan 3, 2025

I am testing, translation will be the last step ;)

This is how it will work ... at least at the moment ^^
grafik

you will get this:

2025-01-03 18:05:21.605 ERROR (SyncWorker_0) [custom_components.another_mvg.sensor] AnotherMVG: Hauptbahnhof --> Template: 
Geplante Abfahrt: 17:24 (Geplante Zeit: 2025-01-03T16:24:00+00:00)
Erwartete Abfahrt: 18:04
Gleis: 25
Transporttyp: Bahn
Abfahrt von: RB33 nach Landshut (Bay) Hbf
Verspätung: 40 Minuten
Zugtyp: 
Stornierung: False
Geplante Abfahrtszeit: 2025-01-03T16:24:00+00:00
Reale Abfahrtszeit: 2025-01-03T17:04:00+00:00
Differenz in Minuten zur realen Abfahrtszeit: 1.3600983666666666 Minuten
Differenz in Minuten gerundet: 1 Minuten

2025-01-03 18:05:21.619 ERROR (SyncWorker_5) [custom_components.another_mvg.sensor] AnotherMVG: Pasing --> Template: 
Geplante Abfahrt: 18:04 (Geplante Zeit: 2025-01-03T17:04:00+00:00)
Erwartete Abfahrt: 18:04
Gleis: 7
Transporttyp: S-Bahn
Abfahrt von: S5 nach Germering, Germering-Unterpfaffenhofen
Verspätung: 0 Minuten
Zugtyp: 
Stornierung: True
Geplante Abfahrtszeit: 2025-01-03T17:04:00+00:00
Reale Abfahrtszeit: 2025-01-03T17:04:00+00:00
Differenz in Minuten zur realen Abfahrtszeit: 1.3603250166666667 Minuten
Differenz in Minuten gerundet: 1 Minuten

so you are flexible

For you

{realtime_departure_diff_minutes}

and

{minutes_difference}

Regarding logging of the state value, maybe there is an option in HA to avoid logging. I have to google it.

@Nisbo
Copy link
Owner

Nisbo commented Jan 8, 2025

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

2 participants