Hello! This is my first time posting to this form, so bear with me
I’m trying to create a sensor that’ll show me the time-til-next-train for our local Caltrain system. The Caltrain API is pretty straightforward, and I’ve been able to get it working in Siri shortcuts and (somewhat) in home assistant. Unfortuately, the json that gets returned is really gross (a bunch of nested dictionaries) so I decided to create a value template to format things a little nicer.
Here’s my current setup (I’m sure there’s a better way to do the time parsing, but this works for now )
sensor:
- platform: rest
resource: <removed>
method: GET
name: bullet_train_data
value_template: >-
{% set test_list = value_json['ServiceDelivery']['StopMonitoringDelivery']['MonitoredStopVisit'] %}
{% set data = namespace(times=[]) %}
{% for train_json in test_list %}
{% set inner_json = train_json['MonitoredVehicleJourney'] %}
{% if inner_json['PublishedLineName'] == 'Local' %}
{% set given_datetime = inner_json['MonitoredCall']['ExpectedDepartureTime'] %}
{% set parsed_datetime = as_timestamp(strptime(given_datetime, '%Y-%m-%DT%H:%M:%SZ')) | timestamp_custom('%H:%m') %}
{% set now_formatted = now().timestamp() | timestamp_custom('%H:%M') %}
{% set time_diff = ((as_timestamp(strptime(now_formatted, '%H:%M')) - as_timestamp(strptime(parsed_datetime, '%H:%M')) /60) | int) %}
{% set formatted_time_diff = time_diff | timestamp_custom('%M') %}
{% set data.times = data.times + [formatted_time_diff] %}
{% endif %}
{% endfor %}
{{ data.times }}
I’ve also enabled debugging for REST and for REST sensors, which has let me verify that the query returns the data I expect. This is what I see in the log:
2021-09-07 08:58:12 DEBUG (MainThread) [homeassistant.components.rest.data] Updating from <redacted>
2021-09-07 08:58:16 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {"ServiceDelivery":{"ResponseTimestamp":"2021-09-07T15:58:21Z","ProducerRef":"CT","Status":true,"StopMonitoringDelivery":{"version":"1.4","ResponseTimestamp":"2021-09-07T15:58:21Z","Status":true,"MonitoredStopVisit":[{"RecordedAtTime":"2021-09-07T15:58:16Z","MonitoringRef":"70012","MonitoredVehicleJourney":{"LineRef":"L5","DirectionRef":"S","FramedVehicleJourneyRef":{"DataFrameRef":"2021-09-07","DatedVehicleJourneyRef":"504"},"PublishedLineName":"LTD 5","OperatorRef":"CT","OriginRef":"70012","OriginName":"San Francisco Caltrain Station","DestinationRef":"70262","DestinationName":"San Jose Diridon","Monitored":true,"InCongestion":null,"VehicleLocation":{"Longitude":"-122.396149","Latitude":"37.7754288"},"Bearing":null,"Occupancy":null,"VehicleRef":"504","MonitoredCall":{"StopPointRef":"70012","StopPointName":"San Francisco Caltrain Station","VehicleLocationAtStop":"","VehicleAtStop":"","AimedArrivalTime":"2021-09-07T16:14:00Z","ExpectedArrivalTime":null,"AimedDepartureTime":"2021-09-07T16:14:00Z","ExpectedDepartureTime":"2021-09-07T16:14:00Z","Distances":""}}},{"RecordedAtTime":"1970-01-01T00:00:00Z","MonitoringRef":"70012","MonitoredVehicleJourney":{"LineRef":"L1","DirectionRef":"S","FramedVehicleJourneyRef":{"DataFrameRef":"2021-09-07","DatedVehicleJourneyRef":"112"},"PublishedLineName":"Local","OperatorRef":"CT","OriginRef":"70012","OriginName":"San Francisco Caltrain Station","DestinationRef":"70272","DestinationName":"Tamien","Monitored":true,"InCongestion":null,"VehicleLocation":{"Longitude":"","Latitude":""},"Bearing":null,"Occupancy":null,"VehicleRef":null,"MonitoredCall":{"StopPointRef":"70012","StopPointName":"San Francisco Caltrain Station","VehicleLocationAtStop":"","VehicleAtStop":"","AimedArrivalTime":"2021-09-07T16:38:00Z","ExpectedArrivalTime":null,"AimedDepartureTime":"2021-09-07T16:38:00Z","ExpectedDepartureTime":"2021-09-07T16:38:00Z","Distances":""}}},{"RecordedAtTime":"1970-01-01T00:00:00Z","MonitoringRef":"70012","MonitoredVehicleJourney":{"LineRef":"L5","DirectionRef":"S","FramedVehicleJourneyRef":{"DataFrameRef":"2021-09-07","DatedVehicleJourneyRef":"506"},"PublishedLineName":"LTD 5","OperatorRef":"CT","OriginRef":"70012","OriginName":"San Francisco Caltrain Station","DestinationRef":"70262","DestinationName":"San Jose Diridon","Monitored":true,"InCongestion":null,"VehicleLocation":{"Longitude":"","Latitude":""},"Bearing":null,"Occupancy":null,"VehicleRef":null,"MonitoredCall":{"StopPointRef":"70012","StopPointName":"San Francisco Caltrain Station","VehicleLocationAtStop":"","VehicleAtStop":"","AimedArrivalTime":"2021-09-07T17:14:00Z","ExpectedArrivalTime":null,"AimedDepartureTime":"2021-09-07T17:14:00Z","ExpectedDepartureTime":"2021-09-07T17:14:00Z","Distances":""}}}]}}}
In terms of already-attempted-debugging, I’ve opened dev tools and set/run the following:
{% set value_json = {"ServiceDelivery":{"ResponseTimestamp":"2021-09-07T15:58:21Z","ProducerRef":"CT","Status":true,"StopMonitoringDelivery":{"version":"1.4","ResponseTimestamp":"2021-09-07T15:58:21Z","Status":true,"MonitoredStopVisit":[{"RecordedAtTime":"2021-09-07T15:58:16Z","MonitoringRef":"70012","MonitoredVehicleJourney":{"LineRef":"L5","DirectionRef":"S","FramedVehicleJourneyRef":{"DataFrameRef":"2021-09-07","DatedVehicleJourneyRef":"504"},"PublishedLineName":"LTD 5","OperatorRef":"CT","OriginRef":"70012","OriginName":"San Francisco Caltrain Station","DestinationRef":"70262","DestinationName":"San Jose Diridon","Monitored":true,"InCongestion":null,"VehicleLocation":{"Longitude":"-122.396149","Latitude":"37.7754288"},"Bearing":null,"Occupancy":null,"VehicleRef":"504","MonitoredCall":{"StopPointRef":"70012","StopPointName":"San Francisco Caltrain Station","VehicleLocationAtStop":"","VehicleAtStop":"","AimedArrivalTime":"2021-09-07T16:14:00Z","ExpectedArrivalTime":null,"AimedDepartureTime":"2021-09-07T16:14:00Z","ExpectedDepartureTime":"2021-09-07T16:14:00Z","Distances":""}}},{"RecordedAtTime":"1970-01-01T00:00:00Z","MonitoringRef":"70012","MonitoredVehicleJourney":{"LineRef":"L1","DirectionRef":"S","FramedVehicleJourneyRef":{"DataFrameRef":"2021-09-07","DatedVehicleJourneyRef":"112"},"PublishedLineName":"Local","OperatorRef":"CT","OriginRef":"70012","OriginName":"San Francisco Caltrain Station","DestinationRef":"70272","DestinationName":"Tamien","Monitored":true,"InCongestion":null,"VehicleLocation":{"Longitude":"","Latitude":""},"Bearing":null,"Occupancy":null,"VehicleRef":null,"MonitoredCall":{"StopPointRef":"70012","StopPointName":"San Francisco Caltrain Station","VehicleLocationAtStop":"","VehicleAtStop":"","AimedArrivalTime":"2021-09-07T16:38:00Z","ExpectedArrivalTime":null,"AimedDepartureTime":"2021-09-07T16:38:00Z","ExpectedDepartureTime":"2021-09-07T16:38:00Z","Distances":""}}},{"RecordedAtTime":"1970-01-01T00:00:00Z","MonitoringRef":"70012","MonitoredVehicleJourney":{"LineRef":"L5","DirectionRef":"S","FramedVehicleJourneyRef":{"DataFrameRef":"2021-09-07","DatedVehicleJourneyRef":"506"},"PublishedLineName":"LTD 5","OperatorRef":"CT","OriginRef":"70012","OriginName":"San Francisco Caltrain Station","DestinationRef":"70262","DestinationName":"San Jose Diridon","Monitored":true,"InCongestion":null,"VehicleLocation":{"Longitude":"","Latitude":""},"Bearing":null,"Occupancy":null,"VehicleRef":null,"MonitoredCall":{"StopPointRef":"70012","StopPointName":"San Francisco Caltrain Station","VehicleLocationAtStop":"","VehicleAtStop":"","AimedArrivalTime":"2021-09-07T17:14:00Z","ExpectedArrivalTime":null,"AimedDepartureTime":"2021-09-07T17:14:00Z","ExpectedDepartureTime":"2021-09-07T17:14:00Z","Distances":""}}}]}}} %}
// this is the copy-pasted log output from above
{% set test_list = value_json['ServiceDelivery']['StopMonitoringDelivery']['MonitoredStopVisit'] %}
{% set data = namespace(times=[]) %}
{% for train_json in test_list %}
{% set inner_json = train_json['MonitoredVehicleJourney'] %}
{% if inner_json['PublishedLineName'] == 'Local' %}
{% set given_datetime = inner_json['MonitoredCall']['ExpectedDepartureTime'] %}
{% set parsed_datetime = as_timestamp(strptime(given_datetime, '%Y-%m-%DT%H:%M:%SZ')) | timestamp_custom('%H:%m') %}
{% set now_formatted = now().timestamp() | timestamp_custom('%H:%M') %}
{% set time_diff = ((as_timestamp(strptime(now_formatted, '%H:%M')) - as_timestamp(strptime(parsed_datetime, '%H:%M')) /60) | int) %}
{% set formatted_time_diff = time_diff | timestamp_custom('%M') %}
{% set data.times = data.times + [formatted_time_diff] %}
{% endif %}
{% endfor %}
{{ data.times }}
// this is copy-pasted from the value template above
which gives me my desired output:
[
"39"
]
When I try to view the sensor in devtools or on the entities page, it always reads Unknown. I’ve tried calling homeassistant.update_entity
on it, which doesn’t change the value, and I’ve tried setting a value manually in devtools (which works) and then calling update, but that just sets it back to Unknown. I’ve also tried setting the value to only one element from the list instead of the list as a whole, but no dice.
Any idea where I’m going wrong? If there are better ways to debug templates, I’d be interested to know, since I think the problem probably lies there instead of in the REST query directly (since I’m getting my expected response back in logs).