The below code works, but it is not what I want to have. The idea is to enter in a six digit user pass code followed by a one digit numeral. The first six characters are the passcode and the last character is what garage door to open or close.
However, I have one user code that starts with a zero and another that starts with a number other than zero. The one with a zero works and the one with a number does not. If I pre-pend a zero to both codes then they both work which is the code I have below but I would rather have the user codes of: â012340â and â123462â because that matches the door lock codes.
I had to jimmy around with the door numbers too, as â1â, â2â, and â3â in the YAML would not work either.
Iâve tried all manner of workarounds without any success. I can accept the door codes being numeral, but really would like a non-zero string of numbers to be treated as a string by the automation without having to pre-pend the zero.
For the record, the event listener does show the entered numbers as a string being received.
Any help would be greatly appreciated. If it is a dummy request - me being the dummy - feel free to virtually slap me.
Long shot, try adding âstringâ filter to the template condition:
In my experience with this yaml/jinja annoyance: whenever you need to use a numeric variable as a string, you should cast it to a string. In other words, casting in the variable definition isnât always enough.
entered_string is numeric (worst case scenario). You can run this in Developer Tools, Actions to see the output. Without the |string filter, the code is never valid. Also, if the script response doesnât apper, just retry 2-3 times.
entered_string is likely not a string, so all the slicing wonât work. Home assistant types top level variables. Meaning, things that are strings but look like numbers will be typed as a number.
To get around this, make complex variable outputs. Assuming you update to @Didgeridrewâs suggestionâŚ
Going to mark petroâs response as the answer. While their YAML did not work, the dictionary approach was the key. As well, a Kudos to Didgeridrew in providing a far more concise block of code that the others built on.
Here is what I have wound up with and both six digit codes now work.
Understood - however, several different AI chatboxes I ran it past commented that a Jinja2-rendered dict is less reliable using dot notation as opposed to bracket notation and as you might have guessed, I am not an advanced YAML user so if the suggestion offered works Iâm fine with using it - especially since I have run into similar issues using dot notation as opposed to bracketed notation with SQL queries in other work I have done so it made sense when it was mentioned.
well in my case I have more than half a dozen users where four started with 4 digit door codes (schlage FE599 lock sets) and when I switched them out for Kwikset with 6-8 digt codes most of those users opted to simply add zeroes to make up the extra two digits, but the other two are new since the Schlage locks were replaced and both opted to use a number other than zero for their first digit.
So the path of least resistance is to use the same codes for this automation which maintains consistency and which makes it less likely I am going to get a call at odd hours asking me to remind them what their code is.
And honestly, I fail to see any good reason other than poor/lazy programming why any assortment of alpha-numeric characters shouldnât be perfectly fine to use. Any decent programming language lets the coder cast a string of all numbers as a string regardless of what the first character is without having to go through all manner of hoops to make that happen.
And you completely miss understood what I said. You canât use the dot notation i.e. something.something if you use a number as the first thing in your key. e.g. something.9something isnât possible. This is a restriction in literally all coding languages. The number restriction isnât about what you put in the dictionary, itâs what key you use. In your case, you are using the word code and the word door for your keys, so this should have no impact on what youâre doing.
And what Petro said applies to the keys. It doesnât affect at all what you can use as the values (so itâs not applicable.to your point about codes and leading zeroes). Either way, youâre completely fine using bracket notation. Itâs just more conventional to avoid it where possible, paradigm-wise.