andystewart999 / ha_transportnsw

A Home Assistant component to provide real-time Transport NSW journey information
GNU General Public License v3.0
7 stars 1 forks source link

Subtracting out the 'wait time' on the state #6

Open ktwb opened 3 days ago

ktwb commented 3 days ago

Hi mate,

I've been trying to get this to show in a table the 'time to leaving', but the return of any given sensor is almost always (by way of state) the time until the train(or service as it may be) leaves its origin.

Is there a way to call this?

andystewart999 commented 2 days ago

Do you mean that you want to see what the actual departure time is? If so, that info is in the departure_time attribute.

Template: {{ state_attr('sensor.sensor_name', 'departure_time') }} Result: 2024-10-22T06:20:00

image

ktwb commented 2 days ago

Not quite - the issue is that the "state" data is the actual departure time. When I feed in the 'wait time', when the state (the 'due in' value in attributes and return state) reaches the 'wait time' (i.e. - if it's going to take 11 minutes to say, walk there, and it's going to arrive in less than 11 minutes) that is when it stops tracking that service and returns the new, latest service. This is shown below in the gap between the minimum value and the origin, which is the value of the wait time that you feed into the data.

image

This is fine, however, and I appreciate its usability for dealing with when the train is actually at the station & the wiggle room around that, however, I was wondering if there is a way to get the 'leaving time' instead of the actual departure time, or, in other words, to have an attribute that is the difference between the current time and the actual departure time plus the wait time.

Practically, the desired result is that I want HA to have a value that counts down when I need to leave to make the train!

I apologise if this is a bit of a weird request - I'm a learner to the world of code/yaml/jinga2 and have probably missed something....

andystewart999 commented 21 hours ago

OK, I get you now. See if this gets you what you expect - you can try it out in 'Developer Tools/Template':

{% set wiggle_room_in_minutes = 8 %}
Seconds before I should leave for the station: {{ as_timestamp(state_attr('sensor.arncliffe_to_city_trip_1', 'departure_time')) - as_timestamp(now()) + (wiggle_room_in_minutes * 60) }}

And it could be used as the state in a template sensor:

  - sensor:
      - name: "Minutes until I need to leave"
        unit_of_measurement: "min"
        state: >
          {% set wiggle_room_in_minutes = 8 %}

          {{ (as_timestamp(state_attr('sensor.arncliffe_to_city_trip_1', 'departure_time')) - as_timestamp(now()) + (wiggle_room_in_minutes * 60) ) / 60}}

Obviously, right now at least, trip_wait_time isn't an attribute so you'd have to define the wiggle room manually. I could add trip_wait_time as an attribute though, if this ends up being the approach you want, which means you could use state_attr('sensor.sensor_name', 'trip_wait_time') in your template, so it automatically captures and reflects and changes to the per-trip config.

Let me know how you go.

ktwb commented 13 hours ago

Makes sense. The ultimate use case here is a little sign next to my door which tells me how long until leaving the station, which uses a flex card to order multiple possible train trips. So ideally, I'd like to have both in the sensor from the integration so that I can display 'time to leave' as a tabled attribute as with 'walk time' (which is the wait time input currently not outputed as an attribute).

image

I'm happy to give the json a go and give you my attempt, but it will be my first shot at json coding and expect I will get it to you via reply comment over the weekend.