Shell_command sensor for last modified date of files

Hello,

I would like to implement a shell_command sensor to get the last modified date of certain files in a directory. The command executes successfully from a VSCode terminal, but I can’t get it to work as a sensor.

Config:

- platform: command_line
  name: Last backup date media
  command: 'find /media/Upload -type f \( -iname \*.jpg \) -printf "%T+\n" | sort | tail -n 1'

If I run this from the VSCode add-on I get the expected result: 2022-03-14+00:29:11.1575092970, but not from the sensor. Debugging logging does not show any additional information:

2022-03-15 21:15:57 DEBUG (SyncWorker_3) [homeassistant.components.command_line.sensor] Running command: find /media/Upload -type f \( -iname \*.jpg  \) -printf "%T+\n" | sort | tail -n 1

The find command in the addon is visibly different than the one in the HA container, where that command will actually be run.

Thanks for the the hint, had a feeling it had to do with -printf missing. Will look for an alternative.

Fixed it the following way:

- platform: command_line
  name: Last backup date media
  command: 'find /media/Upload -type f \( -iname \*.jpg -o -iname \*.png -o -iname \*.mov \) -print0 | xargs -0 stat -c %y | sort | tail -n 1 | sed "s/\s+/+/g"'

Or for a static file to watch:

- platform: command_line
  name: Last backup date media
  command: 'stat /path/to/file -c %y | sed "s/\s+/+/g"'

In the automation I have the following trigger for stale backups:

  - platform: template
    value_template:  "{{ (as_timestamp(states('sensor.last_backup_date_media')) | int)  < ((as_timestamp(now() - timedelta(days = 7)) | int)) }}"