Problem with command_line YAML

I am running HAOS 2023.6 and trying to address the command line change. In my configuration.yaml file I changed the code to:

command_line:
  - sensor:
    name: CPU Temp
    command: 'cat /sys/class/thermal/thermal_zone0/temp'
    unit_of_measurement: '..C'
    value_template: '{{ value | multiply(0.001) | round(2)  }}'

But now I get this error when I check the YAML in Developer Tools.

Configuration invalid!
Invalid config for [command_line]: expected a dictionary for dictionary value @ data['command_line'][0]['sensor']. Got None
extra keys not allowed @ data['command_line'][0]['command']. Got 'cat /sys/class/thermal/thermal_zone0/temp'
extra keys not allowed @ data['command_line'][0]['name']. Got 'CPU Temp'
extra keys not allowed @ data['command_line'][0]['unit_of_measurement']. Got '..C'
extra keys not allowed @ data['command_line'][0]['value_template']. Got '{{ value | multiply(0.001) | round(2) }}'. (See /config/configuration.yaml, line 46).

I have retyped the code several times based on the example and still get the same error. I must be making a mistake but I just cannot see it. Can anyone else?

your indentation is off:

command_line:
  - sensor:
      name: CPU Temp
      command: 'cat /sys/class/thermal/thermal_zone0/temp'
      unit_of_measurement: '..C'
      value_template: '{{ value | multiply(0.001) | round(2)  }}'
2 Likes

Thank you.

But, why ? I don’t understand why sometimes lists require one indentation and sometimes 2 like in this example. I think it’s official: I hate YAML :dizzy_face:

It’s a data structure. It’s dictated by the person who created the data structure. Just follow what the documentation says, don’t bother trying to understand why if you don’t understand the data structure. Just make sure you copy the examples exactly.

If you want to understand the data structure. The documentation tells you how to form the yaml.

image

For the complex data structures, you have 2 main ones:

list

A list in yaml is a collection of items that can be in any order. It’s like any bulleted list. Each bullet represents the item in the list. And the items in the list can be in any order. Just keep in mind that the order is top to bottom. Note that items in the list can be spill into the next line. This happens when you have an item that has multiple values or properties. You can tell when a listed item starts by looking at the bullet and continuing until you hit the next bullet.

e.g. Here’s a list of fruits.

fruit:
- lemon
- lime

map (or dictionary)

A map or dictionary is a collection of values that have a name. For example, when describing an lemon, the color would be yellow and the taste would be sour.

e.g. Here’s

lemon:
  color: yellow
  taste: sour

Combining these…

fruit:
- lemon:
    color: yellow
    taste: sour
- lime:
    color: green
    taste: sour

Now what happens when you have 2 lemons in your list, but one is sweet and sour?

fruit:
- lemon:
    color: yellow
    taste: sour
- lemon:
    color: yellow
    taste: sweet and sour
- lime:
    color: green
    taste: sour

What if…

Now if this said list instead of map… you would be able to do this:
image

fruit:
- lemon:
  - color: yellow         # FIRST LEMON INDICATED BY DASH
    taste: sour           # END OF FIRST LEMON BECAUSE NEXT LINE HAS DASH
  - color: yellow         # SECOND LEMON INDICATED BY DASH
    taste: sweet and sour # END OF SECOND LEMON BECAUSE NEXT LINE HAS DIFFERENT INDENTATION
- lime:
    color: green
    taste: sour
1 Like

Thanks for the explanation @petro.

I also found this YAML Idiosyncrasies link and this GitHub issue to be quite useful in understanding how nested dictionaries sometimes require a double indent, e.g. when a dictionary is nested inside of a list, as is the case here.

Correct YAML:

command_line:
  - sensor:
      name: CPU Temp

is equivalent to this JSON:

{
  "command_line": [
    {
      "sensor": {
        "name": "CPU Temp"
      }
    }
  ]
}

The keys below - sensor: must be indented one additional level compared to - sensor:, otherwise they would be considered part of the same list as sensor.

Incorrect YAML:

command_line:
  - sensor:
    name: CPU Temp

which would result in this bad JSON:

{
  "command_line": [
    {
      "sensor": null,
      "name": "CPU Temp"
    }
  ]
}

The trick here is that the hyphen followed by a single space in “- sensor:” acts as an indentation level, so sensor is actually indented twice in the previous example: the first two spaces = level 1; the hyphen followed by one space = level 2.

Now it becomes clearer that in the bad YAML, sensor and name are at the same indentation level (2), since name is preceded by four spaces. Six spaces are required such that name is one level down (3) compared to sensor (2).

By the way, since the hyphen followed by a space indicates indentation, you don’t need the two spaces before - sensor: and this is also correct:

Correct YAML (alternative):

command_line:
- sensor:
    name: CPU Temp

Yep, I only use the “alternative”.

Now that the YAML format issue has been solved, I have another issue with this command line:

command_line:
- sensor:
    command: python3 -c "import requests; print(requests.get('https://pypi.python.org/pypi/homeassistant/json').json()['info']['version'])"
    name: Latest HA release
    scan_interval: 3600
    unique_id: '1657007051'

I have this error in home-assistant.log:

2023-06-14 10:16:41.218 ERROR (SyncWorker_2) [homeassistant.components.command_line.utils] Command failed (with return code 1): python3 -c "import requests; print(requests.get('https://pypi.python.org/pypi/homeassistant/json').json()['info']['version'])"

The command works fine when I run it inside the Python3.11 virtual env that hosts HA core 2023.6.1:

(homeassistant) homeassistant@raspberrypi:~/.homeassistant $ python3 -c "import requests; print(requests.get('https://pypi.python.org/pypi/homeassistant/json').json()['info']['version'])"
2023.6.1

The command used to work in previous HA core versions.

Also, when I reload “Command Line Entities” in Developer Tools, all command line entities become “Unavailable”. I have to restart HA to get back the correct values.

Are these known bugs ?