Time formatting

I am struggling (as I often do) with time formatting in yaml. This time I am trying to convert the expression below as minutes only, i.e. as an integer telling me how many minutes my garagedoor has been open (or closed).

{{ (now() - states.binary_sensor.garage_door_detector_sensor["last_changed"]) }}

The expression returns the time string format: 1:44:38.073348

Also, is there a good guide to date and time formatting in yaml? I have googled a lot, but not been able to find anything that fits my needs.


I believe the now() function is a home assistant, JSON, or Jinja thing. I’ve looked up docs on JSON and Jinja and didn’t find anything on the topics that match NOW. The format of “last_changed” appears to be formatted in JSON, so maybe now() is a JSON function. Either way, yaml is just the framework for the information. It’s not what executes the code. Jinja is what executes inside the yaml. I’d look there.

Ok, so I need to find the format syntax for JSON then. I have already browsed through the Jinja documentation linked to from HA templates. I’ll try to find some documentation for JSON as well.

What are you trying to do? There may be an easy solution if I were to know your end game.

I want to know if my garage door is left open for more than say 15 minutes and then trigger a warning message so that I can close it remotely.

Ah ok, you should implement an automation using time. This is basically built in.

- alias: "notify if door open for x"
    platform: state
    entity_id: switch.garage_door
    to: 'on'
      minutes: 15

see: https://home-assistant.io/docs/automation/trigger/

Ok, I’ll try that. Thanks!

1 Like

It worked great, thank you!

1 Like