I need help with a trigger based on time minus a google commute sensor

The problem I had with the using "states.sensor.time_to_work.state " as the “trigger” is that it did not update enough, the value often stays the same for 20-30 minutes if traffic is normal. So, often it would not trigger at all. That’s why I incorporated the time sensor.

yeah but now we are using states.sensor.time.state in the if statement which updates all the time.

EDIT:

to clarify, this should try to execute every time sensor.time updates, but will only execute between 5 and 9 am. Also, it should only execute when we are greater than 8.19 - time_to_work.

The reason it wasn’t firing before is because the trigger was only using time_to_work, which wasn’t updating. Now we have a entity that is updating all the time.

Would it continue to alert me every minute under ceritan conditions,

So lets say it’s 8am, and my commute is 20 minutes

we end up with 8.0 > 8.0 so no trigger

8:01am
8.01 > 8.0 so it triggers

For this example, lets just say commute is 20 minutes the whole time

8:06am
8.1 > 8.0

All the way up until 9am, right?

I mean you could even try this, which should update all the time as well:

{% set t = strptime(states.sensor.time.state,'%I:%M') %}
{{ ( t.hour + t.minute / 60 ) > ( 8.33 - states.sensor.time_to_work.state | int / 60) }}

The original problem is that you only have 1 entity_id that gets asigned to the trigger, which is states.sensor.time_to_work.state. Now you’ll have 2 entities that cause the trigger to evaluate: states.sensor.time_to_work.state and states.sensor.time.state

Yes, haha.

Make it into a template sensor!

sensor:
  - platform: template
    sensors:
      message_me:
        value_template: >
          {% set t = strptime(states.sensor.time.state,'%I:%M') %}
          {{ ( t.hour + t.minute / 60 ) > ( 8.33 - states.sensor.time_to_work.state | int / 60) }}

automation:

- alias: my junk
  trigger:
    - platform: state
      entity_id: sensor.message_me
      to: 'on' # May need to be true
      from: 'off'
1 Like

I was confused for a second, but I get it. It’s only on when it’s true. and as soon it it turns on, I need to get going!.

Awesome idea, thanks!

I like this alot, While I could still get multiple alerts, those cases would be good to get multiple alerts

for example, traffic sucks, long commute, I’m about to leave an I get another alert. That would only happen if traffic improved. I think I like that option. I’m only talking about 10 minutes, here. :slight_smile:
Thanks again

No problemo

Getting this error on the sensor

UndefinedError: ‘str object’ has no attribute ‘minute’

hmm, looking into the issue. Formatting?

Edit, I changed %I, to %H, I think it will work now.

Got this on startup, but I assume that it will fix itself once the value is populated.

2018-05-23 17:10:59 WARNING (MainThread) [homeassistant.components.sensor.template] Could not render template Time to Leave for work, the state is unknown.

Sorry, last help with formatting.

I actutually have two of these. One day a week I work somewhere else

  condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.ha_runtime_in_minutes
        above: 1    
      - condition: state
        entity_id: binary_sensor.workday
        state: 'on'
      - condition:
        - condition: time
          weekday:
            - mon
            - wed
            - thu
            - fri  

Got an error with this in my automation.yaml. It seemed ok to me.

so, a few things to note:

If you are trying to ‘and’ a bunch of statements, you don’t actually need to specify ‘and’. A list of conditions defaults to ‘and’.

second, i think the condition condition time section isn’t formatted correctly.

So correcting those issues:

  condition:
    - condition: numeric_state
      entity_id: sensor.ha_runtime_in_minutes
      above: 1    
    - condition: state
      entity_id: binary_sensor.workday
      state: 'on'
    - condition: time
      weekday:
        - mon
        - wed
        - thu
        - fri

I have a quick question.

My automation failed to run again this morning, and I’m wondering if I’m missing something simple

  - condition: time
    weekday:
      - tue

My question is about this. Do I need anything else to be configured for this condition to be available

I do have these sensors

sensor.time
sensor.time__date

This might be the issue, but maybe not. I have a few conditions, but looking through my sensors they all seemed to be true, and this was one I could not “find”

no, the time platform is different than the date_time component.

sensor.time is the time one that just displays time. I think time__date is coming from the date_time component as well, not sure what that displays though.

The only thing i can think of is that this condition was not met:

- condition: numeric_state
  entity_id: sensor.ha_runtime_in_minutes
  above: 1

Also, your condition is missing tuesday:

- condition: time
  weekday:
    - mon
    - wed
    - thu
    - fri

Thanks,

I actually have two nearly identical automations. One I posted earlier that you quoted is missing Tuesday, and the other only include Tuesday. :slight_smile:

Yeah, it looked to me that all the conditions were met this morning, HA uptime was 900+ minutes, workday was on, of course it is Tuesday.

But, after your response, I went back and looked closer, and I think I found the issue. As usual, i think it is just a typo/miss

My workday sensor is named:

binary_sensor.workday_sensor

Automation has - binary_sensor.workday

Thanks for quoting, it made it jump out. :slight_smile:

1 Like

@ptdalen - this is brilliant - can you post the full code that is working for you? including any extra sensors you need - not just the automation

Sure, I’ve changed a little since then. Here is what I currently have. It includes all needed sensors, binary sensors. It does NOT include the script for notifications. I figure everyone has thier own notification preference. So insert your notification method of choice (pushbullet, SMTP, TTS, etc)

- alias: "Notify Me when its time to leave for work"
  trigger:
  - platform: state
    entity_id: sensor.time_to_leave_for_work
    to: 'True'
    from: 'False'
  condition:
  - condition: numeric_state
    entity_id: sensor.ha_runtime_in_minutes
    above: 1    
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  action:
  - service: script.audio_notify
    data_template:
      tts_msg: "Based on current traffic, you have 10 minutes to leave if you want to get to work by 8:30am.  Traffic is {{ states('sensor.my_traffic_density_to_work') }} today."
    mplayer: "main"

BINARY SENSORS

 - platform: workday
   country: 'UnitedStates'
   workdays: [mon, tue, wed, thu, fri]


SENSORS
# Time-Date Sensor
  - platform: time_date
    display_options:
      - 'time'

  - platform: uptime
    name: "HA runtime in minutes"
    unit_of_measurement: minutes
	
  # Commute to Work
  - platform: waze_travel_time
    name: Time to Work
    origin: device_tracker.me
    destination: zone.work
    region: 'US'
	

      time_to_leave_for_work:
        value_template: >
          {% set t = strptime(states.sensor.time.state,'%H:%M') %}
          {{ ( t.hour + t.minute / 60 ) > ( 8.33 - states.sensor.time_to_work.state | int / 60) }}
        friendly_name: 'Time to Leave for Work'
        entity_id: sensor.time
		
      my_traffic_density_to_work:
        friendly_name: Traffic to Work
        value_template: >
          {% if states("sensor.time_to_work") | int >= 30 %}
            Heavy
          {% elif states("sensor.time_to_work") | int < 30 and states("sensor.time_to_work") | int >= 25 %}
            Moderate
          {% elif states("sensor.time_to_work") | int < 25 and states("sensor.time_to_work") | int >= 10 %}
            Normal
          {% else %}
            unknown
          {% endif %}
        entity_id: sensor.time
1 Like

Wow thank you so much your a star!!! - any reason you use WAZE over Google?

I’ve used both. I started getting to the limits with Google. Thats the only real reason I changed

I’m struggling to get it working

My time to be at work is different to yours it’s 9.00am

I know the automation works as I manually set the time to leave sensor to true and it fired

Looking at the history of the time to work sensor it seams it never entered a true state this morning

Do you have another sensor called time? I reused the one from the initial post.