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: -
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?
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.
{{ 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).
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.
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).
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
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 }}
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.
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
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.
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.