Command_line sensor doesn't refresh its state in Automation

sensor:
  - platform: command_line

    name: Monitoring State

    command: '/bin/cat /config/shell/monitoring_output.log'

It reads an output from the shell script.
The output text file contains either ON or OFF.

When I use this sensor in an Automation Condition it seems that it doesn’t refresh its state.

When I check it in Developer Tools - States - Entity: sensor.monitoring_state and press the refresh button it shows the correct state. What does this refresh button do exactly? Does it just refresh the State on the screen or it refreshes the sensor?

Does the test state condition in Automation run that sensor’s command?

As of now, I do not see any solution for this problem and need help.

A Command Line Sensor updates itself automatically every 60 seconds by default. If this is too long an interval for your application, you can change it to 15 seconds (or whatever you need) by setting the sensor’s scan_interval option.

scan_interval integer (Optional, default: 60)

Defines number of seconds for polling interval.

An automation’s condition will use the sensor’s state as it was determined during the last polling cycle. For example, let’s say the sensor is polled every minute and it was polled at 09:00:00, if the automation’s condition happens to check the sensors’s state at 09:00:50 it will get the value from 50 seconds ago.

Thanks for clarifying that.
So, in my automation it requires adding a delay for the time > scan_interval after the action (shell_command). It works now. But not how I expected it to work.
This is my first experience with shell commands and command_line sensors in HA. I am in the process of migration from other platform to HA and this shell part works better there.

I am not sure how often the file sensor updates File - Home Assistant

Thank you for mentioning just another type of sensors.
They mention in the file description that the file sensor is similar to tail -n 1 sensor.txt on the command-line, so it can be used with the command_line to make its refresh interval (now I learned it) more predictable.

Ditto. The documentation for the two integrations states they both employ Local Polling

Local Polling
Offers direct communication with device. Polling the state means that an update might be noticed later.

I see scan_interval documented for Command_line Sensor but not for File Sensor. However, I believe scan_interval is globally available to many (if not all) local polling-based integrations.

Platforms that require polling will be polled in an interval specified by the main component. For example a light will check every 30 seconds for a changed state. It is possible to overwrite this scan interval for any platform that is being polled by specifying a scan_interval configuration key. In the example below we set up the your_lights platform but tell Home Assistant to poll the devices every 10 seconds instead of the default 30 seconds.

The default polling interval seems to vary from one integration to another.