api weather gov http poller (National Weather Service) United States

Moderator: leecollings

Post Reply
fallenleader
Posts: 8
Joined: Tuesday 12 December 2023 3:54
Target OS: Linux
Domoticz version: 15792
Location: USA
Contact:

api weather gov http poller (National Weather Service) United States

Post by fallenleader »

the US government™ has a free api for their weather service.
weather~gov/documentation/services-web-api

api~weather~gov/stations/kmpo/observations/latest
--periods exchanged for tlides due to forum

i'm working on trying to get an http poller and lua parser for it.
needless to say it is not working. I'd appreciate any guidence that can be provided but will eventually figure it out.
Error: CLuaHandler: attempt to call a table value (global 'request')
i'm thinking the poller is not actually pulling the data but am not sure where it would be saved.

most of the code is pulled from others on this site.

Code: Select all

--[[Sample json data
    "properties": {
        "presentWeather": [
            {
                "intensity": "light",
                "modifier": null,
                "weather": "rain",
                "rawString": "-RA"
            }
        ],
        "windDirection": {
            "unitCode": "wmoUnit:degree_(angle)",
            "value": 90,
            "qualityControl": "V"
        },
        "windSpeed": {
            "unitCode": "wmoUnit:km_h-1",
            "value": 18.~359999999999999,  --tlide added due to forum
            "qualityControl": "V"
        },
        "barometricPressure": {
            "unitCode": "wmoUnit:Pa",
            "value": 100410,
            "qualityControl": "V"
        },
        "precipitationLastHour": {
            "unitCode": "wmoUnit:mm",
            "value": 0,
            "qualityControl": "C"
        }
    }

]]

-- Define device IDs
local wind_id = 330
local bar_id = 331
local rain_id = 329

-- Retrieve the content
s = request['content'];

-- Access JSON data using the domoticz_applyJsonPath function
--local WB = domoticz_applyJsonPath(s, '~properties~windspeed~value')
--api has no value for above entry
local WD = domoticz_applyJsonPath(s, '~properties~windDirection~value')
local WS = domoticz_applyJsonPath(s, '~properties~windSpeed~value')
local WG = domoticz_applyJsonPath(s, '~properties~windGust~value')
local TP = domoticz_applyJsonPath(s, '~properties~temperature~value')
local WC = domoticz_applyJsonPath(s, '~properties~windChill~value')
local BAR = domoticz_applyJsonPath(s, '~properties~barometricPressure~value')
local BAR_FOR = domoticz_applyJsonPath(s, '~properties~presentWeather~weather')
local RAINRATE = domoticz_applyJsonPath(s, '~properties~precipitationLastHour~value')
--all periods in above section replaced with tlides for forum

-- Update the devices in domoticz
domoticz_updateDevice(wind_id,'',WB .. ";" .. WD .. ";" .. WS .. ";" .. WG .. ";" .. TP .. ";" .. WC)
domoticz_updateDevice(bar_id,'',BAR .. ";" .. BAR_FOR)
domoticz_updateDevice(rain_id,'',RAINRATE)
changes:
removed the sample code from script, now begins at --define device id's
I've fixed s = request which had ( ) to [ ]
updated the device updateDevice's to match domoticz api/json page and changed function names in script to also more closely match.

still throwing various errors, closest i've gotten was basically it telling me there were not enough values in wind, expected 5 had 2, so that was revised as above which brings us to concatenate nil values errors.
Last edited by fallenleader on Wednesday 13 December 2023 3:16, edited 2 times in total.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: api weather gov http poller

Post by waltervl »

Seems to be Ok. Perhaps try with removing the sample json from the file.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
fallenleader
Posts: 8
Joined: Tuesday 12 December 2023 3:54
Target OS: Linux
Domoticz version: 15792
Location: USA
Contact:

Re: api weather gov http poller (National Weather Service) United States

Post by fallenleader »

we're making progress to something that really appears functional. since it is at least working here.
github com/feelingwalnut/NWS-parser-for-Domoticz/tree/main
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: api weather gov http poller (National Weather Service) United States

Post by habahabahaba »

Hi.
Try this code

Code: Select all

-- TEST

return {
	on = {
		timer = {
			'every minute' -- just an example set as often as you need
		},
		httpResponses = {
			'USGovWeathertrigger' -- must match with the callback passed to the openURL command
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'USGovWeathertrigger',
	},
	execute = function(domoticz, item)
	    
	    local APIURL = 'https://api.weather.gov/points/39.7456,-97.0892'

		if (item.isTimer) then
			domoticz.openURL({
				url = APIURL,  
				method = 'GET',
				callback = 'USGovWeathertrigger', -- see httpResponses above.
			}).afterSec(5)
		end

		if (item.isHTTPResponse) then

			if (item.ok) then
				if (item.isJSON) then
				    
                    domoticz.utils.dumpTable(item.json)
                    
                    local city = item.json.properties.relativeLocation.properties.city

                    local state = item.json.properties.relativeLocation.properties.state
                    
                    local coord1 = item.json.properties.relativeLocation.geometry.coordinates[1] -- 1 because in JSON elements start from 0 and in table it starts from 1
                    local coord2 = item.json.properties.relativeLocation.geometry.coordinates[2]
                        

                    domoticz.log('City='.. city.. '  State='.. state .. ';  Coordinates: '.. coord1..'; '.. coord2, domoticz.LOG_ERROR) -- los as ERROR just to mark it for easy find
                        

				end
			else
				domoticz.log('LOG_ERROR', domoticz.LOG_ERROR)
			end

		end

	end
}

To explore JSON elements you can use JSON viewer
fallenleader
Posts: 8
Joined: Tuesday 12 December 2023 3:54
Target OS: Linux
Domoticz version: 15792
Location: USA
Contact:

Re: api weather gov http poller (National Weather Service) United States

Post by fallenleader »

it looks like a dzvent script and when using events to make one, it will bring up the related json information in the log, but not values of use for weather (temp, baro, wind, rain, etc)
so for script test, yes it technically functions.

i put some instructions on my github for getting local station information. but your api link will need to be /stations/----/observations/latest and the local station information in the --- field to get useful data.
your coordinates point to this monitoring station KMYZ
so: api weather gov/stations/KMYZ/observations/latest

unless i misunderstand.
wish i could just post links but the forum keeps throwing warnings at me.
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: api weather gov http poller (National Weather Service) United States

Post by habahabahaba »

So here is your link

Whats the problem?
fallenleader
Posts: 8
Joined: Tuesday 12 December 2023 3:54
Target OS: Linux
Domoticz version: 15792
Location: USA
Contact:

Re: api weather gov http poller (National Weather Service) United States

Post by fallenleader »

i think my problem is that i'm a new member to the forum so i'm not allowed to post links.
but yes, if the link in your script is replaced with a link like you provided, it should work fine to pull the json information into the log.

my parsing script is also functional as of now, I'm getting the current conditions, wind, rain and barometric pressure from NWS then my temperature and humidity is from a local sensor.

seperately i'm also using a frontpage with a link to the mobile version of the NWS forecast page for my local station.
on my frontpage you click the current conditions and are presented with forecast.
really did not care for visualcrossings.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests