Automation doesn't trigger / manual triggering works

I’m trying to get an automation running, but can’t figure out, what I’m doing wrong. It triggers, if I call it from the dev-panel, but not automatically. Config check says everything ok, restarted Hass.io, automation is ‘on’…

I’m stuck, that’s not surprising, I’m new to HA… :expressionless:

Here’s the automation:

alias: Aufwachen
trigger:
  platform: time_pattern
  minutes: /1
  seconds: 0
condition:
  - condition: template
    value_template: "{{ states('input_boolean.alarm_clock_ha_on') == 'on' }}"
  - condition: template
    value_template: "{{ states('input_boolean.night_mode') == 'on' }}"
  - condition: template
    value_template: "{{ states('sensor.time') >= '22:30' | timestamp_custom('%H:%M', true) or states('sensor.time') <= '12:30' | timestamp_custom('%H:%M', true) }}"
  - condition: template
    value_template: >
      {% if 'states.input_datetime.alarm_clock_pat_time.attributes.timestamp' is le 'states.input_datetime.alarm_clock_steffi_time.attributes.timestamp' %}
        {{ ((as_timestamp(now()) + 300) | timestamp_custom('%H:%M:%S', true)) == 'states.input_datetime.alarm_clock_pat_time' }}
      {% else %}
        {{ ((as_timestamp(now()) + 300) | timestamp_custom('%H:%M:%S', true)) == 'states.input_datetime.alarm_clock_steffi_time' }}
      {% endif %}
action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.night_mode

Thanks a lot for any assistance! :slight_smile:

Triggering the automation from the dev tools services panel skips the conditions.

Put the condition templates into the dev tools template editor and work out which one is not evaluating to true.

Thanks for the help, I didn’t know that conditions are not triggered via the dev-panel.

But I think, I already found the error. After sending the first post, I noticed something weird in the code coloring. I had an " at the end of the conditions.

I changed the first post…

Thanks

The time_pattern looks wrong.
What are you trying to do with that?

Try this:

  minutes: '/1'

and remove this:

  seconds: 0

In addition, go to the States page, find the automation in the list, and confirm it is on.

What I’m trying to achieve is:

  • Alarm Clock in HA with two timers
  • set a time for the alarm_clock from mobile to HA api
  • this sets the input_datetime.alarm_clock_pat_time
  • if alarm in HA is ‘on’ and night_mode is ‘on’ and it is between 22:30 and 12:30
  • then check which alarm is earlier and start the automation five minutes before this alarm

If I understand correctly, the time_pattern should scan every minute, if the alarm_time minus five minutes is reached. Is that the wrong way doing this? Do you have an alternative idea?

Have you tested this template in the Template Editor?

    value_template: "{{ states('sensor.time') >= '22:30' | timestamp_custom('%H:%M', true) or states('sensor.time') <= '12:30' | timestamp_custom('%H:%M', true) }}"

Because I doubt it works.

I thought just minutes:1 would scan every minute.
That is pretty aggressive scanning, though.

Jepp, tested (before and right now, just to check):


I also checked the other conditions (each for itself), work as well with desired outcome.

That’s what I thought, if you’d have a better idea, please bring it up. :slight_smile: I read this solution here in the forum in a thread about alarm clocks. And on the other hand, the time for the alarm can change minutewise, so how do I check that “smart”?

I am almost certain they do not work the way you think they do.

I don’t know why you included this filter:

timestamp_custom('%H:%M', true)

because it’s not converting sensor.time into anything.

The result of this:

states('sensor.time') >= '22:30')

is True or False. With or without timestamp_custom, the result is the same:

I suggest you remove the timestamp_custom filters to improve the template’s legibility.

Finally, you probably already know, but for others who don’t, this is not a numeric comparison:

states('sensor.time') >= '22:30')

The value contained by sensor.time is a string. '22:30' is also a string. So the test is a string comparison (like 'cat' >= 'bat' is true).

The only reason why it works is because number characters happen to be in ascending order. Comparisons work but time arithmetic would not.

You may want to add the seconds: 0 back in so that it fires on the minute. Without the seconds: 0, it will fire at a cycle based on the startup time.

  platform: time_pattern
  minutes: '/1'
  seconds: 0

I believe if seconds is omitted it defaults to seconds: 0.

I ran this automation and restarted Home Assistant twice.

- alias: "time_pattern_test"
  trigger:
  - platform: time_pattern
    minutes: '/1'
  action:
    service: system_log.write
    data_template:
      message: "{{ now().time() }}"
      level: info

The results demonstrate the automation ran every minute on the minute (meaning seconds = 00).

Results of first restart:

2019-05-06 14:52:00 INFO (MainThread) [homeassistant.components.automation] Executing time_pattern_test
2019-05-06 14:52:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Running script
2019-05-06 14:52:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Executing step call service
2019-05-06 14:52:00 INFO (MainThread) [homeassistant.components.system_log.external] 14:52:00.006233
2019-05-06 14:53:00 INFO (MainThread) [homeassistant.components.automation] Executing time_pattern_test
2019-05-06 14:53:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Running script
2019-05-06 14:53:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Executing step call service
2019-05-06 14:53:00 INFO (MainThread) [homeassistant.components.system_log.external] 14:53:00.006489
2019-05-06 14:54:00 INFO (MainThread) [homeassistant.components.automation] Executing time_pattern_test
2019-05-06 14:54:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Running script
2019-05-06 14:54:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Executing step call service
2019-05-06 14:54:00 INFO (MainThread) [homeassistant.components.system_log.external] 14:54:00.007523

Results of second restart:

2019-05-06 14:59:00 INFO (MainThread) [homeassistant.components.automation] Executing time_pattern_test
2019-05-06 14:59:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Running script
2019-05-06 14:59:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Executing step call service
2019-05-06 14:59:00 INFO (MainThread) [homeassistant.components.system_log.external] 14:59:00.005783
2019-05-06 15:00:00 INFO (MainThread) [homeassistant.components.automation] Executing time_pattern_test
2019-05-06 15:00:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Running script
2019-05-06 15:00:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Executing step call service
2019-05-06 15:00:00 INFO (MainThread) [homeassistant.components.system_log.external] 15:00:00.005598
2019-05-06 15:01:00 INFO (MainThread) [homeassistant.components.automation] Executing time_pattern_test
2019-05-06 15:01:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Running script
2019-05-06 15:01:00 INFO (MainThread) [homeassistant.helpers.script] Script time_pattern_test: Executing step call service
2019-05-06 15:01:00 INFO (MainThread) [homeassistant.components.system_log.external] 15:01:00.007680

Could have been updated when the trigger type was changed a while back. So much to keep track of

no. that was changed several versions back.

@123 Thanks a lot for your clarification, I must have misread that.

I was thinking the other way around:
states('sensor.time') is the object, and states.sensor.time is the string represantation. Following that idea, I tried to convert the object with timestamp_custom to a string and compare two strings with true or false.
Or do I understand it wrong, that condition == true means the action is started, condition == false means, the action is not started?

Now that I’ve read your explanation, it makes much more sense! :slight_smile: Thanks a lot! Anyway, this is what I’ve got now, and it seems to work correct:

alias: Aufwachen
trigger:
  platform: time_pattern
  minutes: '/1'
  seconds: '0'
condition:
  - condition: template
    value_template: >
      {% if "state_attr('input_datetime.alarm_clock_pat_time', 'timestamp')" is le "state_attr('input_datetime.alarm_clock_steffi_time', 'timestamp')" %}
        {{ ((states.input_datetime.alarm_clock_pat_time.attributes['timestamp'] - 300) | timestamp_custom('%H:%M', false)) == (states.sensor.time.state) }}
      {% else %}
        {{ ((states.input_datetime.alarm_clock_steffi_time.attributes['timestamp'] - 300) | timestamp_custom('%H:%M', false)) == (states.sensor.time.state) }}
      {% endif %}
  - condition: template
    value_template: "{{ states('sensor.time') >= '22:30' or states('sensor.time') <= '12:30' }}"
  - condition: template
    value_template: "{{ states('input_boolean.alarm_clock_ha_on') == 'on' }}"
  - condition: template
    value_template: "{{ states('input_boolean.night_mode') == 'on' }}"
action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.night_mode

Many thanks to all of you, your answers helped a lot in understanding HA more in depth!

You’re welcome!

BTW, as explained in my previous post, this:

seconds: '0'

is unnecessary because this:

minutes: '/1'

triggers every minute, on the minute (i.e. seconds = 0 ).

I wish to caution users that this convention, with the double-quotes, should be avoided:

{% if "state_attr('input_datetime.alarm_clock_pat_time', 'timestamp')" is le "state_attr('input_datetime.alarm_clock_steffi_time', 'timestamp')" %}

Here’s why:
The timestamp attribute’s value is numeric. Therefore the test for determining which timestamp is less than, or greater than, the other is simply a matter of performing a numeric comparison like so:

{% if state_attr('input_datetime.alarm_clock_pat_time', 'timestamp') <= state_attr('input_datetime.alarm_clock_steffi_time', 'timestamp') %}

However, the example doesn’t do that. It takes the extra step of delimiting the statement with double quotes:

"state_attr('input_datetime.alarm_clock_pat_time', 'timestamp')"

thereby converting timestamp's value to string. As a result, it doesn’t perform a numeric comparison but a string comparison (and there’s no need to do that).

Here’s are examples of string comparisons that all evaluate to True:

{{ 'rat' > 'cat' }}
{{ "" < " " }}
{{ 'something' > 'nothing' }}
{{ "125" > "120" }}
{{ "9:30 am" > "8:30 pm" }}

Although you can compare strings containing number characters, it relies on the fact that number characters are inherently in ascending order.

If the two values are already numeric, there’s no need to convert them to string to perform a comparison (and it’s best that you don’t).

Here are string and numeric comparisons evaluated in the Template Editor:

2 Likes

Thank you very much, for taking again the time to illustrate the way-to-go for me! :slight_smile:

I’ve now taken the time to re-read your posts and came up with this. For now it seems to be working (not that I’d question that):

alias: Aufwachen
trigger:
  platform: time_pattern
  minutes: '/1'
condition:
  - condition: template
    value_template: >
      {% if state_attr('input_datetime.alarm_clock_pat_time', 'timestamp') <= state_attr('input_datetime.alarm_clock_steffi_time', 'timestamp') %}
        {{ ((state_attr('input_datetime.alarm_clock_pat_time', 'timestamp') - 300) | timestamp_custom('%H:%M', false)) == states('sensor.time') }}
      {% else %}
        {{ ((state_attr('input_datetime.alarm_clock_steffi_time', 'timestamp') - 300) | timestamp_custom('%H:%M', false)) == states('sensor.time') }}
      {% endif %}
  - condition: template
    value_template: "{{ states('sensor.time') >= '22:30' or states('sensor.time') <= '12:30' }}"
  - condition: template
    value_template: "{{ states('input_boolean.alarm_clock_ha_on') == 'on' }}"
  - condition: template
    value_template: "{{ states('input_boolean.night_mode') == 'on' }}"
action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.night_mode

@123 What do you say?

I say it looks good.

Just to humor me, can you try this version of the condition and let me know if it works?

condition:
  - condition: template
    value_template: >
      {% set t = states('sensor.time') %}
      {% set pat = state_attr('input_datetime.alarm_clock_pat_time', 'timestamp') %}
      {% set steffi = state_attr('input_datetime.alarm_clock_steffi_time', 'timestamp') %}
      {{ states('input_boolean.alarm_clock_ha_on') == 'on' and 
         states('input_boolean.night_mode') == 'on' and
         (t >= '22:30' or t <= '12:30') and
         (((pat - 300) if pat <= steffi else (steffi - 300)) | timestamp_custom('%H:%M', false) == t) }}

EDIT
Correction. Removed if from {{ if states('input_boolean.alarm_clock_ha_on')