YAML Code Line Counter Sensor

Hello everyone, I wonder if there is the possibility of setting up a sensor that counts the lines of code written in the various .yaml files created for your Home Assistant installation.
I found a thread where the script to count the entities divided by domain was reported but nothing on the lines of code.

Any advice?

Thank You

The “wc” command will count the number of lines in “*.yaml” files.

wc -l /config/*.yaml

You could use this in conjunction with a command line sensor.

However this won’t search all sub-directories, and you can’t exclude some. The “find” command can find all YAML files in sub-directories too, and exclude some folders - I assume you’re only wanting to count your own YAML, rather than that inside custom components etc.

 find . -name "*.yaml" -not -path "*/custom_components/*" -not -path "*/esphome/*" -not -path "*/blueprints/*"

So you can pipe the results of “find” into “wc”, and then use “grep” to just output the “total” line, and then use “grep” again to just output the total line count from that total line:

 find . -name "*.yaml" -not -path "*/custom_components/*" -not -path "*/esphome/*" -not -path "*/blueprints/*" | xargs wc -l | grep "total" | grep "[0-9]*" -o
1 Like

Thank You very much! It works great!!!