Automation on a particular day

Hi,

I am trying to configure an automation to run only on a particular day. I am using the automation wizard, but I cannot see an option under conditions to specify a day.

Am I doing something wrong?

Regards

James

I have this for example not vreated via the wizard but as a yaml automation it might help.

- alias: 'Happy Valentines Day'
  initial_state: True
  trigger:
      - platform: template
        value_template: >-
          {{ now().strftime('%F') == (now().strftime('%Y') | int ) ~ "-02-14" and now().strftime("%R") == "06:00" }}
 
  action:
    service: notify.family_email
    data:
      title: "Happy Valentines Day"
      message: "Happy Valentines Day {{now().year}} Love Jarvis"
      data:
          images:
              - /config/www/images/valentines_day.jpg

I thought using a time trigger in a template wasn’t recommended as it would check the template each second?

Also, does that trigger?

I thought it wouldn’t based on the above link as there is no entity ID to force an update.

If it does work I can simplify some automations in my setup.

Yes it works fine or did on Feb 14th I only wrote it to wind my daughter up.

@daveyrb, I would agree with @silvrr, this shouldn’t trigger as ‘now()’ is not a monitored state.
And as he mentions you would be better in getting the code more economical with processing.
I’d go for a daily trigger at 06:00 and then check the date in the condition

Are you sure you don’t have an entity id of (say) sensor time in there ?

With a particular day do you mean every Monday for example or a specific date, like every year on 14.09?

Is valentines day always on a Monday in Switzerland?

Edit : Ah sorry you meant the OP

I agree it could be more efficient but it was only to tease my daughter. I thought it would provide a pointer and if it can be improved upon great.
it does work and trigger fine I just wrote this as a test and it worked as expected:

- alias: 'Test Automation'
  initial_state: True
  trigger:
      - platform: template
        value_template: >-
          {{ now().strftime('%F') == (now().strftime('%Y') | int ) ~ "-03-12" and now().strftime("%R") == "12:35" }}
 
  action:
    service: notify.ralph_email
    data:
      title: "Test Automation"
      message: 'This is a test automation'

Well, I took your automatation and modified it slightly to fit into my prexisting variables to fire an action : -

automation:
  - alias: 'Test Automation'
    trigger:
      - platform: template
        value_template: >-
          {{ now().strftime('%F') == (now().strftime('%Y') | int ) ~ "-03-12" and now().strftime("%R") == "13:59" }}
    action:
      - service: input_boolean.toggle
        entity_id: input_boolean.ib_global_test

It did not trigger the boolean.I checked this twice and, the second time it DID fire. I owe you an apology.
I can not explain how this works as everything I know about templates says this should not work.
I will tag @123 and @petro because if they don’t have an explanation then I will need to check if the world turns anti-clockwise :scream_cat:

Thanks for the replies, so for a condition for the automation to only work on a Friday the code would be

platform: template
        value_template: >-
          {{ now().strftime('%a') == "Fri" }}
 

Is this correct?

Regards
James

I believe your testing procedure is misleading you to believe it works.

The fact is your template contains no identifiable entities for Home Assistant to monitor for state-changes. That template will be evaluated once when Home Assistant starts and then never again (i.e. not until the next time Home Assistant is restarted).

So if you create the Template Sensor then restart Home Assistant to load it, the template is evaluated on startup and you’re misled to believe “it works”.

Similarly, if Home Assistant is restarted at any time, the Template Sensor is evaluated and, once again, it appears like “it works”.

This behavior is documented here:
Template without entities using now()

I would create an automation that triggers everyday at the same time and then add a time condition to only fire the automation on Friday.

3 Likes

Ok that makes sense.
Sory had a phone call and hit post.
I created an automation for her birthday as well but of cource HA would have been restarted as they are both once a year events.

Here’s a simple and flexible way to do it.

First, create a Template Binary Sensor for each special event in your life: :slight_smile:

#binary_sensor:
  - platform: template
    sensors:
      mary_birthday:
        friendly_name: "Mary's Birthday"
        entity_id: sensor.date
        value_template: '{{ now().month == 7 and now().day == 17 }}'
      john_birthday:
        friendly_name: "John's Birthday"
        entity_id: sensor.date
        value_template: '{{ now().month == 9 and now().day == 28 }}'
      wedding_anniversary:
        friendly_name: "Our Wedding Anniversary"
        entity_id: sensor.date
        value_template: '{{ now().month == 9 and now().day == 28 }}'
      monthly_reminder:
        friendly_name: "It's the second day of the month"
        entity_id: sensor.date
        value_template: '{{ now().day == 2 }}' 

Then create an automation that is triggered by the desired special event.


EDIT

Here’s an automation that will report (at 07:15) any events occurring that day. It requires that you create a group containing all of the “special events” binary_sensors.

group:
  all_events:
    name: All Events
    entities:
      - binary_sensor.mary_birthday
      - binary_sensor.john_birthday
      - binary_sensor.wedding_anniversary
      - binary_sensor.monthly_reminder
automation:
- alias: "special events"
  trigger:
    platform: time
    at: '07:15:00'
  condition:
    condition: template
    value_template: "{{ states('group.all_events') == 'on' }}"
  action:
    service: notify.notify
    data_template:
      title: Special events today
      message: >
        {% set events = expand('group.all_events') | selectattr('state','eq', 'on') | map(attribute='attributes.friendly_name') | list %}
        {% set total = events | count %}
        {% set x = 'are {} events'.format(total) if total > 1 or total == 0 else 'is one event' %}
        {{'Good morning! There {} today: {}.'.format(x, events | join(", and ")) }}

Testing results from the Template Editor:

2 Likes

If you just need it on a DAY (as @Burningstone pointed out and we were all blind to it :hot_face: )
You would be better to use the built in day function. (slightly less processor intensive as it does not have to format the template, pass it to the sandbox and get a result (though it probably does something very similar)
Yours will trigger (if it does trigger on now() which @daveyrb 's recent efforts suggest it might) at 00:00

So to clarify and expand on burning’s suggestion : -
(these are direct from the documentation)

automation:
  trigger:
    platform: time
    # Military time format. This trigger will fire at 3:32 PM
    at: "15:32:00"
  condition:
    condition: time
    # At least one of the following is required.
    weekday:
      - mon
      - wed
      - fri
1 Like

I’m not sure if the requirement is to trigger on a specific day-of-the-year or day-of-the-week. If it’s the latter then your example (using the time condition) is the way to do it.

Apologies, the thread appears to have split into DATE solutions and DAY solutions.
I (based on what I know todate) would fully agree with you and your contentions/suggestions BUT …

The template is evaluated to a True/False state.
And I agree that a restart will evaluate such vales at the start, similarly if you re-evaluate them given a manual reload of automations.

I ran the test with a time 2 or three minutes in the future, then reloaded automations.
The test boolean remained off
I waited the 2 or 3 minutes and the boolean came ON when it reached the designated time

So what does that say about the evaluation ? Does it imply that the state it stored is retentive of the time it is to return True ???

I also found an older post (so may now be way off base) : -

where cgtobi suggested the use of : -

trigger:
  platform: template
  value_template: "{% if now().strftime("%d") == '9' %}True{% endif%}"

(admittedly a long winded way of saying it … )
I’m thinking of re-writing the automation to trigger if the minute ends in 0,2,4,6 or 8 and see what happens to the toggle
I have to say that I expected this to “Just NOT WORK” but … my expeiments have left me in doubt.

Template trigger

Template triggers work by evaluating a template on every state change for all of the recognized entities. The trigger will fire if the state change caused the template to render ‘true’.

If you’re saying that you’ve created a Template Trigger with no entities, just functions, and it gets triggered, then it contradicts all existing documentation.

I don’t have time to explore this but if you believe you’ve found some sort of exception to the rule, you may wish to post it as an Issue in the Core repo.

That’s not a good trigger.

value_template: "{{ now().day == 9 }}"