Time operations with Trasportation sensor

Hi all,

I have setup a trasportation sensor that allows me to monitor some attributes. I want to create a template sensors that take an attributes in time format (the time of train arrival, for example 19:00) and make a math operations: reduce the value by 10 minutes, something like this:

{{ states.sensor.frecciarossa_delle_1845.attributes [“compOrarioPartenzaZeroEffettivo”] }} - now() - 1h .

It is possible?

Thank you in advance.

So you just want to be notified 10 minutes before a train is arriving?

in your sensor section, this is assuming your time is formatted: “19:00”:

  - platform: template
    sensors:
      next_train_arrival:
        value_template: >
          {% set hour, minute = states.sensor.frecciarossa_delle_1845.attributes [“compOrarioPartenzaZeroEffettivo”].split(":") %}
          {% if now().hour == hour | float and now().minute == minute | float %}
            on
          {% else %}
            off
          {% endif %}

Then build your automations off the transition from off to on using sensor.next_train_arrival

First of all thank you!

I copied and pasted your code but I receive an error:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected char ‘“’ at 72) for dictionary value @ data[‘sensors’][‘next_train_arrival’][‘value_template’]. Got ‘{% set hour, minute = states.sensor.frecciarossa_delle_1845.attributes [“compOrarioPartenzaZeroEffettivo”].split(“:”) %} {% if now().hour == hour | float and now().minute == minute | float %}\n on\n{% else %}\n off\n{% endif %} \n’. (See ?, line ?). Please check the docs at Template - Home Assistant

I take this opportunity to better explain my goal:

The train sensor has attribute only the arrival time for the final destination (the last station). But I need to visualize the arrival time to another station that is more or less 10 minutes before the last one.

So if the compOrarioArrivoZeroEffettivo is 19:00 my goal is to create a sensor that shows 18:50. Is this possible?

Thank you!

try replacing all double quotes with single. Make sure the character is not copied from microsoft word, because they use the forward/backward quotes. This needs to be the normal quote.

OK. Now it works!!

But I can see only the status as on or off…not the time minus 10 minutes…I know I miss something…but I’m a total noob regard templating…What should I do?

you need to come up with the math associated with removing minutes from the hour, minute that you parsed.

So you will need to convert hour/minute into seconds, remove 10 minutes (in seconds), and convert it back.

I don’t know how to start to learn to do it ! :zipper_mouth_face:

It’s simple math man. How many seconds are in a minute? How many minutes are in an hour? My niece just learned this in 7th grade, you can do it if you are working with home assistant.

The problem is not the math. But how to write the math in yaml.

That’s your first problem. The code placed in yaml files is not yaml, its Jinja. Yaml is the format, like xml. Jinja is the for loops, if statements, and overall object use inside the templates.

http://jinja.pocoo.org/docs/2.10/

That’s a link to the documentation

1 Like