tofuSCHNITZEL / home-assistant-wienerlinien

A sensor that give you information about departures from a specified Wiener Linien stop.
MIT License
25 stars 8 forks source link

Bring back countdown #14

Open danieldifferential opened 1 year ago

danieldifferential commented 1 year ago

1.3

Not having countdown available any more severly limits the usage in a number of visualisations - bar cards with severity (like: red color if I can't make it from my behind to the station anyways, green if I can make it), placing the value in a custom map based on a picture,... all that does not seem to work (at least not for me) without a countdown-attribute.

prettyplease? ;-)

ahja: danke, tofuschnitzel, dass es dich gibt ;-))

log

Add your logs here.
tofuSCHNITZEL commented 1 year ago

Hi, if I remember correctly the home assistant documentation states that an attribute of an entity should not "change too often" and that was a main reason for removing it... but can't you use a template like this: {{ as_timestamp(states('sensor.kardinal_nagl_platz_u3_ottakring_first_departure'))-as_timestamp(now()) }} which will give you the difference in seconds between the next departure and now (which is basically a countdown?)

1.3 is only a beta release... and I'm not really happy with the way this component works right now (lots of issues behind the scenes - and its not very robust against the crazyness that is the wiener linien api) so a complete overhaul with have to be done anyway if I ever find the time to do it.

cheers

the01 commented 1 year ago

I wanted to have this in a template card with easy dynamic formatting for 12 seconds, 5 minutes, .. etc. until departure, but relative time currently only works with times in the past. So it first calculates the time difference in seconds in the future and then creates a time in past with the exact same difference:

{{ relative_time(now() - timedelta(seconds=(as_datetime(states.sensor.kardinal_nagl_platz_u3_ottakring_first_departure.state) - now()).total_seconds())) }}

Not sure if there is a better way to achieve this (without coding the seconds/minutes/.. formatting myself), but wanted to share in case some one else needs it. Sometimes it takes the api longer to update, in which case the new calculated time is again in the future and a full datetime is shown instead of the formatted time difference. Also for night time it's helpful to include an if-block that tests if the value is set