Value Template Date Trigger not working

Hello,

I’m trying to test a Condition, but I always get an error with the config. I’m not sure what I’m actually missing. Can anyone try to help me figure out the issue?

- id: testreminder
  alias: Test Reminder
  trigger:
  - platform: template
    value_template: '{{ now().date() | string == “2019-07-18” }}'
  condition: []
  action:
  - data:
      message: Test reminder working
      title: Automation Triggered
    service: notify.ios_iphone

What I’m trying to accomplish is to send the reminder to my phone via notification if the date matches today. But I can’t get this working.

And whenever I type this in the automation.yaml, I get an error when I run the Check Config. Though I think the codes seems it will work.

Configuration invalid

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected char '“' at 28) for 
dictionary value @ data['trigger'][0]['value_template']. Got None. (See /config/configuration.yaml, line
19). Please check the docs at https://home-assistant.io/components/automation/

Thanks.

  1. now() cannot cause a trigger. You need to use sensor.date.
  2. You’ve got the wrong quotes in your template.
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
- id: testreminder
  alias: Test Reminder
  trigger:
  - platform: template
    value_template: "{{ is_state('sensor.date', '2019-07-18') }}"
  condition: []
  action:
  - data:
      message: Test reminder working
      title: Automation Triggered
    service: notify.ios_iphone

Also, this will trigger at midnight.

Thanks @petro for clarifying that now() can’t cause a trigger. Your code fixed the problem, I manually triggered and it worked.

So when do you use or when do you use now()? And if the sensor.date you wrote only triggers at midnight, how do you specifically add a certain time, like at 10:00 in the morning?

Sorry for the questions. I just started setting up Home Assistant recently and trying to understand everything little by little from reading forums. But there’s no clear answers when it comes to specific date and time.

Oh it worked. I added another trigger, which is the time. I didn’t know it’s that simple! Thanks @petro

You pretty much use it if something else is the trigger and you want to also check the time in that template. It’s a pretty niche case.

Seems like you got it figured out but you’d add and is_state('sensor.time','10:00') to your template.

@petro I tried adding the sensor.time but it doesn’t automatically trigger. Although manually it sends out the notification.

here is what I currently have:

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

Is this how you add the and is_state('sensor.time','10:00') with the sensor.date?

That should work but it will only trigger on the transition from 13:49 to 13:50. If you restart and it’s already 13:50 it will not trigger.

Also, just for sanity, add a space between )and

A sensor that triggers only at a certain time and on a specific date. The year is specified so this it not a recurring time and date. Basically this sensor’s job consists of a single event.

How do you intend to use this sensor?

@petro I can’t seem to get this to work though, but it’s not showing me errors and I can manually trigger it. I also tried:

condition:
  - condition: template
    value_template: '{{ is_state(''sensor.time'',''15:00'') }}'

It won’t trigger on a specific time I set it to (3:00pm)

@123 I’m planning to automate the lights [LIFX] to trigger a specific action on a specific date and time of the year for every year.

You’ll need a slightly more sophisticated template compared to the one shown above.

Can you help me with something basic that will trigger an action for a specific date and time?

Except it’s not really a specific date you’re after, it’s a recurring date. Like a birthday. It’s every year but on a specific month and day.

Or did I misunderstand your requirements?

Yes. You’re correct. Like a birthday that happens every year.

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.