Trigger automation based on state attribute

I have a sensor called : sensor.next_departure which tells me when the next train Fridays departs. It also has some attributes which you can seen below.

  "Destination": "Neuchâtel",
  "Next departure": "11:45",
  "Next on departure": "11:52",
  "Remaining time": "0:19",
  "Start": "Biel/Bienne",
  "friendly_name": "Next train to Neuchâtel",
  "icon": "mdi:train"

What I would like to do is trigger an automation when the time for the next train departure is less that 15 do I accomplish this trigger based on a state attribute?


Did you try something along these lines?

- alias: trigger for train
    platform: template
    value_template: "{% if is_state('sensor.next_departure', < "15:00") %}true{% endif %}"

thx for the reply but i dont see how this will help since the sensor.next_departure is the time of the next departure.

And for some reason i cannot test it because it gives me a yaml syntax error on the line where platform is

You should able to use the template above for an attribute. It would just be something like 'sensor.your_sensor.attribute.the_attribute_you_want'

the thing is that {states.next_departure.attributes.remaining_time} does not give back anything, whereas {states.next_departure.attributes.departure} gives back the right value of the attribute.

In the dev console I have tried writing remaining_time in every possbile way but i cant get the value displayed

hey @mefistofelis, after spending a LOT of time on this, it just wasn’t reliable enough for me (showing negative values close to departure) and also missing updates, I’m sending you a PM with the different components to achieve it.