Another obscure AppleScript integration I'd like to share in case anyone has the same requirement (doubtful!) or wishes to bring in MQTT data from another device. Output from Weathercat to MQTT was done well way back (2012) by mangrovemike, but getting MQTT data into Weathercat wasn't so simple!
My use case - I have bought a cheap Chinese diesel RV heater on eBay in order to heat my 6'x10' greenhouse during winter (hopefully to make better use of it). It was an "all-in-one" with 6L tank and cabinet. I used this as I don't have mains (but do have a spare SUV battery and solar) and they are very efficient (about 250-500ml/hr for 5kW out). The supplied controller is pretty basic but Ray Jones here in AUS (
http://afterburner.mrjones.id.au) makes a brilliant replacement controller (with WiFi/BT, external temp probes etc) that can (amongst other things) be monitored/controlled via MQTT (not a lot on the site but subscribe to his Facebook group via the link at the bottom for lots of info on this). The controller is in active development so is being added to all the time.
While I can get the greenhouse temp via my Davis Temp/Hum station, I did want to tap into the MQTT messages to get the fuel used, put it on a custom gauge and alert me if it was getting low (rather than having to access the controller webpage or front panel all the time). This is the resultant script. It may also be useful as a basis for getting data from other external sensors that are MQTT capable (or you have made capable via an arduino/Esp32 etc) but not able to be integrated into your station (as a Davis user I know all about this!). The Afterburner sends data in JSON format
As mentioned, subscribing to MQTT data (ie data in) is a bit more fiddly than publishing (ie data out). The reason is several. Firstly the Synthetic channel AppleScript uses the data that is returned and so the script needs to complete (and is triggered periodically). The second issue is that although the MQTT subscription via mosquitto writes to stdout, you can't get to the output directly via AppleScript while it is running. Even using the -E (ie run until everything is read then exit) option I had issues catching and working the results on exit. The "standard" way to access this type of script data is via redirecting outputting to a file and then reading the file back in, which is what I have done. Also helps in debug. The third issue is that Afterburner only sends a full JSON string periodically (with only changed items in-between) so if no other processes (such as MQTT webpage) subscribe (which also triggers a full output) then it could be some time until you get your data. Esp an issue with the fuel burned as if the heater is not running it is not burning fuel and hence no parameter output! So with the AppleScript exiting each time (and I could not find an obvious way to read the synthetic channel's previous value), you also need to save older data to file to recover it for the next AppleScript cycle.
The script is assuming that you are using the mosquitto broker (like mangrovemike's setup) and have installed JSONHelper (free from Mac Store). It could be modified for other brokers as it is done via shell script anyway. You don't need JSONHelper, but the native code then required is awful unless your are a Mac dev!
In essence I have a file (
inFile) to capture the subscription output from
mosquitto_sub() and a file to hold the last fuel usage value. After I read the old value back in, I run the
mosquito_sub() script to get the latest MQTT data. There are several command line options here which are important:
1. I need to maintain a persistent session between the individual AppleScript runs. So I need the '
--disable-clean-session' to make it persistent and '
-i MosqTestS' to manually set a session name to what I want (in this case MosqTestS).
2. In order to store all (normally) "non-retained" data on the broker until the next subscription request the QOS must be 1 or 2 (Afterburner makes the data non-retained explicitly and I have no intent on forking/modifying the codebase!)
3. The '
-E' causes the script to exit once all available MQTT subscription items have been read (rather than crudely killing the process!)
I then try to read the lines of subscription info in
inFile, run them through JSONHelper to decode the JSON text into an AppleScript list, and if the parameter I need (PumpCount) is present then I assign it to a variable to use. Note that once I have exited the repeat loop I will only have the latest value in
newPumpCount. I then convert it to fuel used, save it to
lastFile and return the value. Separately I have an alert message to tell me if the fuel used is over 5000ml.
The fuel usage is manually reset (via either the web interface or controller) after you refill the tank.
Hopefully this may be of use to someone