First off, I'm not a programmer but I've managed to do a few things in the past. Much more of a hacker than anything.
I'm using Mochad to interface with my X10 receiver and I want to take the output from Mochad, parse a few pieces from it and then send that as a MQTT message so I can use it with my home automation hub Home Assistant. This is what the output of Mochad looks like:
pi@raspberrypi:~ $ nc localhost 1099
12/30 17:37:18 Rx RF HouseUnit: B3 Func: On
12/30 17:37:19 Rx RF HouseUnit: B4 Func: Off
12/30 17:37:21 Rx RF HouseUnit: B6 Func: On
12/30 17:37:23 Rx RF HouseUnit: B6 Func: On
12/30 17:37:24 Rx RF HouseUnit: B7 Func: Off
12/30 17:37:25 Rx RF HouseUnit: B8 Func: On
12/30 17:37:27 Rx RF HouseUnit: B2 Func: On
Notice that I am using net cat to view this data. A new line is only added when an X10 signal is detected. I'd like to grab the HouseUnit values (ex B3) and the Func values (ex On). I can do this easily with awk but I can't figure out how to do this from the netcat output.
Once these two values or parsed, I would like to publish them to MQTT message like this:
mosquitto_pub -t /X10/B3 -m On
After this the routine would wait for another message and just keep doing this procedure for each new message received from Mochad.
Is there an easy way to do this via a commandline script? I've written for loops that can do these sort of things but I'm struggling getting the net cat output into the for loop.
I'm doing all of this on a RaspberryPi3 with the HASSBian distro (Raspbian modified for Home Assistant). Any help is greatly appreciated.
EDIT Anyone else able to help? Many thanks!
The following should do what you want:
This takes the output of
nc
and pipes it intoawk
which then callsmosquitto_pub
for each line.EDIT: Tested with the following:
nc -l localhost 1099 < nc-test
Where nc-test is a text file with the example data shown in the question.
p.s. as a rule MQTT topics should not start with a leading / as it adds a extra null level of to the topic tree.