Automation on a particular day

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 }}"

Well,
I changed the test automation to clean in up, take less space, sort the quotes etc. and toggle every 2 minutes, ended up with : -

automation:
  - alias: Now Test Automation
    trigger:
      - platform: template
        value_template:  "{{ now().strftime('%F') == (now().strftime('%Y') | int ) ~ '-03-12' and now().strftime('%R') [4:5] | int in [0,2,4,6,8] }}"
    action:
      - service: input_boolean.toggle
        entity_id: input_boolean.ib_global_test

Without any further intervention from me this now toggles the boolean every two minutes - This Should Not Work ! But it does !

Admittedly I just hate this, I wouldn’t rely on it cos it may just be that someone will find the error and correct it so it won’t work next week !
Interestingly my processor usage and average load has not budged so It’s not even consuming much resources. WEIRD !

Yep which is why I said : -

It’s cgtobi 's template from the post quoted

Thanks for the replies I used the following code.

  condition:
    condition: time
  # At least one of the following is required.
    weekday:
      - fri

Regards
James

We should be thanking both you and @daveyrb, this has highlighted a potentially serious issue :+1:

It’s not long winded… it’s just wrong. It’s omitting a return value for false.

1 Like

Point Taken

I just looked at it, saw it was trying to give a True/False but had evaluated the necessary bit, didn’t see the point in the rest and discontinued reading.
My Bad - Thanks for pointing it out

Results of my experiment:

I created four automations, each with one of the following Template Triggers:

      value_template: "{{ now().minute % 2 == 0 }}"
      value_template: "{{ now().minute >= 0 }}"
      value_template: "{{ now().minute - 1 + 1 >= 0 }"
      value_template: "{{ now().strftime('%F') == now().strftime('%Y') ~ '-03-12' and now().minute >= 0 }}"
  • All four templates have no identifiable entities and all rely on the now() function.
  • Each automation’s action simply toggled an input_boolean.
  • Only the automation using the first template worked (i.e. it toggled the input_boolean on even minutes).

It’s an interesting result and, on the face of it, challenges the documentation’s broad assertion that the template must contain an identifiable entity and that now() cannot be used as a trigger. However, it’s a very subtle exception to the rule because 3 out of the 4 variations failed to work.

  • The second template relies on now() but doesn’t trigger the automation.
  • The third template does some simple arithmetic but doesn’t trigger the automation.
  • The fourth template adds string concatenation but also fails to trigger the automation.
  • The first one is almost the same as the second one but performs integer division and that seems to make a difference. In the case of the first template, Home Assistant appears to have assigned a listener to the automation’s trigger. :thinking:

For anyone who wishes to try this for themselves:

- alias: 'Toggle every even minute'
  trigger:
    - platform: template
      value_template: "{{ now().minute % 2 == 0 }}"
  action:
    - service: input_boolean.toggle
      entity_id: input_boolean.flash

Screenshot from 2020-03-12 12-42-01

I have submitted the issue on github but your tests are ‘interesting’ I’ll edit my post to add (a link) to your analysis :+1:

looking at your templates, they ALL seem to ALWAYS evaluate to True apart from the first

And @daveyrb 's uses the time to evaluate, not just the minute, mine takes the time and strips to the last ‘unit value’ of minute as well (so mines well less efficient aswell).

Only the first is capable of returning false but the template has to be evaluated in order to determine that. What’s causing the template to be evaluated? It would be a listener assigned to monitor it. That listener appears to be evaluating it when now().minute changes value. So why aren’t the other three templates being handled the same way? I don’t know the answer to that.

it’s does not use what’s already there (days) and therefore is slower/worse - that’s true.
but I think there is nothing wrong in not returning False - the docs say only about True and anything else (including None which is most likely what it gets) is interpreted as “not True” i.e “False”.

Seems that long nights are not so good :frowning:
Tried example by Taras123 in post 31- all works but how to reset the binary sensor after expire- 0:00 next day? Now it remains active despite date change.
TIA and best, JR
And another q: how to find out e.g. 3rd Sunday in May in template?