Extract line from log to trigger an automation

Hi everyone,

I’m trying to trigger an automation based on an error string in the log.
I created a .sh file with this command inside:

if grep -i "Switchbot communication failed" /config/home-assistant.log >/dev/null; then echo "ON"; else echo "OFF"; fi

Then on my configuration.yaml I’m using a binary sensor:

binary_sensor:
  - platform: command_line
    command: "/config/switchbot_error.sh"
    name: "Switchbot Error"

I need help to understand why it’s not working. Thanks

Define “not working”, maybe?

What is the state of the binary sensor?
Something in the logs?

The state is always off but when I run the command from Terminal, the binary sensor state is set accordingly.

On the log I got just

2021-05-16 11:09:31 ERROR (SyncWorker_2) [homeassistant.components.command_line] Command failed: /config/switchbot_error.sh

That’s a big hint, isn’t it :wink:

Maybe try

command: "sh /config/switchbot_error.sh"

I don’t get the error anymore but it’s still not changing the binary sensor state

Be careful that command_line sensors only update once per minute, by default.
Also, I assume you took care of potential permissions issues?

Thanks for your help, I didn’t know the command_line updates once per minute. It seems to be working now

The polling frequency is governed by the scan_interval option and the default is 60 seconds.

scan_interval integer (optional, default: 60)
Defines number of seconds for polling interval