How to set a date and time trigger

hey all,

i have been wanting to set up some automations to help me with reminders and wish to have then trigger at different times on different days, i currently have automations running at set times- using the condition to specify the day, but i was wondering if i could set one automation to trigger on different times and days instead of having each one set to a specific day/set of days?

basically just this but having several as triggers instead of several automations with this as a condition

any help is appreciated!

There are multiple ways to do what you want in a single automation.

One approach would be to use multiple triggers for the different times then use a Choose action to run the desired actions based on the trigger that fired and the day of the week.

It would be much easier to give you a specific answer if you share the configuration of your automations, as specified in the Questions Guidelines.

1 Like

i would like in theory for it to be something along the lines of

–trigger–
friday at 3pm
or
Tuesday at 8pm
or
Wednesday at 10am

–action–
-send a message through Alexa device
-wait 5 minutes (to prevent spam)
loop top two actions until nfc tag scanned- then stop loop and trigger again when any of the above triggers are met

Most efficient way to start would be to use a Schedule helper set up via the UI to represent the day / times that you want; and a state trigger that fires when the schedule entity goes to on.

1 Like

If all the triggers execute the same action sequence, you don’t need a Choose as proffered in my previuos post. You just need to use conditions appropriately. Here is one example of triggering on Time and using conditional logic:

Automation Example
trigger:
  - platform: time
    id: Tuesday
    at:
      - "20:00:00"
  - platform: time
    id: Wednesday
    at:
      - "10:00:00"
  - platform: time
    id: Friday
    at: 
      - "15:00:00"
condition:
  - alias: Check if the Trigger ID matches the current weekday
    condition: template
    value_template: "{{ trigger.id == now().strftime('%A') }}"
action:
  - #YOUR ACTION SEQUENCE

Note: now().strftime('%A') will return the weekday in whatever language your system uses, so make sure the trigger IDs are set to match those.

It is also possible to create a functionally equivalent automation without a Template condition, the condition block is just more verbose.

Verbose Condition Block
trigger:
  - platform: time
    id: Tuesday
    at:
      - "20:00:00"
  - platform: time
    id: Wednesday
    at:
      - "10:00:00"
  - platform: time
    id: Friday
    at: 
      - "15:00:00"
condition:
  - alias: Check if the Trigger ID matches the current weekday
    condition: or
    conditions:
      - and:
          - condition: trigger
            id: Tuesday
          - condition: time
            weekday:
              - tue
      - and:
          - condition: trigger
            id: Wednesday
          - condition: time
            weekday:
              - wed
      - and: 
          - condition: trigger
            id: Friday
          - condition: time
            weekday:
              - fri
action:
  - #YOUR ACTION SEQUENCE
1 Like

something like this?

alias: "NEW CHIN CLEAN CAGE REMINDER "
description: ""
trigger:
  - platform: time
    at: "10:00:00"
  - platform: time
    at: "09:00:00"
  - platform: time
    at: "13:00:00"
  - platform: time
    at: "17:00:00"
  - platform: tag
    tag_id: 4d9c*******
    id: STOP TAG
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            weekday:
              - mon
              - tue
              - sat
              - sun
            after: "10:00:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage mon,tues,sat,sun
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: trigger
                  id:
                    - STOP TAG
      - conditions:
          - condition: time
            weekday:
              - wed
            after: "09:00:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage wednesday
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: trigger
                  id:
                    - STOP TAG
      - conditions:
          - condition: time
            weekday:
              - thu
            after: "13:00:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage thursday
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: trigger
                  id:
                    - STOP TAG
      - conditions:
          - condition: time
            weekday:
              - fri
            after: "17:00:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage friday
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: trigger
                  id:
                    - STOP TAG
mode: single

(ah i was using the choose i will try your other suhgestions now)

(edit 2: i do quite like the choose option as it does give the possobility to customise the actions if needed to have slightly altered ones between days but these are both perfect for what i need one for a single action set and the other to let me have diffrent actions thanks so much!)

an update to this the tag scan trigger is not actually cancelling the repeat? it just keeps repeating even though the tag scans correctly? any advice?

You’re misunderstanding how the trigger works.

Perhaps use a different automation to set an input boolean helper “flag” (which you’ll need to create) when the tag is scanned (or however it works) and use the state of that helper in your until loop, resetting the flag helper on exit?

trigger:
  - platform: tag
    tag_id: 4d9c*******
action:
  - service: input_boolean.turn_on
    target:
      entity_id: input_boolean.chin_clean_flag

then

        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage mon,tues,sat,sun
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: state
                  entity_id: input_boolean.chin_clean_flag
                  state: 'on'
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.chin_clean_flag

I would set up a trigger-based Template sensor to save a timestamp of the last time the tag was scanned. This can serve two functions in your automation. First, you can use it as a general condition so you aren’t alerting if the cage clean tag has already been scanned for the day. Whether this automation is for you or someone else in your household, there are few things more annoying in a “smart” home than being pestered to do something you’ve already done. Second, it can act as the condition that closes out your loop.

template:
  - trigger:
      - platform: tag
        tag_id: 4d9c*******
    sensor:
      - name: Chin Cage Tag Last Scan
        device_class: timestamp
        state: "{{ now() }}"

In your automation you have used the Time condition alone instead of a Time condition and a Trigger condition suggested in my previous post. That will not have the behavior you want . For example, in your first Option, the condition will allow the notifications to be executed at 10:00, 13:00, and 17:00 on Mon,Tue, Sat, and Sun. You need to make the Option exclusive to the correct trigger.

alias: "NEW CHIN CLEAN CAGE REMINDER "
description: ""
trigger:
  - platform: time
    at: "10:00:00"
    id: "10"
  - platform: time
    at: "09:00:00"
    id: "9"
  - platform: time
    at: "13:00:00"
    id: "13"
  - platform: time
    at: "17:00:00"
    id: "17"
condition:
  - alias: Check if tag has not been scanned today
    condition: template
    value_template: |
      {{ today_at() > states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local }}
action:
  - choose:
      - conditions:
          - condition: time
            weekday:
              - mon
              - tue
              - sat
              - sun
          - condition: trigger
            id: "10"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage mon,tues,sat,sun
                - delay: "00:05:00"
              until:
                - alias: Tag was scanned today
                  condition: template
                  value_template: |
                    {{ today_at() < states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local }}
      - conditions:
          - condition: time
            weekday:
              - wed
          - condition: trigger
            id: "9"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage wednesday
                - delay: "00:05:00"
              until:
                - condition: template
                  value_template: |
                    {{ today_at() < states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local }}      
      - conditions:
          - condition: time
            weekday:
              - thu
          - condition: trigger
            id: "13"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage thursday
                - delay: "00:05:00"
              until:
                - condition: template
                  value_template: |
                    {{ today_at() < states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local }}
      - conditions:
          - condition: time
            weekday:
              - fri
          - condition: trigger
            id: "17"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage friday
                - delay: "00:05:00"
              until:
                - condition: template
                  value_template: |
                    {{ today_at() < states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local }}
mode: single

ADDENDUM: You could use a Time condition by itself if you use both an above and a below to exclude triggers later in the day:

- condition: time
  weekday:
    - mon
    - tue
    - sat
    - sun
  after: "10:00:00"
  before: "10:01:00"
1 Like

ok i think i should have it all done except the tag sensor since i realised it didnt run today and it had the “tag scanned” condition in place

i assume its this but I’m not sure what to do with it? i dont do templates or anything usually since I’m still not round to understanding them yet haha

template:
  - trigger:
      - platform: tag
        tag_id: 4d9c*******
    sensor:
      - name: Chin Cage Tag Last Scan
        device_class: timestamp
        state: "{{ now() }}"

You paste that into your configuration.yaml file, save it, then reload your configuration or restart Home Assistant.

Home Assistant Docs: Editing configuration.yaml

i have done some things before with help so i am unsure of how to write this up to work as i keep getting errors


image

You need to remove the second template: and add a hyphen to the sensor:

template:
  - trigger:
      - platform: tag
        tag_id: 4d9c*******
    sensor:
      - name: Chin Cage Tag Last Scan
        device_class: timestamp
        state: "{{ now() }}"

  - sensor:
      - name: "alexa_selector"
        state: >-
# .... the rest of the config ....
1 Like

ok new update it doesnt seem to be triggering?? this is all i have

alias: "NEW CHIN CLEAN CAGE REMINDER "
description: ""
trigger:
  - platform: time
    at: "10:00:00"
  - platform: time
    at: "09:00:00"
  - platform: time
    at: "13:00:00"
  - platform: time
    at: "17:00:00"
  - platform: tag
    tag_id: 4d9cd8c2-9fc1-456f-86f2-e49daa511b11
    id: STOP TAG
condition:
  - alias: Check if tag has not been scanned today
    condition: template
    value_template: >
      {( today_at() >
      states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local }}
action:
  - choose:
      - conditions:
          - condition: time
            weekday:
              - mon
              - tue
              - sat
              - sun
            after: "09:59:00"
            before: "10:01:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage mon,tues,sat,sun
                - service: notify.alexa_media_craigs_dot_bedside
                  metadata: {}
                  data:
                    message: clean chin cage mon,tues,sat,sun
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: trigger
                  id:
                    - STOP TAG
      - conditions:
          - condition: time
            weekday:
              - wed
            after: "08:49:00"
            before: "09:01:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage wednesday
                - service: notify.alexa_media_craigs_dot_bedside
                  metadata: {}
                  data:
                    message: clean chin cage wednesday
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: template
                  value_template: >
                    {{ today_at() <
                    states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local
                    }}
      - conditions:
          - condition: time
            weekday:
              - thu
            after: "12:59:00"
            before: "13:01:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage thursday
                - service: notify.alexa_media_craigs_dot_bedside
                  metadata: {}
                  data:
                    message: clean chin cage thursday
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: template
                  value_template: >
                    {{ today_at() <
                    states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local
                    }}
      - conditions:
          - condition: time
            weekday:
              - fri
            after: "16:59:00"
            before: "17:01:00"
        sequence:
          - repeat:
              sequence:
                - service: notify.craigs_pc
                  metadata: {}
                  data:
                    message: clean chin cage friday
                - service: notify.alexa_media_craigs_dot_bedside
                  metadata: {}
                  data:
                    message: clean chin cage friday
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 0
                    milliseconds: 0
              until:
                - condition: template
                  value_template: >
                    {{ today_at() <
                    states('sensor.chin_cage_tag_last_scan')|as_datetime|as_local
                    }}
mode: single

image

edit: i do know last night i was fiddling with scanning the tag and seeing if the condition for “tag is scanned” passed so it might have just been that i was doing that after midnight and didn’t realise so it thought that i had already done it today perhaps? (last scanned seems to say it was before midnight yesterday so i suppose not)

If you want you can change the cutoff time by adding a time to the today_at() in the condition.

Double check the value of the sensor you created. Keep in mind that it should be in UTC not your local time zone; it may look way off if you don’t take that into account.

1 Like

everything i have showed above is what i have so i dont know where im supposed to be looking for time zone things?

Use the developer tools to check the template (or more specifically, the individual things you compare in the template). I’m not sure, but you use as_local for the sensor, but not for today_at. My first guess would be you should use it at both sides, or neither side.

1 Like

i dont know if i have mentioned but i have never used templates before because they are quite beyond me i can deal with everything else but i am yet to wrap my head around them so I’m not really sure what i am looking for hahah

There’s also {( where {{ should be. The as_local will not change today_at, so that isn’t it. Fix the brackets first.

3 Likes

ok that’s done that’s brought the colour into the txt so i assume that’s actually a proper template now