Very basic yaml formatting question

Forgive the very newbie question, but I have a simple issue with YAML I’m hoping someone can clear up. Questions are numbered for ease of reply. Let’s say I have some templates. I can have

template:
  - sensor:
    name ....
    ......
  - sensor:
    name ....
    ....
  
  1. Does the following achieve the same thing?
template:
  - sensor:
    - name ....
      .....
      ....
    - name ....
      ....
      ....

Then we can have:

sensor:
  - platform: template
    sensors:
      sensor1:
      etc....

2.Is this any different from example 1 above?

3.Finally, can you have multiple, say, template: or sensor: statements in a config file?

Thanks for listening.

No. The first one is a list under template. The second one is a list under sensor.

Yes. And No. That is a completely different layout schema used for legacy template sensors, but it achieves the same thing.

No you can not.

I actually spent some time yesterday reading doc about YAML, because, yeah, let’s face it, beyond copy/paste, it’s quite a bit confusing.

If I understood properly, the first one technically applies the “name” attribute to the “sensor” object.
A dash (“-”) defines a list.

Correct me if I’m wrong, but in JSON terms, the 1st one is

{
  "template" : [
    {
      "sensor": {
        "name": "foo"
      }
    },
    {
      "sensor": {
        "name": "foo2"
      }
    }
  ]
}

and the second

{
  "template" : [
    {
      "sensor": [
        {
          "name": "foo"
        },
        {
          "name": "foo2"
        }
      ]
    }
  ]
}

I find yaml quicker and easier to read than json.

And yes your conversion is correct.

I suppose it is (less overhead), although the translation to lists/dicts - arrays/objects is less intuitive to me, so far.
Then again, you have to know when to use a list and when to use a dict in HA, but that’s another story :slight_smile:

I found this helpful for me when I started.

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

1 Like

That’s fair enough, but what difference does that make in practice for the two examples? Could you use either to create your template-based sensors? I have a bunch of copy-and-pasted template sensors, and they use the second method. As has been said:

Understanding the YAML formatting and putting it into practice in HA is the tricky bit!

I had read that, but it goes back to the previous point. I guess I’ll get the hang to it eventually, going through the learning curve like everyone else…

Thank you everyone for your invaluable help…

One is valid the other isn’t.

Each platform expects a particular schema (as shown in the documentation) and you have to follow it or it won’t pass the configuration check.

It’s like asking someone for a shopping list and they give you a list of things to do (that includes “do the shopping”) instead. It’s not what you want or need. Same goes for the platforms. They require their configuration options to be defined in a particular way and they may have different options.

You can use either the legacy or the new format, or both. But you must follow the schema for each one. You cant use the schema for one in the other.

template:
has a schema. https://www.home-assistant.io/integrations/template/#state-based-template-sensors

platform: template
has a different schema. https://www.home-assistant.io/integrations/template/#legacy-sensor-configuration-format

As long as you follow each of those they can coexist together in the one configuration.

Right, so to be clear, the pertinent part in the documentation is

sensor
List of sensors

Meaning that

template:
  - sensor:
    - name ....
      .....
      ....
    - name ....
      ....
      ....

is the way to go? Thank you very much for the hand-holding.