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 1 month ago

ktwb commented 1 month 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 1 month 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 1 month 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 4 weeks 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 4 weeks 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.

andystewart999 commented 4 weeks ago

Good luck! I'm not familiar with the card you're using but if it's this one, a quick look here suggests that you can't use a template for a column value directly, it would have to be via the template sensor approach discussed.

ktwb commented 3 weeks ago

Alright - I've tried to make the changes. I'm not even sure how Github works to be perfectly honest with you so don't put any weight on how it is that I've clicked 'submit' - I had intended for it to be drafts...

I'm not entirely sure how to test the changes I've made either on my home assistant...

{% set wiggle_room_in_minutes = 15 %} Minutes 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))/60 }}

Template code had a sign the wrong way for anyone else using this.

(EDIT )

I can't even work out how to test it on my own HA... I would be thankful if you could take what I've done and make another attribute to do it, and am happy to do (to the best of my ability) what I can do assist you with it.

andystewart999 commented 3 weeks ago

OK - I've updated sensor.py to support a new configuration setting departure_time_offset and to add a new attribute offset_departure_time which basically is the time the train is expected to leave minus whatever departure_time_offset is.

  - platform: ha_transportnsw
    api_key: <blah>
    origin_id: 207210
    destination_id: 207710
    transport_type: 1 # Only trains
    return_info: medium
    name: "Gordon to Waitara"
    trips_to_create: 1
    trip_wait_time: 2
    departure_time_offset: 11

Take the contents of the attached file and paste them over homeassistant/custom_components/ha_transportnsw/sensor.py, then restart Home Assistant. Each journey will then include the new attribute:

image

Depending on how you want to actually consume this information though, it does seem to me that just adding your 'wiggle room' factor to trip_wait_time would achieve exactly the same thing, in that it would only show trips that depart, say, 2 + 11 minutes from now?

sensor.txt

ktwb commented 5 days ago

Thank you king! Sorry for the delay, I'm still working through this but your suggestions have been very helpful. I'll update this thread once I've finished it all up!