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" }}'
1 Like

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…

2 Likes

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:

3 Likes

I came across this post when looking for a solution to my requirement.

I want to setup a list of month/days on which to trigger an event, in this case it is the refuse disposal schedule for a 1 year period. The dates are NOT always previous date + 7 days, so I was thinking to use an array something like this:

[[1,7,22],[1,14,22],[1,20,22],[1,28,22],[2,5,22],[2,12,22],.........[12,27,22]

Is this the best way of doing this, and if so how would I go about coding it, given that this is a ‘pseudo’ array ?

Where do you get the dates from to begin with.
Perhaps there is a way to skip the manual work completely?

But that could work, only thing I would add is an empty array at the beginning to make january [1] not [0].
Another option which will be easier to code in HA would be if the format is [“2023-01-01”, “2023-01-07”, … “2023-03-20”…]

I wanted to have an Automation that ran just once to update my Electricity tariffs. From the advice above, this is what I ended up with.

alias: Electricity Rate Change
description: ""
trigger:
  - platform: time
    at: "00:01:00"
condition:
  - condition: template
    value_template: "{{ now().date() | string == \"2024-04-01\" }}"
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_day_rate
    data:
      value: 0.62726
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_kwh_rate
    data:
      value: 0.23838
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: automation.turn_off
    target:
      entity_id: automation.electricity_rate_change
    data:
      stop_actions: false
mode: single

The Automation will trigger every day at 1 minute past midnight but then not get any further until the date in the Condition Template is reached. It then updates the rates, waits 10 seconds and then disables itself. I can reactivate it when I get notice of the next rate change.
Thanks

Your condition looks fine syntactically, but you can use single quotes within double quotes and avoid the escaping. Of course it’s not 1st April, so what was that string the last time it was supposed to run and what did the trace show? As is, it shouldn’t run.

You will need to update the date when you get a tarrif change, so an easier method would be to use an input date time helper instead of the hard-coded date in the condition.

That’s how it came out when I went from the GUI editor to YAML.

Presumably, you mean it shouldn’t run until 1 Apr 24. I’ve never run it so am waiting till 1 Apr when I will check. If it doesn’t work I’ll come back.

Don’t know what you’re getting at here. This seems pretty simple to me, I mean, I can understand it! Just edit the date and values in the automation and re-enable it.

If the goal is only allow it to execute its actions on April 1st every year, you can do it like this (without the need to manually modify the year annually).

condition:
  - condition: template
    value_template: "{{ (now().month, now().day) == (4, 1) }}"

No, the tarrifs can change anytime and several times a year. I just wanted a way so I could ensure they got updated at the right time without me having to remember. With the Automation, I can just go in and edit it (re-enable, date and new tarrifs) as soon as I get a notification.

In that case I agree with parautenbach that exposing this date in the UI, as an Input Datetime, can improve visibility and convenience. FWIW, I created a “configuration page” in the UI where several Input Datetimes and Input Booleans are used to control the behavior of various automations. I find it easier to manage centralized controls.

Nonetheless, it’s your choice as to what you find more efficient for your needs.

1 Like

I think I see what you both are getting at. I could create a panel on a dashboard for entering the new tariffs and the date they start. I like a challenge so will give it a go.
At the end of the day though, I’m normally sitting at my computer with the email telling me of an impending rate change rather than standing in front of my wall mounted panel in the hall.

1 Like