Adding a way to set door codes - Part 3: Use our helpers in the script


< Previous
                                                                                            Next >

We have a script and we have some 'helpers'.  Now we can use the information we get from the helpers in the script.  In the post after this one, we will tie this all together in a 'card' that we will put on our dashboard.

  1. Write a 'script' to set the door code.
  2. Create 'helpers' to make it easy to select the door to use, the code slot, and to enter the door code itself.
  3. Use the 'helpers' in the script.
  4. Create a 'card' that uses the helpers and the script.  This is like the lock card, but uses the helpers to get the door, code slot and door code.

Use the 'helpers' in the script

This will be the most cryptic part of this process.  Trust me.  I find I learn better if I make something work first, even if I do not understand it completely, then go back and twiddle with it.

We 'hardcoded' the values in the script when we wrote it.  It would be a lot nicer if we did not have to use "node_id" and other confusing values when we set a code.  We'll change that now.

Open the script again:
If you are not already there, open up the Home Assistant - http://homeassistant.local:8123/, then go to 'Configuration' in the sidebar, and select 'Scripts'.

Select the that is to the right of your script.  This will take you back to the script edit screen.

In the upper right click theand select "Edit as YAML"

This brings up the YAML editor.  Remember what I said about engineers loving acronyms?  Well YAML is one (as well as a little technical inside joke).  All that matters is it is a way of typing stuff that helps computers understand what you mean.
The stuff in the editor 'means' the same thing as the 'script' we wrote.

For now, just copy this, and paste it into the editor (this is the "Trust me" part)
alias: Set Door Lock Code
description: Set a lock code for a specific code slot and door
sequence:
  - service: lock.set_usercode
    data_template:
      node_id: '{{node_id_v}}'
      code_slot: '{{code_slot_v | int}}'
      usercode: '{{usercode_v}}'
mode: single
variables:
  node_id_v: >
    {% if 'Scottish Room' in states('input_select.lock_name') %}
      2
    {% elif 'Colorado Room' in states('input_select.lock_name') %}
      3
    {% elif 'English Room' in states('input_select.lock_name') %}
      4
    {% elif 'Eastern Room' in states('input_select.lock_name') %}
      5
    {% endif %}
  code_slot_v: '{{ states(''input_text.key_code_slot'') | int }}'
  usercode_v: '{{ states(''input_text.user_code'') | int }}'
Your editor should look like this

If you are happy not knowing the details of what this does right now, you can go on to the next post.

What the *bleep* does this YAML thing mean?

If you go back to the "Edit with UI" mode, you will see that the name is still the same "Set Door Lock Code" and is the same as
alias: Set Door Lock Code
In the script.
But a lot of the YAML stuff does not even appear in the "UI" (acronyms...eek).  In fact the UI tells you that:

If you go back to the top right of the screen and select "Edit as YAML" again, you see the entire 'script'.

The YAML is what the script is really doing.  The "UI" is just trying to make many tasks easier...just like this entire effort is making setting a usercode on a lock simpler to do.

So let's look at the YAML.
The first thing to know about it is that the indentation is very, very  important.  It is a little like an outline in a written paper.  If you have ever used outlines when writing a paper, this is just like that.
  1. Part 1
    1. Part 1.1 (really this is part of 1)
      1. Part 1.1.1 (really this is part of 1.1 which is part of 1...etc)
    2. Part 1.2 (yep, part of 1 as well)
  2. Part 2 (not part of 1)
The primary difference is that the things in the outline are all understood first then used.  So Part 1 might use Part 2 in some way.  This is really up to the way the script understands things, and may change depending on what is 'reading' the YAML.

So, line by line....
  • Line 1 is just the name of the script. No great magic there, except it is used to generate the 'Entity Id' of the script.  Really though the 'Entity Id' does not have to be based on it.  You must refer to the list of Entities in the Configuration to know for sure what the script 'Entity Id' is.  For example I have a few scripts, and one is named script.1606515837177.  This 'bit' me when I was working on my configuration.  Learn from my pain.

  • Line 2 is the beginning of a sequence of events.  Kind of like - "I will do this first, then this then this...etc".  Right now our script only does one thing, then it is done.
  • Line 3 is where the script is being told that it will be calling a service, and what service it will be calling - lock.set_usercode.
  • Line 4 is different than the original script (which had data), because we are no longer telling the script what the information itself is, but what the information will look like - thus it is now a data 'template'.
  • Line 5, 6 and 7 explain to the script where the data is coming from, which is from a few lines later in the script (Line 10 - 22).  This is an example of how the entire script is 'read', and one part of it might 'refer' to another part even though it comes later.
  • Line 8 tells the script what to do if someone tells it to run again before it is complete.  It is a pretty techie thing that is best left alone for now.
  • Line 9 is just a helpful way to tell the person who is looking at the script what it does.  This is something for people, not computers.
  • Line 10 tells the script that there will be some values with specific names and values that will be there for it when we tell it to run.  I just made up the names so I could use them in Lines 5, 6 and 7.  These could just as easily be fred, wilma and barny.  However, then it would be harder to see that they are related to node_id, code_slot and usercode (node_idcode_slot and usercode cannot be changed for this to work.  The service is expecting these very specific names).
    • node_id_v
    • code_slot_v
    • usercode_v  

So what about this:
    {% if 'Scottish Room' in states('input_select.lock_name') %}
      2
    {% elif 'Colorado Room' in states('input_select.lock_name') %}
      3
    {% elif 'English Room' in states('input_select.lock_name') %}
      4
    {% elif 'Eastern Room' in states('input_select.lock_name') %}
      5
    {% endif %}
Well, if you ignore the esoteric characters, it is somewhat readable:
"If the 'Scottish Room' is what the 'lock_name' is set to, then the real value is 2, otherwise if..."
The real value of what?  node_id_v.
Why node_id_v?  Because of the way text is indented and comes after node_id_v.  This is just how the script understands things.

The other two lines:
 code_slot_v: '{{ states(''input_text.key_code_slot'') | int }}'
and
 usercode_v: '{{ states(''input_text.user_code'') | int }}'
Are shorter because they are not as complicated.  The "| int " thing means that the script should use whatever the 'state' of the helpers are and interpret them as numbers.

Where did 
input_select.lock_name
input_text.key_code_slot and 
input_text.user_code 
come from?

These are the just the helpers we created.  

In the next post, we will see how the a person can change the values in the helpers, then call the script.  This will tie this all together and give ourselves a simpler way to set the usercodes on the door.


< Previous                                                                                            Next >

Comments

Popular posts from this blog

Home Automation

Getting your house to ask you for help: Setting up Email Notification using GMail

Adding a way to set door codes - Part 2: Create helpers