Value Template Date Trigger not working

I missed the sensor: part. Now the template works as it should be.

Thanks again @petro, you solved my week worth of headache.

1 Like

If you want it to reoccur every year like @123 said, you’ll need to use a different template.

- id: testreminder
  alias: Test Reminder
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.date'')[-5:] == ''07-17'' and is_state(''sensor.time'', ''13:50'')}}'
  action:
  - data:
      message: Test reminder working
      title: Automation Triggered
    service: notify.ios_iphone
1 Like

I offer you the following approach which attempts to handle many events (birthdays, anniversaries, etc) with a single automation.

Create a Template Binary Sensor for each recurring event. When the day of the event arrives, the binary_sensor’s state will be set to on (and it remains that way all day long). Each binary_sensor is now accessible to automations, scripts, other template sensors, etc.

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 == 8 and now().day == 1 }}'
      wedding_anniversary:
        friendly_name: "Our Wedding Anniversary"
        entity_id: sensor.date
        value_template: '{{ now().month == 5 and now().day == 20 }}'
      monthly_reminder:
        friendly_name: "It's the second of the month"
        entity_id: sensor.date
        value_template: '{{ now().day == 2 }}'  

Create an automation that runs every day at 09:00 and checks if there’s an event. If there is then the action reports it (the friendly_name) via a notification.

automation:
- alias: "special events"
  trigger:
    platform: time
    at: '09:00:00'
  condition:
    condition: template
    value_template: >
      {{ 'true' if
         states('binary_sensor.mary_birthday') or
         states('binary_sensor.john_birthday') or
         states('binary_sensor.wedding_anniversary') or
         states('binary_sensor.monthly_reminder')
         == 'on' else 'false' }}
  action:
    service: notify.ios_iphone
    data_template:
      title: Special event today
      message: >
        {% set events = [states.binary_sensor.mary_birthday,
                        states.binary_sensor.john_birthday, 
                        states.binary_sensor.wedding_anniversary,
                        states.binary_sensor.monthly_reminder] %}
        {{ (events | selectattr('state','eq', 'on') | list)[0].attributes.friendly_name }}

This automation has a limitation. If two or more events occur on the same day, this automation is only able to report one of them.

2 Likes

Sorry for the late response. The previous template with the year will do fine. But this one without the year will come in handy eventually. Thanks.

Can you explain what does the [-5:] do? I would like to try this script, but I would like to learn how it works and not just copy everything.

Oh and why is that the value_template here is '{{ states(''sensor.date'')[-5:]... and not '{{ is_state("sensor.date")... Is there a big difference between the two?

Thanks.

Thanks @123. @petro’s templates with the year suits my needs for now. But thanks for this template idea you came up with. You gave me a good idea for a project I will do in the future. This is definitely useful.

But can you explain how the condition: and action: work.

Thanks!

It only compares the last 5 characters which will be the month and day ‘xx-xx’. It ignores the year so that it will happen ever year.

In the automation I posted above:

  • It triggers when the time is 09:00:00
  • The condition checks if any one of the four binary_sensors is on. If it is then it reports true which allows the automation to continue to the action section. If none of the binary_sensors is on then it reports false which prevents the action from being executed.
  • The action sends a notification. It uses a template to determine which one of the four binary_sensors is on and then uses its friendly_name to serve as the notification’s message.

More generally it reads the last five characters in a string.

So if your entity state is a string of ‘abcdefghijk’ then the [-5:] will only read the ‘ghijk’ and ignore the rest - abcdef [ghijk].

In your template it reads the ‘07-17’ which is the last five characters of the date including the dash - 2019- [07-17].

Here’s a follow-up to the suggestion I made for reporting events. This version is significantly simpler and easier to maintain.

As per my previous suggestion, create a Template Binary Sensor for each event:

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 of the month"
        entity_id: sensor.date
        value_template: '{{ now().day == 2 }}'

Then create a group containing all the events:

group:
  all_events:
    name: All Events
    entities:
      - binary_sensor.mary_birthday
      - binary_sensor.john_birthday
      - binary_sensor.wedding_anniversary
      - binary_sensor.monthly_reminder

Now the automation can be reduced to this simplified version. Another improvement is that it also reports multiple events occurring on the same day:

automation:
- alias: "special events"
  trigger:
    platform: time
    at: '09:00:00'
  condition:
    condition: template
    value_template: "{{ states('group.all_events') == 'on' }}"
  action:
    service: notify.ios_iphone
    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 ")) }}

Maintenance is easier. Create a new binary_sensor for an event then add it to the group. You don’t need to modify the automation.

Here’s the result of the template when tested in the Template Editor:

There’s been some really advanced responses to this and so I’m going to be cheeky and jump on to the end of this thread, as I think the right people are here to lend a hand. I’m in the process of creating a heating scheduler for each day of the week. Here’s a screen of the card in lovelace: -

image
The orange circles are just on and off, the times are input_datetime entities. The times are very similar now but there’s occasions when I need to vary these per day.

The way I was planning to handle this was to create an individual automation that triggers at the specific time (morning/evening), then a condition to check which day of the week it is and if that day is set to on/off (morning/evening). The action being to set an input_boolean to on, which controls my heating and individual radiators. I don’t really like this method though because everything will trigger and there’ll be loads of automations for each day, morning and evening.

I found my way to this thread because I was wondering if there’s a neater way to use now().day and concatenate it with another string to form the name of the entity I want to check i.e “input_datetime. & now().day & _timer_morning = input_datetime.mon_timer_morning”. From reading this post I don’t think I can use now().day as part of a trigger but could anyone give me a little nudge in the direction of a template that could help me avoid having to trigger all start times at once?

Thanks

I can’t really speak to the automations you are trying to run (I’m not quite wrapping my head around exactly what you are trying to do) but If you are just wanting to find a way to get a day of the week (monday, tuesday, etc) you can create a template sensor to pull that info then use the state of that sensor in your automations string.

sensor:
  - platform: template
    sensors:
      day_of_week:
        friendly_name: Day of Week
        entity_id: sensor.date
        value_template: "{{ as_timestamp(now()) | timestamp_custom('%A') }}"

the sensor will update to the current day name once a day at midnight when sensor.date changes.

if you want the shortened day name (mon, tue…) just substitute “%a” for the “%A” in the template above.

1 Like

{{ now().weekday() }} returns the weekday number, 0 being monday, 6 being sunday.

and if you keep the format input_datetime.mon_timer_morning… You can create a morning template sensor that displays the morning time (and use this for your daily automations).

assuming:

input_datetime.mon_timer_morning
input_datetime.tue_timer_morning
input_datetime.wed_timer_morning
input_datetime.thu_timer_morning
input_datetime.fri_timer_morning
input_datetime.sat_timer_morning
input_datetime.sun_timer_morning

sensor:
  - platform: template
    sensors:
      timer_morning:
        friendly_name: Today Morning Start
        value_template: >
          {% set day = now().strftime('%a').lower() %}
          {% set entity_id = 'input_datetime.' ~ day ~ '_timer_morning' %}
          {{ states(entity_id) }}

Wow!! Thank you both very much, it’s amazing the depth of coding that can be used for templating!! They’re both excellent examples of achieving what I want and so I’ll report back once I get a working solution.

Thank you again!

Unfortunately, using @petro method. I couldn’t get the sensor to update. The state always remained ‘unkown’ I tried restarting the server and waited overnight but no joy. I did come to the conclusion that using a sensor might be tricky because I’d need another automation to trigger and update the sensor, if I changed the time during the day. Instead, I’ve gone for the less elegant solution of having individual automations for each day (on/off/morning/evening).

- id: mon_on_morning
  initial_state: 'true'
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (state_attr('input_datetime.mon_on_morning', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: and
    conditions:
    - condition: time
      weekday:
      - mon
    - condition: state
      entity_id: input_boolean.timer_mon_morning
      state: 'on'
  action:
    entity_id: input_boolean.heating_switch
    service: input_boolean.turn_on
- id: mon_off_morning
  initial_state: 'true'
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (state_attr('input_datetime.mon_off_morning', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: and
    conditions:
    - condition: time
      weekday:
      - mon
    - condition: state
      entity_id: input_boolean.timer_mon_morning
      state: 'on'
  action:
    entity_id: input_boolean.heating_switch
    service: input_boolean.turn_off
- id: mon_on_evening
  initial_state: 'true'
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (state_attr('input_datetime.mon_on_evening', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: and
    conditions:
    - condition: time
      weekday:
      - mon
    - condition: state
      entity_id: input_boolean.timer_mon_evening
      state: 'on'
  action:
    entity_id: input_boolean.heating_switch
    service: input_boolean.turn_on
- id: mon_off_evening
  initial_state: 'true'
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == (state_attr('input_datetime.mon_off_evening', 'timestamp') | int | timestamp_custom('%H:%M', False)) }}"
  condition:
  - condition: and
    conditions:
    - condition: time
      weekday:
      - mon
    - condition: state
      entity_id: input_boolean.timer_mon_morning
      state: 'on'
  action:
    entity_id: input_boolean.heating_switch
    service: input_boolean.turn_off

I’ll stop hijacking this thread now and start a new one to see if there’s others who are interested in this scheduler. I still need to test if this works as well!

Thanks again for the help, I will use your guidance to try a refined automation in the future.

You need to add sensor.date to it, but if you already have sensor.time implemented. But… there’s no point because you can make toggles that include your conditions.

assuming these exist:
input booleans

input_boolean:
  input_boolean.timer_mon_morning:
    name: Timer Monday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_mon_evening:
    name: Timer Monday Evening
    initial: off
    icon: mdi:clock
  input_boolean.timer_tue_morning:
    name: Timer Tuesday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_tue_evening:
    name: Timer Tuesday Evening
    initial: off
    icon: mdi:clock
  input_boolean.timer_wed_morning:
    name: Timer Wednesday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_wed_evening:
    name: Timer Wednesday Evening
    initial: off
    icon: mdi:clock
  input_boolean.timer_thu_morning:
    name: Timer Thursday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_thu_evening:
    name: Timer Thursday Evening
    initial: off
    icon: mdi:clock
  input_boolean.timer_fri_morning:
    name: Timer Friday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_fri_evening:
    name: Timer Friday Evening
    initial: off
    icon: mdi:clock
  input_boolean.timer_sat_morning:
    name: Timer Saturday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_sat_evening:
    name: Timer Saturday Evening
    initial: off
    icon: mdi:clock
  input_boolean.timer_sun_morning:
    name: Timer Sunday Morning
    initial: off
    icon: mdi:clock
  input_boolean.timer_sun_evening:
    name: Timer Sunday Evening
    initial: off
    icon: mdi:clock

input_datetimes

input_datetime:
  input_datetime.mon_on_morning:
    name: Monday On Morning
    has_date: false
    has_time: true
  input_datetime.mon_off_morning:
    name: Monday Off Morning
    has_date: false
    has_time: true
  input_datetime.mon_on_evening:
    name: Monday On Evening
    has_date: false
    has_time: true
  input_datetime.mon_off_evening:
    name: Monday Off Evening
    has_date: false
    has_time: true
  input_datetime.tue_on_morning:
    name: Tuesday On Morning
    has_date: false
    has_time: true
  input_datetime.tue_off_morning:
    name: Tuesday Off Morning
    has_date: false
    has_time: true
  input_datetime.tue_on_evening:
    name: Tuesday On Evening
    has_date: false
    has_time: true
  input_datetime.tue_off_evening:
    name: Tuesday Off Evening
    has_date: false
    has_time: true
  input_datetime.wed_on_morning:
    name: Wednesday On Morning
    has_date: false
    has_time: true
  input_datetime.wed_off_morning:
    name: Wednesday Off Morning
    has_date: false
    has_time: true
  input_datetime.wed_on_evening:
    name: Wednesday On Evening
    has_date: false
    has_time: true
  input_datetime.wed_off_evening:
    name: Wednesday Off Evening
    has_date: false
    has_time: true
  input_datetime.thu_on_morning:
    name: Thursday On Morning
    has_date: false
    has_time: true
  input_datetime.thu_off_morning:
    name: Thursday Off Morning
    has_date: false
    has_time: true
  input_datetime.thu_on_evening:
    name: Thursday On Evening
    has_date: false
    has_time: true
  input_datetime.thu_off_evening:
    name: Thursday Off Evening
    has_date: false
    has_time: true
  input_datetime.fri_on_morning:
    name: Friday On Morning
    has_date: false
    has_time: true
  input_datetime.fri_off_morning:
    name: Friday Off Morning
    has_date: false
    has_time: true
  input_datetime.fri_on_evening:
    name: Friday On Evening
    has_date: false
    has_time: true
  input_datetime.fri_off_evening:
    name: Friday Off Evening
    has_date: false
    has_time: true
  input_datetime.sat_on_morning:
    name: Saturday On Morning
    has_date: false
    has_time: true
  input_datetime.sat_off_morning:
    name: Saturday Off Morning
    has_date: false
    has_time: true
  input_datetime.sat_on_evening:
    name: Saturday On Evening
    has_date: false
    has_time: true
  input_datetime.sat_off_evening:
    name: Saturday Off Evening
    has_date: false
    has_time: true
  input_datetime.sun_on_morning:
    name: Sunday On Morning
    has_date: false
    has_time: true
  input_datetime.sun_off_morning:
    name: Sunday Off Morning
    has_date: false
    has_time: true
  input_datetime.sun_on_evening:
    name: Sunday On Evening
    has_date: false
    has_time: true
  input_datetime.sun_off_evening:
    name: Sunday Off Evening
    has_date: false
    has_time: true

sensor:

sensor:
  - platform: time_date
    display_options:
      - 'time'

binary sensors:

binary_sensor:
  - platform: template
    sensors:
      morning_heat:
        friendly_name: Morning Heat
        entity_id: sensor.time
        value_template: >
          {% set day = now().strftime('%a').lower() %}
          {% set time = states('sensor.time') %}
          {% set start = state_attr('input_datetime.' ~ day ~ '_on_morning', 'timestamp') | int | timestamp_custom('%H:%M', False)) %}
          {% set stop = state_attr('input_datetime.' ~ day ~ '_off_morning', 'timestamp') | int | timestamp_custom('%H:%M', False)) %}
          {% set on = is_state('input_boolean.timer_' ~ day ~ 'morning', 'on') %}
          {{ on and start <= time <= stop }}
      evening_heat:
        friendly_name: Evening Heat
        entity_id: sensor.time
        value_template: >
          {% set day = now().strftime('%a').lower() %}
          {% set time = states('sensor.time') %}
          {% set start = state_attr('input_datetime.' ~ day ~ '_on_evening', 'timestamp') | int | timestamp_custom('%H:%M', False)) %}
          {% set stop = state_attr('input_datetime.' ~ day ~ '_off_evening', 'timestamp') | int | timestamp_custom('%H:%M', False)) %}
          {% set on = is_state('input_boolean.timer_' ~ day ~ 'evening', 'on') %}
          {{ on and start <= time <= stop }}

then this automation will cover all days:

- id: mon_on_morning
  initial_state: 'true'
  trigger:
    platform: state
    entity_id: 
      - binary_sensor.morning_heat
      - binary_sensor.evening_heat
  action:
    service_template: "input_boolean.turn_{{ trigger.to_state.state }}"
    entity_id: input_boolean.heating_switch

Much easier to manage.

1 Like

I try to use this to set a binary_sensor thats reminds me which waste wil be collected.
So far without result, what am I doing wrong? I have put this in the configuration.yaml for plastic waste.

binary_sensor:
  - platform: template
    sensors:
      plastic:
        entity_id: sensor.date
        friendly_name: "Plastic"
        value_template: '{{ ((now().strftime("%W") | int) % 4) == 2) }}'

Your template has a syntax error.

Remove the last right parenthesis ) from the template.

Screenshot%20from%202019-10-22%2017-23-19

Thnx 123 Taras,

It finally works, so simple but I never saw the extra parenthesis )

I wanted to add to this old thread because it helped me find a simple solution to triggering an automation at a specific date and time - without using a sensor.

I may be wrong, but I have found that now() can be used to trigger an automation.

If using the UI, simply select “Template” as the trigger type and put something like this as the “Value template”:

{{ now().year == 2021 and now().month == 8 and now().day == 21 and now().hour==18 and now().minute==5 }}

That will trigger the automation to run on 2021/08/21 at 18:05

You can get creative. For example, to trigger at noon on the 15th day of every month:
{{ now().day == 15 and now().hour==12 }}

For those that prefer the YAML, here is an automation to turn off vacation mode at a specific date and time:

- id: '1628175419106'
  alias: Vacation - Stop
  description: ''
  trigger:
  - platform: template
    value_template: '{{ now().year == 2021 and now().month == 8 and now().day == 21  and
      now().hour==18 and now().minute==5 }}'
  condition: []
  action:
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.vacation_mode
  mode: single
1 Like

Your contribution is appreciated but it would have been best to post it in its own topic (optionally with a link to this topic to show your source of inspiration) as opposed to adding it as the 32nd post to an ancient thread that has been dormant since October 2019.

Screenshot_20210806-091425~2

A lot has changed since then and there are now new ways to do many things in Home Assistant, including how to trigger an automation at a specific time and date (see Time Trigger which now supports the use of an input_datetime).

I encourage you to repost in Share Your Projects (after deleting this post) along with an example of using a Time Trigger with an input_datetime. It will be more visible and the discussion it will foster will be based on Home Assistant’s current best practices and not mixed up with old ways of doing things.

1 Like