Trigger fires every time, no matter what the value is

I’m trying to make HA alert me about having to take out the trash. I did this by integrating a Google calendar and sending notifications to Google Hangout, which, all by itself, works fine.

Only problem is, the trigger sends me notifications no matter what the value_template is.

This is the automation:

- alias: Gelber Sack
  trigger:
    - platform: template
      value_template: '{{states.sensor.date.state == strptime(states.calendar.gelbersack.attributes.start_time, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")}}'
    - platform: time
      at: '06:00:00'
  action:
    - service: notify.me
      data:
        message: "Gelben Sack rausstellen!"

I checked the different parts in the template editor:

{{states.sensor.date.state}}
{{states.calendar.gelbersack.attributes.start_time}}
{{states.sensor.date.state == strptime(states.calendar.gelbersack.attributes.start_time, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")}}

Those are the results:

2018-09-17
2018-10-08 00:00:00
False

As you can see, the dates differ and the value_template is evaluated to “False”, which is correct.

I even have a second calendar:

- alias: Restmüll
  trigger:
    - platform: template
      value_template: '{{states.sensor.date.state == strptime(states.calendar.restmuell.attributes.start_time, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")}}'
    - platform: time
      at: '06:00:00'
  action:
    - service: notify.me
      data:
        message: "Restmüll rausstellen!"

Same debugging tests:

{{states.sensor.date.state}}
{{states.calendar.restmuell.attributes.start_time}}
{{states.sensor.date.state == strptime(states.calendar.restmuell.attributes.start_time, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")}}

Results:

2018-09-17
2018-09-17 00:00:00
True

As you can see, in case 1 the dates differ, 2018-09-17 != 2018-10-08, and the result is “False”.
In case 2 the dates match, 2018-09-17 == 2018-09-17, and the result is “True”.

Still I get sent notifications for both triggers every time.

Did I make a mistake somewhere? I can’t wrap my head around this. Is it the use of platform: template AND platform: time which makes the trigger fire every time? Do I need to add logical AND/OR conditions?

When you say that the notification is triggered “every time” - what do you mean by that? Every minute, every hour, every day, …?
The time trigger you defined is definitely going to fire every day at 6am.

I mean everyday at 6 AM.

Today I would have expected to get a notification for the “Restmüll”-trigger since that is a date set in the calendar. Yesterday I shouldn’t have gotten any notification, and I shouldn’t get any notifications tomorrow. Instead I got both trigger notifications yesterday and today, and probably tomorrow, too.

OK… Automation Trigger - Home Assistant

MULTIPLE TRIGGERS

When your want your automation rule to have multiple triggers, just prefix the first line of each trigger with a dash (-) and indent the next lines accordingly. Whenever one of the triggers fires, your rule is executed.

So I’ve got to find a different way to send me a notification at a precise time in the morning.

Try a condition for the first trigger currently.

Right now, your automation will fire:

  • if your value_template logic returns true
  • OR if it is 6:00:00

It looks like you want it to fire only when both are true. You can accomplish this by putting one of those in a condition instead of the trigger.
In words: "at 6:00:00, send a notification only if I need to put out the trash
In yaml:

- alias: Gelber Sack
  trigger:
    - platform: time
      at: '06:00:00'
  condition:
    condition: template
    value_template: '{{states.sensor.date.state == strptime(states.calendar.gelbersack.attributes.start_time, "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d")}}'
  action:
    - service: notify.me
      data:
        message: "Gelben Sack rausstellen!"
1 Like

@vloris Thanks a lot, this does exactly what I had in mind. Just tested it, now it works.