Command line sensor (tail -s1 -f)

I want to setup a binary sensor which looks at a logfile and it it sees a text it should go true. Sounds simple but I guess it’s not…

I can figure out a text which should turn it on and one the should turn it off, but how would I setup the command line sensor? Problem is that the logfile it stored for days and contains lot’s of these on/off messages so I need to have the sensor constantly looking at the file and only the latest data not the whole file.

Is this possible?

Without knowing more about what you’re looking for, it’s hard to offer suggestions.

To push you in the right direction - have you looked at tail’s -n option?

$ tail -1 file.log

-n will output that many lines from the end of a file. -1 will output only the last line of a file. You could pipe this through egrep or similar to format the output into something easy for HASS to understand.

I will usually write a shell script to accomplish what I want, and then just output a pass/fail to the command line, which HASS picks up and uses to populate the sensor.

Just as an example, to see if you can connect to an IP address:

ping.sh
------------
ping -w $2 $1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
      echo "Success"
else
      echo "Fail"
fi
------------
# This sensor detects if TV is turned on (responding to pings)
binary_sensor: 
- platform: command_line
  scan_interval: 10
  name: tv_power
  command: "/home/pi/bin/ping.sh <<ip address of tv>> 2"
  payload_on: "Success"
  payload_off: "Fail" 
------------
# I use the exact same code to detect when my internet is down:
- platform: command_line
  scan_interval: 30
  name: potato
  # 8.8.8.8 is Google's DNS.  if it is unreachable, I might as well not have internet
  command: "/home/pi/bin/ping.sh 8.8.8.8 5"
  payload_on: "Success"
  payload_off: "Fail"
------------

You can use something similar by just adjusting the ping.sh to suit your needs. Again, without knowing what you’re trying to sense, I’m shooting in the dark, but I’m hoping this serves to illustrate the methodology.

zyberse.sh
tail -1  $1 |  grep -q $2
if [ $? -eq 0 ]; then
      echo "Success"
else
      echo "Fail"
fi
------
binary_sensor:
- platform: command_line
  scan_interval: 30
  name: zyberse_sensor
  # First argument is the path to your script, second is the logfile 
  # you are looking at, final argument is the string you are looking for.
  # Obviously edit this to suit your needs. - Double quote ( " ) your  
  # entire command, single quote ( ' ) each item within.
  command: "/path/to/zyberse.sh '/path/to/logfile' 'string to search for'"
  payload_on: "Success"
  payload_off: "Fail"
------

Don’t know if that is what you’re looking for or not. If you need more help, add some more detail, and I’m sure we can puzzle something out.

Thank you @Zen :slight_smile:

I have a Fibaro - The Button which isn’t supported by OZW or HA yet so I can’t get any events from it at the moment so I was hoping it could be resolved (quick and dirty) by watching the OZW log and trigger on events there.

So I just want the most basic functionality, push = trigger an action.

Here is what I see after a single push

2016-12-20 22:50:19.130 Detail, Node009,   Received: 0x01, 0x08, 0x00, 0x04, 0x00, 0x09, 0x02, 0x98, 0x40, 0x20
2016-12-20 22:50:19.131 Info, Node009, Received SecurityCmd_NonceGet from node 9
2016-12-20 22:50:19.131 Info, NONCES: 0xf9, 0x51, 0x36, 0x78, 0xfc, 0xbf, 0x88, 0x6c
2016-12-20 22:50:19.131 Info, NONCES: 0x94, 0x2d, 0x0b, 0xb6, 0xd8, 0x5d, 0xc6, 0x85
2016-12-20 22:50:19.131 Info, NONCES: 0x60, 0x0d, 0x41, 0x9a, 0xa0, 0x31, 0x58, 0x40
2016-12-20 22:50:19.131 Info, NONCES: 0x97, 0xe8, 0xad, 0xd4, 0xc5, 0xda, 0x88, 0xbf
2016-12-20 22:50:19.131 Info, NONCES: 0xaa, 0x3d, 0x37, 0x27, 0x7b, 0x3e, 0x92, 0x0f
2016-12-20 22:50:19.131 Info, NONCES: 0x6a, 0x9c, 0x44, 0x42, 0x78, 0x0b, 0x46, 0xd7
2016-12-20 22:50:19.131 Info, NONCES: 0x88, 0x3a, 0xf2, 0x80, 0xf3, 0x45, 0x44, 0xc1
2016-12-20 22:50:19.132 Info, NONCES: 0xa0, 0x66, 0x90, 0x6e, 0xbe, 0x5e, 0xae, 0xb4
2016-12-20 22:50:19.132 Info, Node009, Sending (WakeUp) message (Callback ID=0x01, Expected Reply=0x00) - Nonce_Report - 0x01, 0x11, 0x00, 0x13, 0x09, 0x0a, 0x98, 0x80, 0x88, 0x3a, 0xf2, 0x80, 0xf3, 0x45, 0x44, 0xc1, 0x05, 0x01, 0x11:
2016-12-20 22:50:19.138 Detail,   Received: 0x01, 0x04, 0x01, 0x13, 0x01, 0xe8
2016-12-20 22:50:19.138 Detail,   ZW_SEND_DATA delivered to Z-Wave stack
2016-12-20 22:50:19.154 Detail,   Received: 0x01, 0x07, 0x00, 0x13, 0x01, 0x00, 0x00, 0x02, 0xe8
2016-12-20 22:50:19.154 Detail,   ZW_SEND_DATA Request with callback ID 0x01 received (expected 0x01)
1 Like

After some fiddling I came this far, this command works in bash but the sensor in HA is dead, no errors in the log.
(I set it to look 4sec back in time and as I understand the command runs every 2sec in HA so that should well cover it for testing purposes)

binary_sensor:
  - platform: command_line
    command: awk -v start="$(date +"%F %R:%S" --date=@$(expr `date +%s` - 4))" -v end="$(date "+%F %R:%S")" '$0 ~ start, $0 ~ end' /home/hass/.homeassistant/OZW_Log.txt | grep -o "Node009" > nul && (echo "ON") || (echo "OFF")
    name: fibaro_button

Went to bed, couldn’t let this go, had to go up again and fix it :smiley:

Here is the solution:

the_button.sh (chmod 755 …)

#!/bin/bash

awk -v start="$(date +"%F %R:%S" --date=@$(expr `date +%s` - 3))" -v end="$(date "+%F %R:%S")" '$0 ~ start, $0 ~ end' /home/hass/.homeassistant/OZW_Log.txt | grep -o "Node009" > /dev/null && (echo "ON") || (echo "OFF")

sensor

binary_sensor:
  - platform: command_line
    command: "/home/hass/.homeassistant/the_button.sh"
    name: fibaro_button
    scan_interval: 3