Home IoT Home Assistant Lesson 4 – YAML code basic rules

Home Assistant Lesson 4 – YAML code basic rules

0 comment 154 views
YAML code basic rules

Code rules:

Today’s lesson I will write about the basic rules of code in Hass. This article will help you know how to code YAML and know the basic errors.

Home Assistant (or Hass) uses YAML to configure. All the main code to customize for your Hass is contained in the Configuration.yaml file (where this file is located, you know).

YAML code basic rules
notify:
  platform: pushbullet
  api_key: "o.1234abcd"
  name: pushbullet
  • Above is the sample code for me to describe to you about YAML code. The above example is to add a Pushbullet component to Hass. PushBullet is a component that helps you customize Hass notifications to your phone or tablet … Notification line: means that the code below belongs to the notify group, which is a group of notification components. Besides notify, there are many groups such as sensors, switches, light …. In the notify group, there are many types of platforms that need pushbullet, HTML5, .. Basically, like we go to buy a computer, the computer is a big group, then buying any brand is like us. We choose notify, then notify whichever type we prefer (that type is the platform). So where do we see the list of these platforms? See here: https://www.home-assistant.io/components/ Hass’s Components page contains all components that can be integrated into Hass and sample code to be able to add those components.

Rule

  • Some rules you should know about YAML: Each YAML code line has a pair and separated by a colon (“:”) and follows the key: value rule. If you have the same key, the last key value in the code will be used by Hass. Every time you declare attributes, you must indent two spaces against the higher level. Understandably, the lines below 1 level indented 2 lines, indicating that it is a property of higher level code. The lines of code with the same indentation are equal. And you remember we use two spaces, not TAB. If you use confused TAB and whitespace, you will get Code Hass error which is case sensitive, so ‘on’ or ‘On’ or ‘ON’ is completely different. Similarly, switch.Lamp and switch.lamp are different, so please pay attention.found character '\t' that cannot start any token
  • When you declare multiple platforms in the same class, declare them consecutively as follows, for example:
sensor:
  - platform: mqtt
    state_topic: sensor/topic
  - platform: mqtt
    state_topic: sensor2/topic
  • The following declaration will cause an error:
sensor:
  - platform: mqtt
    state_topic: sensor/topic
sensor:
  - platform: mqtt
    state_topic: sensor2/topic
  • When you want to split configuration.yaml into multiple files, you use! Include and declare the following:
lights: !include lights.yaml
  • Specifically, how to divide the file I will present in a separate article.

Note

  • Finally, the comment lines for the code are also called comments. Its purpose is to express the meaning of the code. At the same time, after reviewing, you will be easier to understand. You will leave the # sign at the beginning of the line. For example:
input_select:
  threat:
    name: Mức độ quạt
# Các tùy chọn cho quạt
    options:
     - 0
     - 1
     - 2
     - 3
    initial: 0

Related Tips And Tricks

Leave a Comment

Tips and Tricks

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More