Input_datetime without the year?

I would like to set two dates (April 1 and November 1 for the time being) to indicate transitions between two different seasons (let’s say, summer and winter). The dates should be adjustable, so I’m thinking input_datetime although they’re likely to stay the same for a few years. I therefore would like NOT to enter the year.

I haven’t found a way to make the input_datetime omit the year. I could, of course, define two input_number per date - for month and day - but it’s not quite as elegant as I would like.

Any ideas?

You could create a sensor that just replicates the input datetime but in the format you desire.
The problem would be, that you can’t adjust this, so you’d need to display the input datetime on (say) a maintenance view somewhere.

If you use an input_text you can specify a pattern to constrain text-entry.

pattern
(string)(Optional)
Regex pattern for client-side validation.
Default value:
empty

1 Like

Ah, yes, input_text is clever. Thanks!

For information, the following regex pattern limits input to DD/MM:
^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))$

1 Like

Yep, brilliant actually - but doesn’t this allow 00/00 - but as you would be setting it the chances of that is negligible.
And as you’ve specified it as always two digits, it should be easy to strip out to month and date values for an automation trigger to switch your boolean or sensor (whatever)
It’s not immediately apparent so I’d suggest using the (DD/MM) in the friendly name.
I wonder if you could just have something like : - (for the icing on the cake)
^(['January '] | ['February '] | ['March '] | ['April '] | [May '] | [June '] | [July '] | [August '] | [September '] | [October '] | [November '] | [December ']) ([0-2][0-9]|(3)[0-1])$
Well there goes another day playing with a new toy

Thanks, I’ve learnt something

:+1:

Edit: just looked at this again, I think I have my brackets mixed up

Edit 2: So far I have (say) Mar 28 with : -
^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s\d\d
And easily expandable to Full Month Names.
But it will accept 00 through 99
:thinking:

Yup, you are correct @Mutt, the regex pattern I proposed allows 00/00, 31/02 and other non-existent dates… It was quickly set up, though…

Like you have done, I have started looking into a regex pattern that only allows dates that may actually exist. I’ve never dealt with regex patterns before, and I must say: Whoa - who came up with regex? It may be precise and efficient, but learning it is a pain…

I couldnt agree more, I thought “this is neat” (and it is) but by god, you seem to need to know it in detail to get anywhere (no little steps at all)
Good Luck

Oh by the way I solved the 00 date regex pattern. You merely include another range so [0][1-9]| to stop 00 being valid

I think this “neat little” regex pattern does the trick:

^((0[1-9])|([12])([0-9])|(3[01]))(-|\/)(0[13578]|10|12)$|^((0[1-9])|([12])([0-9])|(30))(-|\/)(0[469]|11)$|^((0[1-9])|([12])([0-9]))(-|\/)(02)$

It contains three parts:
First allows any date up to 31 for months 01, 03, 05, 07, 08, 10 and 12
Then allows any date up to 30 for months 04, 06, 09 and 11
Then allows any date up to 29 for month 02

It expects date to be on the format DD/MM or DD-MM (I’m in Norway, so day first, then month)

It does not accept anything before the day (indicated by the hat ^) and it does not accept anything after the month (indicated by the dollar sign $). No white space allowed, but expects preceding zeros (double digits in both days and months).

I’m exhausted… :wink:

EDIT: For months on the format ‘Jan’, ‘Feb’ and so on, you would have to use your ^(Jan|Feb|… idea coupled with three parts for 29, 30 and 31 days maximum as I did here.

EDIT 2: Of course, there was a mistake in the pattern. It did allow omission of preceding zeros. Fixed now (I think).

Reasonable, it should ALWAYS be most significant to least significant (or the reverse) (I prefer yyyymmdd international format and text/numeric sortable) And definitely not mixed up like our American brethren
Otherwise 2541 would be written as one, five hundred, forty, two thousand (or some other stupid variation)
Even Tim Berners-Lee wishes (if he had the time again) to put domain first, company 2nd, etc (descending order). Though I don’t know why they don’t announce a deprecation, transition addresses in the interim and have a cut off date.

Edit: Your pattern is a monster !
:rofl:

I fully agree. When writing full dates, I always write yyyy-mm-dd or without the dashes.

Most of my fellow Norwegians write dd/mm-yyyy on paper. Many of them would use dd-mm-yyyy (or ddmmyyyy) in a file name and electronic documents - which is totally unsortable…

In this specific case, however, when giving only day and month, I opted for dd/mm as this is more according to my family member’s (and my own, to be honest) natural way of giving day and month. The ambition is that at some point, I’m going to let my family use the HA app themselves and it has to be user friendly…

I did - as you suggested - include (DD/MM) in the friendly name…

1 Like

Worse still is when they think dmyyyy is equivalent to ddmmyyyy
:roll_eyes:

Why suffer? O’Reilly publishes a cookbook of regex patterns to solve common problems and they offer book excerpts online (as well as online training):

Here’s DD/MM using their posted examples:

  day_month:
    name: Day and Month
    pattern: '^((3[01]|[12][0-9]|[1-9])\/(1[0-2]|[1-9]))$'

If you want it to support zero-padding then it will require an additional |0[1-9] for both capture groups.

For testing regex patterns, I recommend using regex101.com

2 Likes