Trigger an Action on Specific Date & Time

I’ve created Date & Time sensor already (named sensor.date__time) and I want to try to set an automation to trigger at a specific date and time is this trigger correct? I’m still pretty new to this and feel like it might be right but wanted to make sure I’m on the right path. (in my example I’m intending to have it trigger tomorrow at 11:21)

- alias: 'Test reminder'
  trigger:
    platform: template
    value_template: "{% if is_state('sensor.date__time', '2017-03-11, 11:21') %}true{% endif %}"
  action:
    - service: notify.Pushetta_1
      data:
        message: "TEST"

Yeah that is pretty much it BUT this will trigger A LOT on the 11th of march on 11:21 because this automation gets checked for EVERY state change within HA. So every state change would check your template in this very minute and trigger the automation.

To do it only once I would suggest using another trigger…like this one: https://home-assistant.io/docs/automation/trigger/#time-trigger

But take note of the warning box “Remember that if you are using matching to include both minutes and seconds. Without seconds, your automation will trigger 60 times during the matching minute.”

~Cheers

so maybe something like this would work?

  trigger:
    platform: time
    hours: 16
    minutes: 0
    seconds: 0
  condition:
    condition: template
    value_template: '{{ states.sensor.date.state == "2017-03-11" }}'

I would prefer “now().date()” instead of “states.sensor.date.state” but that’s just me, it should work like that just fine.

~Cheers

I’ve looked and looked and couldn’t find any examples of the “now().date()” you mention… so I just want to be sure something like this looks right to you (thanks again for all of your help)

  trigger:
    platform: time
    hours: 9
    minutes: 0
    seconds: 0
  condition:
    condition: template
    value_template: '{{ now().date() == "2017-03-12" }}'

I got the “now()” from here: https://home-assistant.io/docs/configuration/templating/
You can test your templates in the Web-UI of HA btw. (the value_template). For this you go to your HA website, open the navigation tab to the left, on the bottom there are 5 icons. The 5th says “templates”. Click there and you get to a site where you can test 'em :slight_smile:

~Cheers

Is the code above accurate? I am trying to do this exact same thing and it is not working.

I used the “templates” section, nice to know about. And the below condition shows false? any idea what to change?

condition:
condition: template
value_template: ‘{{ now().date() == “2017-05-11” }}’

And all my spaces are correct, I am not getting any errors. Just that the value_template is false.

now().date() returns a date, “2017-05-11” a string.
Convert your date to a string and it works.

{{ now().date() | string == "2017-05-11" }}

2 Likes

Perfect, thanks!!!

What about this? And why not this if the previous string conversion example also works?

{{ now().strftime("%Y-%m-%d") == "2017-11-01" }}

One seems to be a filter and the other a function? I don’t know this stuff much…

1 Like

I like this idea - will hopefully allow me to do something like this:

{{ now().strftime("%m-%d") == "10-03" }}

So I can run an action on the same time, same day every year :smile:

2 Likes