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?
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 @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.
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.
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.
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 @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.
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.