Calculating with times

HI,

i have a small brain freeze here.

I would like to create a automation where I can see when to leave the house as best for work.

It kind already works fine, but I want to have it like, If i want to be an on time I have to leave now.
If I would leave now, I will be good in time.

  - alias: "to work"
    trigger:
      platform: time
      minutes: '/1'
      seconds: '0'
    condition:  
      condition: and
      conditions:
        - condition: time
          weekday:
            - mon
            - tue
            - wed
            - thu
            - fri
        - condition: time
          after: '06:30:00'
          before: '07:45:00'        
        - condition: state
          entity_id: device_tracker.creyiphone
          state: 'home'
    action:
      service: light.turn_on
      entity_id: light.neu
      data_template: 
        color_name: >
           {% if states.sensor.zu_arbeit_mann.state < "21" %} green
           {% elif states.sensor.zu_arbeit_mann.state == "21" %} orange
           {% elif states.sensor.zu_arbeit_mann.state == "22" %} orange
           {% elif states.sensor.zu_arbeit_mann.state == "23" %} orange            
           {% elif states.sensor.zu_arbeit_mann.state > "24" %} red
           {% endif %}
        brightness: 250

So, I need to be at work, at 07.45 every day.
With the sensor.zu_arbeit_mann I get the time i need from google travel time

So I need something like, if current time plus travel time is before (or shorter than?) 7.45 (or 7.40) all cool light should be green.
If current time plus travel time is 7.45 get red.

Is there anyway to make this possible ?

Thanks

You can check out the calculations I did for my “ideal time to leave for work” tts script.

Hi,

thanks for the answer, but I am getting nowhere form here…

what I have so far is:

        value_template: '{{ (strptime(now(), "%H:%M") + (strptime(sensor.zu_arbeit_mann, "&H:%M"), "%H:%M")) }}'
        unit_of_measurement: 'time'

The Result is 22.10, which does not change at all etc.

I am totally lost here…

It’s easier if you make sensors and break into smaller pieces. Basically what you want to do is 1) get the time of day you want to be at work and convert to a Unix time stamp, then 2) minus that by the minutes of travel as from the google travel time sensor. This will give you your latest time to leave. If now is greater than that time, then turn the light red. 3) create a sensor that uses that leave time minus 10 minutes. And if that time is greater but less than the first sensor turn yellow. 4) create a new template sensor that is leave time minus 20 minutes etc. Go through all of your colours. Give it a try and if you’re absolutely stuck I can give you a hand with it tomorrow. :slight_smile:

I think I know where the problem is, but I can not resolve it.

I have two sensors, the now() and the sensor.zu_arbeit_mann.
The Problem is, that the sensor.zu_arbeit_mann only provides minutes, and I guess this is the reason I can’t work with the now() as just adding minutes will not help here.

@TheCrey The best way to work with times is to convert into a timestamp ie 1513549191 (it’s the current seconds since Jan 01, 1970) and then subtract the seconds from that. Let’s say you want to subtract 15 minutes, you’d need to convert minutes to seconds (15 * 60) and subtract that number from the current timestamp. Then you’d convert the timestamp back into readable format. See my example for how to do it. It’s all there. Best of luck.