Set Todays Date Values For Use In Automation (Date Range Automation)

While you can create static sensors or templates to do date-range based automation, I created this so you can easily and quickly create date-range automation on the fly (i.e., turn on Christmas lights if it is between Black Friday and January 6th) or even specific dates within a month (i.e., 1st through the 15th). You can set up four fields and use this blueprint to keep all of your automation together in one place rather than having some as sensors and some as various Py scripts.

The concept is simple, create four number inputs to hold numeric values for the full date (i.e., 20201231), full date without the year (i.e., 1231), the month and the current day. Then in automation use these to determine if the current date falls in between your requirements (i.e., above 1201 and below 1231 indicates any day in December of any year).

Once added, this will run at 5 seconds past midnight each day to calculate the values.

This is my first go at this and it works well in my system, hopefully others will find it useful.

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Or import this Blueprint by using the forum topic URL:

blueprint:
  name: Todays Date Helpers
  description: 'Set the value of various helpers with a numeric value of todays date to use in Automation'
  domain: automation
  input:
    full_date:
      name: Full Date Input
      description: The number input that holds the full date (i.e., 20201201).
      selector:
        entity:
          domain: input_number
    month_day:
      name: Month/Day Date Input
      description: The number input that holds the month and day (i.e., 1201).
      selector:
        entity:
          domain: input_number
    day:
      name: Date Day Input
      description: The number input that holds todays date (i.e., 31).
      selector:
        entity:
          domain: input_number
    month:
      name: Month Input
      description: The number input that holds the current month (i.e., 12).
      selector:
        entity:
          domain: input_number          
trigger:
  - platform: time
    at: '00:00:05'

condition: []

variables:
  full_date: '{{ now().strftime("%Y%m%d") }}'
  month_day: '{{ now().strftime("%m%d") }}'
  day: '{{ now().strftime("%d") }}'
  month: '{{ now().strftime("%m") }}'

action:
  - service: input_number.set_value
    data:
      entity_id: !input 'full_date'
      value: '{{ full_date }}'
  - service: input_number.set_value
    data:
      entity_id: !input 'month_day'
      value: '{{ month_day }}'
  - service: input_number.set_value
    data:
      entity_id: !input 'day'
      value: '{{ day }}'
  - service: input_number.set_value
    data:
      entity_id: !input 'month'
      value: '{{ month }}'      

Example Automation Condition To Run Between November 1 and December 25

2 Likes

You’re all acting like children. Stop ganging up on each other. Just talk about using the blueprint please.

3 Likes

THanx @CO_4X4 !

As a NOOB I have been looking for a while for such a conditional control for my Hot water Immersion heater that I want to run an automation only between 1st May and 30th Septmeber when my Gas central heating system will be turned off for the summer months. So I couldnt find the URL to import your blueprint, so I manually created a new blueprint file in the HA folder and pasted in the code. Then back in my Automation page I edited the Immersion heater code and added a condition. But for now I dont find the “input_number.todays_date_month_day” Entity in the Condition template…Is it beacause I have to wait until 00:05 tomorrow for the script to run and get today’s date? I set up the condition in my Automation anyway and used the ‘Above’ as 0501 and the ‘Below’ as 0930 which I hope means the automation will only run between 1st May and 30th Sept.

I cant seem to get this helper to work as a condition. There is no entity that has been created after adding the "Blueprint"manually to a .yaml file.

The above instruction indicates that there should be an entity called “input_number.todays_date_month_day” but that entity does not exist.

Has anyone else been able to get this to work or can assist me to achieve my objective to be able to set up an Automation that will only work between 1st May through until 30th September?

Thank you in advance.

I have continued trying to parse the yaml code but getting stuck here at row 45 with the following error message:
image

This is my formatted yaml copied from the Ops post:

blueprint:
  name: Todays Date Helpers
  description: 'Set the value of various helpers with a numeric value of todays date to use in Automation'
  domain: automation
  input:
    full_date:
      name: Full Date Input
      description: The number input that holds the full date (i.e., 20201201).
      selector:
        entity:
          domain: input_number
    month_day:
      name: Month/Day Date Input
      description: The number input that holds the month and day (i.e., 1201).
      selector:
        entity:
          domain: input_number
    day:
      name: Date Day Input
      description: The number input that holds todays date (i.e., 31).
      selector:
        entity:
          domain: input_number
    month:
      name: Month Input
      description: The number input that holds the current month (i.e., 12).
      selector:
        entity:
          domain: input_number          
trigger:
  - platform: time
    at: '00:00:05'

condition: []

variables:
  full_date: '{{ now().strftime("%Y%m%d") }}'
  month_day: '{{ now().strftime("%m%d") }}'
  day: '{{ now().strftime("%d") }}'
  month: '{{ now().strftime("%m") }}'

action:
  - service: input_number.set_value
    data:
      entity_id: !input 'full_date'
      value: '{{ full_date }}'
  - service: input_number.set_value
    data:
      entity_id: !input 'month_day'
      value: '{{ month_day }}'
  - service: input_number.set_value
    data:
      entity_id: !input 'day'
      value: '{{ day }}'
  - service: input_number.set_value
    data:
      entity_id: !input 'month'
      value: '{{ month }}'      

Can anyone help to point me in the right direction as to why the !input is not validated from the earlier input definitions in the upper part of the yaml coding?

Getting the same error as @angusc. I’ve searched for a reason, but not finding anything on thing on this. Both syntax and logic appear to be correct, based on the documentation. As indicated in the screen shot by @angusc, it points to the first character of the variable name in the entity_id entry in the action: section. Code snippet shown here :

action:
  - service: input_number.set_value
    data:
      entity_id: !input full_date
      value: '{{ full_date }}'

I’ve moved the - service sections around, and it doesn’t matter which order they are in, the error is always on the service: entry in the list. I’ve also removed the single-quotes from the variable names, but it makes no difference either way. So my guess is that the variable name is not being accepted for the entries for some odd reason. Anyone have an idea what’s wrong here?

I tried running it to see if I ran into your syntax issues, but it just worked. Are you running the latest Home Assistant? I tried making my first blueprint last week and failed miserably. I tried again after updating to core-2021.4.3 and everything worked fine. I don’t know if I did something different my second attempt, or if updating Home Assistant fixed it.

Also, @angusc mentions it not creating “input_number.todays_date_month_day”. I don’t think that’s how this works. You create input_number helpers (Configuration->Helpers, Add, Input Number), ensure they have an appropriate range to hold numbers like 20211111, and you pass your Input Number to the blueprint.

Actually, the more that I think about it,m I’m not even sure how you guys are getting to that error message, or how you got that far without supplying input_numbers… You aren’t pasting the blueprint in as an automation, are you? You have to render blueprints to automations via the Blueprints menus.

1 Like

Thank you!

That was the missing information that us NOOB’s needed! Just for clarity the error that I posted was when you use the built in ‘File Editor’ found in the right hand menus bar. It displays a gree tick mark when the yaml file is correctly formatted or a red exclamation mark if there are problems…for newbees it is hard to know what each error can mean and a lot of trial and error or a helpful soul like yourself comes to the recue.

I only recently started to discover the helpers menu and still done fully understand how they work but I think it is like a place to dump information and then the helper shares it with automations. And I would not have got the Blueprint rendering unless you had mentioned it.

So for clarity to other Newbies that might read this later, these are the steps that i have now taken:

  1. Created the date_helpers.yaml file by copying the code from @CO_4X4 and saved it in Blueprints folder.
  2. Went to Configuration/helpers and created 4x separate helpers called ‘day’ ‘month’ ‘month_day’ & ‘full_date’. the setting for each is given in the description of each in the .yaml code.
  3. Restarted HA.
  4. Went in to Configuration/Blueprints where I then found my ‘date_helpers’ then clicked on “create Automation”. The next screen opens with the 4 inputs being required, just select these helpers that you created in step 2) above for each relevant input.

And possibly learning a little more than I knew a few moths ago…I could have just gone to Configuration/Blueprints and then bottom right corner, “Import Blueprint” using the link provided: ( Set Todays Date Values For Use In Automation (Date Range Automation) )

I Live and learn as they say!

1 Like

Glad it’s working!

I could have just gone to Configuration/Blueprints and then bottom right corner, “Import Blueprint” using the link provided: ( Set Todays Date Values For Use In Automation (Date Range Automation) )

You should also just be able to click the

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

from the original post.

1 Like

Thanks, @angusc! That’s what I needed. Unfortunately there is FAR too much assumption that everyone understands exactly what has to be set up exactly which way in the Home Assistant docs. If the documentation was complete and easy to follow it wouldn’t be a problem, but the docs are really not very helpful for noobs. They’re great as reference (once one knows the actual system), but horrible for learning it. Apparently trial and error and long nights of banging one’s head on the desk is the preferred way to learn this system.

But back to this particular blueprint… I didn’t want to simply import the blueprint because I wanted to set the trigger configuration up like this:

trigger:
  - platform: time
    at: '00:00:05'
  - platform: homeassistant
    event: start
  - platform: event
    event_type: 
      - automation_reloaded

so that it would run automatically in the event that my automations reloaded or Home Assistant restarted, rather than just in the middle of the night. At least, I’m assuming that would be appropriate, since this info is useful all the time rather than just after midnight and my HA is a work in progress, and I’m constantly reloading Core as I add and update different things…

But now my day is good, as I learned something else. So thanks everyone for the comments, and the work in developing this blueprint. Cheers…

Hello,

as a french noob too, I can’t find the configuration/helpers menu.

I’m trying to automate my cover, and only during summer time. This blueprint is perfect for me…just need to make it work.

Where is that ?

Configuration / Entrées.

you are fast…merci.

I should probably change the system langage…

I found another way to make it work but easier:

For the exemple: between 10th of june and 10th of september

- "{{ (6,10) <= (now().month, now().day) <= (9,10) }}"
1 Like

Thanks to all of you for your various explanations around this. I finally have this working, having followed what I thought were the required steps and then reinterpreting them as I figured things out.

I don’t consider myself a Newbie (I joined Home Assistant back in the days pre dated releases), but it still took about an hour of figuring things out to work out what needs to go where. If I may, a slightly more verbose method that I would have found helpful when I was starting through this (very similar to @angusc’s method):

  1. Create 4 Helpers (from the Helper menu in Settings > Devices & Services). These need to be Number helpers. I suppose you could constrain the values to within sensible ranges (e.g. the month one between 1 & 12), but I don’t think this matters, and only makes entering your own values for e.g. testing more difficult. They should be called ‘day’ ‘month’ ‘month_day’ & ‘full_date’ (as described in the yaml code in the blueprint).
  2. Click on the link to download the blueprint.
  3. From the blueprint menu, create an automation by clicking on the “CREATE AUTOMATION” option on Todays Date Helpers.
  4. In the automation, choose the entities of the helpers you created in step 1. You can get it to suggest the helpers by beginning to type e.g. “input_number.” and select the correct one for each one. Name the Automation something sensible so you can find it.

That’s it! No need to restart HA.

You can check that it’s working by assigning temporary values to each of the helpers by clicking on them from within the Helpers menu, and changing the value of the helper from within its own Info tab. Then, once you’ve set them to something obviously not today, you can Run the automation you created by clicking on the 3 dots menu on the right of the automation, and then go back to each helper to check that they’ve indeed been updated to today’s information.