How to read the next 4 characters from the last occasion of a string from a textfile?

I have a Zipato/Schlage Mini Keypad and I’m trying to get a hold of which user code was used for the arm/disarm.
I have found the string in the OZW_Log.txt that indicates the user code slot, so now I just need to find the string to make a sensor or attribute out of it.

At a certain state change, I want to search the OZW_Log.txt for "Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, ", then grab the following 4 characters and put that in a sensor.

Any suggestions how to accomplish that?

You can use a command_line sensor.
There’s an example how to grep a file for a given string.

Thanks, I’ve been looking at that, and to find the rownumber seems easy, but I can’t figure out if grep can do what I want - to find the last of several occasions of a string, then pick the 4 following characters.
Preferably also only pick the last 50-100 rows in the log to speed up the analyze.

I know how to do it in PowerShell, but that doesn’t help in HA :slight_smile:

Please post a few lines of your log.

I did a little grep-digging yesterday and I think I’m on to something.

grep 'Detail, Node006, Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, ' config/OZW_Log.txt | tail -1

…gives me the row I want, the last occasion of the row containing the data:
2019-12-18 21:11:18.818 Detail, Node006, Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, 0x01, 0x6c

Now all that’s left is to extract the second last part of it, “0x01, 0x6c” in this case. I guess I could try “cut” or “sed” to find that part. I seems the data I want is always at position 150-160 in the fetched row.

Next thing is to execute a command to actually update the sensor when a new unlock is triggered, but I guess that can be a homeassistant.update_entity?

Anyway, first things first. Example log:

2019-12-18 21:09:57.968 Info, Node002, Received Meter report from node 2: Power=0.000W
2019-12-18 21:09:57.968 Detail, Node002, Refreshed Value: old value=0.000, new value=0.000, type=decimal
2019-12-18 21:09:57.968 Detail, Node002, Changes to this value are not verified
2019-12-18 21:09:57.968 Detail, Node002, Notification: ValueChanged
2019-12-18 21:09:57.977 Detail, Node002, Notification: ValueChanged
2019-12-18 21:11:18.818 Detail, Node006,   Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, 0x01, 0x6c
2019-12-18 21:11:18.819 Detail, 
2019-12-18 21:11:18.819 Info, Node006, Received Alarm report: type=0, level=0, sensorSrcID=0, type:Access Control event:6, status=255
2019-12-18 21:11:18.819 Detail, Node006, Initial read of value
2019-12-18 21:11:18.819 Detail, Node006, Initial read of value
2019-12-18 21:11:18.819 Detail, Node006, Initial read of value
2019-12-18 21:11:18.819 Detail, Node006, Initial read of value
2019-12-18 21:11:18.819 Detail, Node006, Notification: ValueChanged
2019-12-18 21:11:18.826 Detail, Node006, Notification: ValueChanged
2019-12-18 21:11:18.833 Detail, Node006, Notification: ValueChanged
2019-12-18 21:11:18.838 Detail, Node006, Notification: ValueChanged
2019-12-18 21:11:18.872 Detail, Node006,   Received: 0x01, 0x08, 0x00, 0x04, 0x00, 0x06, 0x02, 0x84, 0x07, 0x74

A few examples with cut

root@ubu1604:~# echo "2019-12-18 21:11:18.818 Detail, Node006,   Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, 0x01, 0x6c" | cut -d',' -f19-20
 0x01, 0x6c
root@ubu1604:~# echo "2019-12-18 21:11:18.818 Detail, Node006,   Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, 0x01, 0x6c" | cut -c150-160
0x01, 0x6c

or maybe do further processing of the grep output with the value_template of the command_line sensor.
There are more posssibilities with python i think.

Brilliant!

The complete command is now
grep 'Detail, Node006, Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, ' config/OZW_Log.txt | tail -1 | cut -c150-153

which outputs only the slot (which after a little thought can only b 0-9 dec/hex, hence I only need one digit)

1

Edit:

Needed to enclose path in ’ to make command_line work:
grep 'Detail, Node006, Received: 0x01, 0x10, 0x00, 0x04, 0x00, 0x06, 0x0a, 0x71, 0x05, 0x00, 0x00, 0x00, 0xff, 0x06, 0x06, 0x01, ' 'config/OZW_Log.txt' | tail -1 | cut -c150-153