Automation not triggered by time pattern

Hello there!

I wrote a quite complex automation to run my robot vacuum and tested it by manually triggering the automation during development. The trigger is a really simple one but it won’t trigger at all.
So i I’ve got a few questions regarding the “triggering”:

  1. Does triggering an automation by hand skip the condition-checks?
  2. Does the “last triggered” value in the automations-overview show only “triggered” or “triggered and passed conditions”?
  3. Do you see any problems in my following automation causing it to not getting triggered?
alias: Auto-cleaning
description: >-
  Clean room while nobody is home
trigger:
  - platform: time_pattern
    minutes: /16
condition:
  - condition: time
    after: '08:00:00'
    before: '20:00:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  - condition: not
    conditions:
      - condition: state
        entity_id: group.residents
        state: home
  - condition: template
    value_template: >-
      '{{ states('sensor.date') > states('input_datetime.livingroom_cleaned')
      }}'
  - condition: template
    value_template: '{{ not is_state(''vacuum.borris'', ''cleaning'') }}'
action:
  - service: xiaomi_miio.vacuum_clean_segment
    data:
      segments:
        - 16
    entity_id: vacuum.borris
  - wait_template: '{{ is_state(''vacuum.borris'', ''cleaning'') }}'
    timeout: '00:00:10'
    continue_on_timeout: true
  - repeat:
      while:
        - condition: template
          value_template: '{{ is_state(''vacuum.borris'', ''cleaning'') }}'
      sequence:
        - service: >-
            {% if is_state('group.residents' , 'home') %} script.dockVacuum {%
            else %} script.waitOne {% endif %}
  - condition: not
    conditions:
      - condition: state
        entity_id: group.residents
        state: home
  - service: input_datetime.set_datetime
    data_template:
      date: '{{ states(''sensor.date'') }}'
    entity_id: input_datetime.livingroom_cleaned
mode: single

Additionally I tried some things in the development-tools to verify my conditions:

{{ states('input_datetime.livingroom_cleaned') }}

2020-12-20

{{ states('sensor.date') }}

2020-12-22

{{ states('sensor.date') > states('input_datetime.livingroom_cleaned') }}

True

{{ states('group.residents') }}

not_home

I hope this is not too complicated or worse - too easy.
All suggestions and improvements are welcome.

Thanks in advance!

The conditions are normally skipped. When you trigger the automation from the dev-tools you can set “skip_condition” to false.

The last triggered should be set when the conditions has been passed.

Your questions, concerning the behavior of an automation that is manually triggered, are answered in the documentation:

Troubleshooting Automations - Testing your automation

The following Time Pattern Trigger means you want it to trigger every 16 minutes within any given hour. That means 3 times per hour (60 minutes divided by 16 minutes).

Hi, @Rapsbeere - I don’t see anything apparently wrong in your logic.

A couple of things look a little out of place. The first is the use of double single quotes in:

I tested that out in the developer template section and it throws an error. I suggest changing those to single double quotes as follows:

{{ not is_state("vacuum.borris", "cleaning") }}'

The second thing is the use of the single quote around the entire template in:

The rules on using templates are:

  • If the template is in-line as in:
value_template: '{{states("sensor.xxx")}}'

Always include surrounding quotes - I used single quotes in the above example. Then always use different type of quotes inside the template (I used double quotes).

  • If the template is multiline, as you have written, then do NOT include the surrounding quotes. Otherwise it evaluates to the literal string that is inside the surrounding single quotes, and that string is not True (and then doesn’t pass the condition).

In your case, the comparison logic for the dates should be (no surrounding quotes):

  - condition: template
    value_template: >-
      {{ states('sensor.date') > states('input_datetime.livingroom_cleaned')
      }}

This could be re-written inline as (surrounding single quote and single double quotes inside the template):

  - condition: template
    value_template: '{{ states("sensor.date") > states("input_datetime.livingroom_cleaned")'
      }}

I did check this on the Configuration/Server Controls/CHECK CONFIGURATION page. It doesn’t throw an error (which is a little concerning). Removing the surrounding single quotes passes the condition test and continues to the action statements. Note the use of the different quote types.

The other suggestion is to add some logging statements within the action section of the automation. These look like this:

action:
- service: system_log.write
  data_template:
    message: >
      {% if trigger.platform == "time_pattern" %} Action triggered by time_pattern
      {% else %}                                  Action triggered by something else
      {% endif %}
    level: warning

I sprinkle these all over the place when I’m developing a somewhat complex automation - it helps figure out what’s really happening.

I created a test version of your automation to test the time_pattern trigger and the quote changes suggestged above. The logger result is (I adjusted the time pattern to trigger every 5 seconds for testing):

2020-12-22 09:01:15 WARNING (MainThread) [homeassistant.components.system_log.external] Action triggered by time_pattern
2020-12-22 09:01:20 WARNING (MainThread) [homeassistant.components.system_log.external] Action triggered by time_pattern
2020-12-22 09:01:25 WARNING (MainThread) [homeassistant.components.system_log.external] Action triggered by time_pattern
2020-12-22 09:01:30 WARNING (MainThread) [homeassistant.components.system_log.external] Action triggered by time_pattern
2020-12-22 09:01:35 WARNING (MainThread) [homeassistant.components.system_log.external] Action triggered by time_pattern

So your trigger is good and so are the conditions that I can test. You can put all kinds of things here to help debug what’s happening (including {{trigger.to_state}}, etc.

There’s a good templating section that explains this. It also explains the use of the double and single quotes really well. It’s at: Automation Templating - Home Assistant (home-assistant.io)

For reference, here’s the complete working automation that I used to test things out (note that I commented out some things because I didn’t have them:

- alias: Auto-cleaning-test
  description: >-
    Test cleaning automation script
  trigger:
    - platform: time_pattern
      seconds: /5
  condition:
    - condition: time
      after: '08:00:00'
      before: '20:00:00'
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  # - condition: not
  #   conditions:
  #     - condition: state
  #       entity_id: group.residents
  #       state: home
    - condition: template
      value_template: >
        {{ states('sensor.date') > states('input_datetime.test_input_datetime')
        }}
    - condition: template
      value_template: '{{ not is_state("vacuum.borris", "cleaning") }}'
  action:
  - service: system_log.write
    data_template:
      message: >
        {% if trigger.platform == "time_pattern" %} Action triggered by time_pattern
        {% else %}                                  Action triggered by something else
        {% endif %}
      level: warning
1 Like

That’s valid and is what the Automation Editor normally uses. The first single quote serves to escape the meaning of the second single quote. Personally I prefer to use double-quotes, like you suggested, but two consecutive single-quotes are also valid.

Your other observation, about the incorrect use of inner and outer quotes, is spot on.

Thanks, @123.I hadn’t seen this before so tested it in the developer template. It resulted in:


(not sure the image is going to show up…). But when I add {{states(''sensor.date'')}} to the template editor, it throws this error:
TemplateSyntaxError: expected token ',', got 'sensor'

If I remove the extra single quotes, it works fine. Thoughts? Maybe it only works that way when it’s inside another set of quotes in the in-line template version? Only asking to understand better - I’ll likely stick to my habit of single quotes on the outside, double quote on the inside. Or, after falling into this trap numerous times, I’ve started to add templates as if they were always multiline… even when they are only one line…

Oh boys! (or girls)

The quotation is the thing. I use the GUI to edit my automations and this is creating my issues. My double single-quotes are generated if I use single-quotes to describe my template. They get converted. And I never thought about using double-quotes as there was no warning shown.
The same thing is the reason for the multi-line templates. Apparently they are just to long for the GUI and it splits it.
From now on I will check the quotes every time I save the automation.

The information about the use of developer-tools to trigger automations instead of using the GUI was also a huge help! Thanks @dennis84de

Love this community! See you again soon!

1 Like

That’s a Jinja2 interpreter, not a YAML processor.

See:

The Automation Editor doesn’t create your issues; just let it take care of adding outer quotes.

Not exactly. The Automation Editor reformats it for other reasons and not specifically that it’s “too long”. It’s more of a convention as opposed to a requirement.

Just be aware that it’s a Jinja2 interpreter and not a YAML processor.

Got it - I should have realized that… :wink:

Glad it worked out!

I’ve changed my MO to always use the multiline version of a template. That way I don’t have to remember about the surrounding quotes. I’ve been nailed by that mistake more than a few times…