Multiple triggers in a time window

My lights off automation is based on motion and time. Basically I wait for the sensor to be off for a period of time:

- alias: Front motion - lights off
  trigger:
    platform: state
    entity_id: binary_sensor.front_motion
    to: 'off'
    for:
      minutes: 5

I was thinking I would only use the second trigger ( I left it for comparison). I do realize though that what Iā€™m trying to do with that trigger is more of a conditon. :frowning:

So, when my automation completes (If I was still in the room, but still enough for this to happen)

wait_template: "{{ is_state('binary_sensor.motion_delay_kitchen', 'off') }}"

I then move while the automation is probably still in progress turning off the lights. The binary_sensor.motion_sensor_kitchen_motion changes to on. But since the automation is not quite finished, it does not trigger. When the automation finishes, the motion is still on, not changing TO: on. So it does not trigger until it goes off and then back on.

I was thinking (incorrectly), that that the trigger template would be on. and would trigger if it had recently triggered.

I dont like the on/off for X minute automations (probably just being picky), I like it when the lights turn off quickly after leaving a room. Well most the time anyway. :slight_smile: Iā€™m going to rethink this a bit. Iā€™ll share anything i come up with.

I havenā€™t completely followed how your binary_sensor reacts to motion, and exactly what youā€™re ultimately trying to achieve, but, yes, it is problematic if an automationā€™s trigger fires while the actions of the automation are still running from a previous trigger. I definitely try to avoid that. So one thing I can suggest (in general, and especially if there are any waits or delays in an automationā€™s actions) is to move the actions to a script, and start the script from the automation. In fact, the automation should first cancel the script, then call it. So, like this:

action:
  - service: script.turn_off
    entity_id: script.MY_SCRIPT
  - service: script.MY_SCRIPT

Thanks for your help. I feel like Iā€™m starting to wrap my head around the value template more and more. Does this look like it will work. I have two possible work locations, and I used to always work at each location on specific days, so my automation was hard coded for which day of the week, but lately Itā€™s been more fluid, so I wanted to make a single automation and have it trigger based on an input select of work locations. The sensor changes to true when I should be ready to head out the door. That works well.

- alias: "Notify Me when its time to leave for work"
  trigger:
  - platform: template
    value_template: >
      {{ is_state('sensor.time_to_leave_for_work1', 'True') and
         is_state('input_select.work_location', 'Work1')  }}
  - platform: template
    value_template: >
      {{ is_state('sensor.time_to_leave_for_work2', 'True') and
         is_state('input_select.work_location', 'Work2')  }}
  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: >-
      {% if states.input_select.work_location == 'Work1' %}
        tts_msg: "Work1d on current traffic, you have 10 minutes to leave if you want to get to the work1 by 8:30am.  Traffic is {{ states('sensor.me_traffic_density_to_work1') }} today."
      {% elif states.input_select.work_location == 'Work2' %}
        tts_msg: "Work1d on current traffic, you have 10 minutes to leave if you want to get to khaki by 8:30am.  Traffic is {{ states('sensor.me_traffic_density_to_work2') }} today."
      {% else %}
        tts_msg: "I'm not sure where you're going today"
      {% endif %}
    mplayer: "main"

I believe that should work, assuming the time_to_leaveā€¦ sensors change to something other than 'True' at some reasonable point.

But, you could probably do this in one template trigger:

  platform: template
  value_template: >
    {{ is_state('sensor.time_to_leave_for_work1', 'True') and
       is_state('input_select.work_location', 'Work1')  or
       is_state('sensor.time_to_leave_for_work2', 'True') and
       is_state('input_select.work_location', 'Work2')  }}

Awesome, much cleaner. Yes, basically at midnight they turn false, and then at some point in the morning, based on traffic and time, they turn to true. Iā€™m glad to see Iā€™m getting it ā€œrightā€ the first time more often. :slight_smile:

EDIT: One quick thought/question. When I get home from work, the value is still true. If I change my work location/input select, to where Iā€™m working the next day, it would trigger wouldnt it. Might need to adjust my sensor to turn back to false earlier.

Edit2: Added this condition, which puts the time past midnight, where the sensor would be false. Basically an easy way to ensure it only runs once a day.

  - condition: template
    value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.notify_me_when_its_time_to_leave_for_work.last_triggered) | int > 64800 }}' 

A template trigger will only ā€œfireā€ when it goes from False to True. It wonā€™t trigger, even if a referenced entity changes, if it goes from True to True. So, when you change the input_select, if the overall expression is still True, it wonā€™t trigger. And if it goes to False, then obviously it wonā€™t trigger either. It has to go to False and then back to True to trigger again.

Wouldnā€™t changing the input select change it from FALSE to TRUE? The sensor would be true, but the input select would be false for one for the other trigger. At any given time only one of the conditions can be true, but both the sensors would be true.

Edit: Nevermind I get it. There is only One Trigger. Itā€™s true if either one is true, and only false when both are false. :slight_smile:

1 Like

What am I doing wrong here. Error Invalid config for [automation]: expected a dictionary for dictionary value

action:
  - service: script.audio_notify
    data_template: >-
      {% if states.input_select.work_location == 'Work1' %}
        tts_msg: "Work1d on current traffic, you have 10 minutes to leave if you want to get to the work1 by 8:30am.  Traffic is {{ states('sensor.me_traffic_density_to_work1') }} today."
      {% elif states.input_select.work_location == 'Work2' %}
        tts_msg: "Work1d on current traffic, you have 10 minutes to leave if you want to get to khaki by 8:30am.  Traffic is {{ states('sensor.me_traffic_density_to_work2') }} today."
      {% else %}
        tts_msg: "I'm not sure where you're going today"
      {% endif %}
    mplayer: "main"

here is an example if a working 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.me_traffic_density_to_work') }} today."
      mplayer: "main"

Check the indentation of mplayer in the first one. It needs to be indented under data_template, like in the second one.

Still getting this, Iā€™m sure its a typo, but cannot see it
Invalid config for [automation]: expected a dictionary for dictionary value @ data[ā€˜actionā€™][0][ā€˜data_templateā€™]. Got None.
it passes as a valid template in the template section of the UI

  - service: script.audio_notify
    data_template: >-
      {% if states.input_select.work_location == 'work' %}
        tts_msg: "Based on current traffic, you have 10 minutes to leave if you want to get to the work by 8:30am.  Traffic is {{ states('sensor.pauls_traffic_density_to_work') }} today."
      {% elif states.input_select.work_location == 'Work2' %}
        tts_msg: "Based on current traffic, you have 10 minutes to leave if you want to get to Work2by 8:30am.  Traffic is {{ states('sensor.pauls_traffic_density_to_work2') }} today."
      {% else %}
        tts_msg: "I'm not sure where you're going today"
      {% endif %}
        mplayer: "main"

Oh, sorry, I missed another problem. You canā€™t define YAML parameters conditionally using templates. Templates can only be inside yaml parameters. So:

  - service: script.audio_notify
    data_template:
      tts_msg: >-
        {% if states.input_select.work_location == 'Base' %}
          Based on current traffic, you have 10 minutes to leave if you want to get to the Navy Base by 8:30am.  Traffic is {{ states('sensor.pauls_traffic_density_to_base') }} today.
        {% elif states.input_select.work_location == 'CACI' %}
          Based on current traffic, you have 10 minutes to leave if you want to get to khaki by 8:30am.  Traffic is {{ states('sensor.pauls_traffic_density_to_caci') }} today.
        {% else %}
          I'm not sure where you're going today
        {% endif %}
      mplayer: "main"
1 Like

oh yeah, that makes sense, thanks for the tips.

Well that helped the automation to be valid. This morning it triggered, but it said ā€œIā€™m not sure where youā€™re going todayā€. Iā€™m 100% sure that the input select is correct, is my if else logic wrong??

Um, ok, thatā€™s another problem I didnā€™t notice. :sweat_smile: Try this:

  - service: script.audio_notify
    data_template:
      tts_msg: >-
        {% if states('input_select.work_location') == 'Base' %}
          Based on current traffic, you have 10 minutes to leave if you want to get to the Navy Base by 8:30am.  Traffic is {{ states('sensor.pauls_traffic_density_to_base') }} today.
        {% elif states('input_select.work_location') == 'CACI' %}
          Based on current traffic, you have 10 minutes to leave if you want to get to khaki by 8:30am.  Traffic is {{ states('sensor.pauls_traffic_density_to_caci') }} today.
        {% else %}
          I'm not sure where you're going today
        {% endif %}
      mplayer: "main"
1 Like