Author Topic: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole  (Read 686 times)

0 Members and 1 Guest are viewing this topic.

Offline sdmike

  • Member
  • *
  • Posts: 4
Hi everyone,

So, wanting to get my Davis VP2 Data into Home Assistant in the least expensive way, I decided to play with exporting the data via AppleScript and passing it to HA via MQTT.

For the most part, I have been able to make that work reliably, mostly through old posts (thanks to all who have posted over the years, especially mikegf) and partially using ChatGPT. I can get 26 entities (readings) in HA. Two of the ones I want most are Daily High and Daily Low. I cannot figure out how to get those.

I have tried creating Synthetic Channels, but probably due most to my lack of understanding on how they work, I can't seem to make that happen. My understanding was that there is a way to send them via MQTT the same way the other channels are sent. But that is not happening. I can see the Daily High and Daily Low figures in the Synthetic Channel config settings, so I know that part is working.

Has anyone else had success with this or know where I might be going wrong?

 [ You are not allowed to view attachments ]  [ You are not allowed to view attachments ]  [ You are not allowed to view attachments ]

Offline elagache

  • Moderator
  • Senior Member
  • *****
  • Posts: 76
    • Canebas Weather Conditions
Re: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole
« Reply #1 on: April 10, 2025, 05:48:54 PM »
Dear sdmike and WeatherCat faithful,

. . .
So, wanting to get my Davis VP2 Data into Home Assistant in the least expensive way, I decided to play with exporting the data via AppleScript and passing it to HA via MQTT.

For the most part, I have been able to make that work reliably, mostly through old posts (thanks to all who have posted over the years, especially mikegf) and partially using ChatGPT. I can get 26 entities (readings) in HA. Two of the ones I want most are Daily High and Daily Low. I cannot figure out how to get those.
. . .

I'm not sure why your synthetic channels aren't working as you have written then, but you might try instead to use the STAT$ tag to get at the values you seek.

To get the maximum temperature, use this code in your synthetic channel:

Code: [Select]
tell application "WeatherCat"
set Query to "STAT$TEMPERATURE:MAX:TODAY$"
set highTemp to QueryResult
end tell

The minimum temperature is as you would expect:

Code: [Select]
tell application "WeatherCat"
set Query to "STAT$TEMPERATURE:MIN:TODAY$"
set highTemp to QueryResult
end tell

As noted in the section of the WeatherCat manual on synthetic channels (starting on page-59) there is no input for a STAT$ tag based synthetic channel.

I cannot test these for you easily as I have all of my 5 synthetic channels active.  However, I can run the AppleScript examples and they return the correct values.

Hopefully, this will solve your problem.  Let us know either way.

Edouard

Offline sdmike

  • Member
  • *
  • Posts: 4
Re: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole
« Reply #2 on: April 11, 2025, 03:20:28 PM »
Thank you elagache for the quick reply.

So I applied the code in the synthetic channel, as you mentioned. I am seeing the correct values in the synthetic channel config pane. I can also go to

Tools --> Custom Graphs --> New
or
Tools --> Custom Gauges --> New

and when I use the dropdown menu to select the input (Series 1, Series 2, Series 3), I do see the synthetic channels listed there, along with all the Station Hardware Options. I can create a graph or gauge and the rendered data looks correct, but it does not seem to be included with the other data via mqtt.

Below is the AppleScript I am using. Is there something in there (or not in there) that I'm missing that would keep the synthetic channel data from being pulled and included?


property _ucChars_ : "AÄÁÀÂÃÅĂĄÆBCÇĆČDĎĐEÉÈÊËĚĘFGHIÍÌÎÏJKLĹĽŁMNÑŃŇ" & ¬
   "OÖÓÒÔÕŐØPQRŔŘSŞŠŚTŤŢUÜÚÙÛŮŰVWXYÝZŽŹŻÞ"

property _lcChars_ : "aäáàâãåăąæbcçćčdďđeéèêëěęfghiíìîïjklĺľłmnñńň" & ¬
   "oöóòôõőøpqrŕřsşšśtťţuüúùûůűvwxyýzžźżþ"

tell application "WeatherCat"
   
   -- Change these variables as you desire
   set loopDelay to 60 -- 60 seconds
   set mqttServer to "<Server IP>"
   set mqttport to "<mqtt port>"
   set mqttChannel to "<mqtt channel>"
   set mqttuser to "<user>"
   set mqttpassword to "<pw>"
   set mqttQOS to "1"
   
   set oldCurrentConditions to ""
   set oldDriverStatus to ""
   
   set quantum to 0.1 -- Used for rounding data to 1 decimal place
   
   -- Initialise the previous values list
   set previousValues to {} -- array/list of previous values
   repeat NumberOfChannels times
      set end of previousValues to ""
   end repeat
   
   repeat -- run around this loop forever, once every x seconds
      
      -- Get the Current Conditions (text)
      set currConditions to CurrentConditions
      if oldCurrentConditions is not currConditions then
         do shell script "/usr/local/bin/mosquitto_pub -h " & mqttServer & " -p " & mqttport & " -q " & mqttQOS & " -u " & mqttuser & " -P " & mqttpassword & " -t " & mqttChannel & "weather/lacostaweather/ -m " & quoted form of currConditions
      end if
      set oldCurrentConditions to currConditions
      
      repeat with theIncrementValue from 1 to NumberOfChannels
         
         set WorkingChannel to theIncrementValue
         set wcname to WorkingChannelName
         set wcvalue to (round (WorkingChannelValue / quantum)) * quantum
         set wcstatus to WorkingChannelStatus
         
         -- Set the previous value from the list
         set wcpreviousvalue to item theIncrementValue of previousValues
         
         -- If the WeatherCat channel is OK and the value has changed then publish
         if wcname is not missing value and wcstatus is true and wcpreviousvalue is not wcvalue then
            
            set wcnamecleaned to my replaceString(wcname, " ", "_")
            set wcnamecleaned to my replaceString(wcnamecleaned, ".", "")
            set wcnamecleaned to my replaceString(wcnamecleaned, "(", "")
            set wcnamecleaned to my replaceString(wcnamecleaned, ")", "")
            
            set wcnamecleaned to my lowerString(wcnamecleaned)
            
            do shell script "/usr/local/bin/mosquitto_pub -h " & mqttServer & " -p " & mqttport & " -q " & mqttQOS & " -u " & mqttuser & " -P " & mqttpassword & " -t '" & mqttChannel & wcnamecleaned & "/' -m " & wcvalue
         end if
         
         set item theIncrementValue of previousValues to wcvalue
         
         
      end repeat
      
      delay loopDelay
   end repeat
end tell

on replaceString(theText, oldString, newString)
   set AppleScript's text item delimiters to oldString
   set tempList to every text item of theText
   set AppleScript's text item delimiters to newString
   set theText to the tempList as string
   set AppleScript's text item delimiters to ""
   return theText
end replaceString

on lowerString(theText)
   local upper, lower, theText
   try
      return my translateChars(theText, my _ucChars_, my _lcChars_)
   on error eMsg number eNum
      error "Can't lowerString: " & eMsg number eNum
   end try
end lowerString

on translateChars(theText, fromChars, toChars)
   local Newtext, fromChars, toChars, char, newChar, theText
   try
      set Newtext to ""
      if (count fromChars) ≠ (count toChars) then
         error "translateChars: From/To strings have different length"
      end if
      repeat with char in theText
         set newChar to char
         set x to offset of char in fromChars
         if x is not 0 then set newChar to character x of toChars
         set Newtext to Newtext & newChar
      end repeat
      return Newtext
   on error eMsg number eNum
      error "Can't translateChars: " & eMsg number eNum
   end try
end translateChars

Offline elagache

  • Moderator
  • Senior Member
  • *****
  • Posts: 76
    • Canebas Weather Conditions
Re: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole
« Reply #3 on: April 12, 2025, 05:57:46 PM »
Dear sdmike and WeatherCat scripters,

. . .
So I applied the code in the synthetic channel, as you mentioned. I am seeing the correct values in the synthetic channel config pane. I can also go to
. . . .
. . . I can create a graph or gauge and the rendered data looks correct, but it does not seem to be included with the other data via mqtt.

Below is the AppleScript I am using.
. . . .

Unfortunately, I don't understand how your AppleScript is supposed to work.  Is it a stand-alone application or are you trying to put this code in a synthetic channel?  Please give us a bit more information so we can try to assist you on your ambitious project.

Edouard

Offline sdmike

  • Member
  • *
  • Posts: 4
Re: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole
« Reply #4 on: April 18, 2025, 11:15:42 AM »
Sorry for the delay. For some reason I didn't get notified of your response.

The AppleScript is standalone and runs separately on the same computer as WeatherCat which has a serial connection to my Davis VP2.

It's a variation (that i've tweaked a little) of Mangrove Mike's script as seen here:

https://github.com/mangrovemike/WeatherCatMosquittoBridge/blob/master/weathercat_mqtt_publish.scpt

and mentioned here:

https://athena.trixology.com/index.php?topic=496.0

It runs continuously and pulls the readings from WC and pushes them via MQTT to Home Assistant. It seems pretty reliable.



Offline elagache

  • Moderator
  • Senior Member
  • *****
  • Posts: 76
    • Canebas Weather Conditions
Re: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole
« Reply #5 on: April 19, 2025, 04:48:23 PM »
Dear Mike and WeatherCat scripters,

Sorry for the delay. For some reason I didn't get notified of your response.

Sorry for my delay.  These days I seem to spend most of the time running around like a chicken with its head cut off! 

The AppleScript is standalone and runs separately on the same computer as WeatherCat which has a serial connection to my Davis VP2.
. . . .
It runs continuously and pulls the readings from WC and pushes them via MQTT to Home Assistant. It seems pretty reliable.

Okay, I had forgotten about this script.  There are better ways to create stand-alone applications in AppleScript, but there is no point in arguing with success.

. . . .
Below is the AppleScript I am using. Is there something in there (or not in there) that I'm missing that would keep the synthetic channel data from being pulled and included?

In situations like this, sometimes it is necessary to delve into the fine-print to be found in the WeatherCat manual.  On page-62 of the current manual, you will find the following caveat:

Quote
f). Synthetic channels are not currently available via WeatherCat's AppleScript interface nor do they appear in the data viewer/editor.

So unfortunately, you cannot access synthetic channels directly from a stand-alone AppleScript.  If you are bold you could have your synthetic channel write its value to a small file and modify Mangrove Mike's script to retrieve the value from the file.  That is the only I'm aware to get around this particular problem.  I don't know if you are comfortable this sort of hack though.

Sorry, such are the conditions . . ..

Edouard

Offline sdmike

  • Member
  • *
  • Posts: 4
Re: WeatherCat, MQTT, and Home Assistant: Going Down The Rabbit Hole
« Reply #6 on: April 21, 2025, 04:58:46 PM »
elagache, thank you for the response.

I swear I read that page at least twice and thought it said it could access them. But there it is.  #-o

I guess the eyes see what they want to see sometimes.

I may play with your idea to have the synthetic channel write its value to a small file and try to grab that instead.

I appreciate your time in looking over my work and setting me straight.

Thanks!  :thumbsup:

 

anything