Command line sensor using 'awk'

Any AWK experts here? I’m trying to simplify my setup and if I can get this one sensor to work I can get rid of glances. I just want to monitor the usage of a btrfs array.

On my linux host:

$ df -h /mnt/btrfs_raid
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdf         39T   27T   13T  69% /mnt/btrfs_raid
$ df -h /mnt/btrfs_raid | awk '{print $5}'
Use%
69%
$ df -h /mnt/btrfs_raid | awk 'NR==2 {print $5}'
69%

$ df -h /mnt/btrfs_raid | awk 'NR==2 {print substr($5, 1, length($5)-1)}'
69

However in home assistant:

command_line:
  - sensor:
      name: btrfs_used
      command: "df -h /mnt/btrfs_raid | awk 'NR==2 {print substr($5, 1, length($5)-1)}'"

Returns nothing. Any ideas, I want a number returned, not a string so that I can apply:

unit_of_measurement: "%"

to the sensor.

And in my home assistant log I get:

df: /mnt/btrfs_raid: can't find mount point

however:

command_line:
  - sensor:
      name: btrfs_used
      command: "df -h /mnt/btrfs_raid | awk '{print $5}'"

Does return “Use%” so I don’t think the error in the log is correct?

Without the awk:

      command: "df -h /mnt/btrfs_raid"
      value_template: "{{ (value|regex_findall('(\d*)% /mnt'))[0] }}"

Thanks but:

command_line:
  - sensor:
      name: btrfs_used
      command: "df -h /mnt/btrfs_raid"
      value_template: "{{ (value|regex_findall('(\d*)% /mnt'))[0] }}"

gives me:

2024-01-02 16:10:54.973 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while scanning a double-quoted scalar
  in "configuration.yaml", line 203, column 51. Activating recovery mode
  in "/config/configuration.yaml", line 203, column 23
found unknown escape character 'd'
  in "/config/configuration.yaml", line 203, column 51
2024-01-02 16:10:54.974 ERROR (MainThread) [homeassistant.bootstrap] Failed to parse configuration.yaml: while scanning a double-quoted scalar
  in "configuration.yaml", line 203, column 23

Have I implemented your suggestion correctly?

You may use command:

df -h your_mountpoint| grep -v Filesystem|awk '{print $5}'|sed 's/%//'

Thanks, that command works on the host command line but in home assistant I get nothing (blank/null).

command_line:
 - sensor:
     name: btrfs_used
     command: "df -h /mnt/btrfs_raid | grep -v Filesystem|awk '{print $5}'|sed 's/%//'"

In fact if I add the unit of measurement, I get the error below:

command_line:
 - sensor:
     name: btrfs_used
     command: "df -h /mnt/btrfs_raid | grep -v Filesystem|awk '{print $5}'|sed 's/%//'"
     unit_of_measurement: "%"  

Error:

ValueError: Sensor sensor.btrfs_used has device class 'None', state class 'None' unit '%' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: '' (<class 'str'>)

This suggests to me a bug in home assistant?

If I adjust come of the spacing in the command around the pipes I get a different result:

command_line:
 - sensor:
     name: btrfs_used
     command: "df -h /mnt/btrfs_raid | grep -v Filesystem | awk '{print $5}' | sed 's/%//'"
df: /mnt/btrfs_raid: can't find mount point

Any ideas?

OK so I think I’ve found the issue. Commands are executed within the context of the deployment, for me a docker container, whereas the mount point I’m trying to retrieve data for is on the underlying host hence the mount point can’t be found.

Not sure if there’s a way around this, maybe make the command into a script that’s somehow accessible?

You may provide in your docker-compose.yml:

  volumes:
     - /mnt/btrfs_raid:/btrfs_raid

so in the container you should then use df -h /btrfs_raid instead of /mnt//btrfs_raid.

Yes, just found the same advice in (advised to mount as read-only):

Thanks for your help