Okay, thanks @georgesattali, i am now testing your suggestion, it seems to work but requires some tweeking;
My Pressure device showed temperature value as pressure: 12.2 hPa, so your suggestion works, but should be as mentioned below i think?
Code: Select all
commandArray[indexArray] = {['UpdateDevice'] = idxPressure..'|0|' .. pressure .. '; '.. humidity .. ';' .. humidity_status .. '; '.. temperature ..';'.. trend}
My (debug) log now shows:
Code: Select all
2016-04-22 11:51:03.689 LUA: | ------------------------------------------------ |
2016-04-22 11:51:03.689 LUA: | =============== API Wunderground =============== |
2016-04-22 11:51:03.689 LUA: | ------------------------------------------------ |
2016-04-22 11:51:03.689 LUA: | wuAPIkey = xxx
2016-04-22 11:51:03.689 LUA: | now updating the data and lastupdate uservariable
2016-04-22 11:51:03.689 LUA: | curl http://api.wunderground.com/api/xxx/conditions/q/country/city.json
2016-04-22 11:51:03.689 LUA: | Lat: latitude | Long: longtitude | Alt: 3.00000000
2016-04-22 11:51:03.689 LUA: | Tijdstip van waarneming: Fri, 22 Apr 2016 11:50:10 +0200
2016-04-22 11:51:03.689 LUA: | pressure_mb: 1021 hPa | trend: 0
2016-04-22 11:51:03.689 LUA: | temperatuur: 12.2 C | humidity: 59%
2016-04-22 11:51:03.689 LUA: | gevoelstemperatuur: 12.2 C
2016-04-22 11:51:03.689 LUA: | zicht: 10.0 km
2016-04-22 11:51:03.689 LUA: | wind: NE (49) | snelheid: 9.7 km/h | windvlaag: 16.1 km/h
2016-04-22 11:51:03.689 LUA: | neerslag vandaag: 0 mm | neerslag uur: 0 mm
2016-04-22 11:51:03.689 LUA: | UV: 3.7 | solar radiation: 823 Watt/m2
2016-04-22 11:51:03.689 LUA: | weather: Mostly Cloudy
2016-04-22 11:51:03.689 LUA: | ------------------------------------------------ |
2016-04-22 11:51:03.689 LUA: | ------------ updating device values ------------ |
2016-04-22 11:51:03.689 LUA: | ------------------------------------------------ |
2016-04-22 11:51:03.689 LUA: | Temperature device updated with value: 12.2
2016-04-22 11:51:03.689 LUA: | Temperature realfeel device updated with value: 12.2
2016-04-22 11:51:03.689 LUA: | Humidity device updated with value: 59%
2016-04-22 11:51:03.689 LUA: | Pressure device updated with value: 1021
2016-04-22 11:51:03.690 LUA: | Visibility device updated with value: 10.0
2016-04-22 11:51:03.690 LUA: | Solar device updated with value: 823
2016-04-22 11:51:03.690 LUA: | Weather device updated with value: Mostly Cloudy
...
I am updating several (virtual) devices, below is (part of) my script with the JSON call to WU which actually delivers all the info for various weather devices, this part works like a charm! I am using the returned values to set my devices.
Code: Select all
-- perform the API Wunderground call
cmd = 'curl http://api.wunderground.com/api/'..wuAPIkey..'/conditions/q/'..country..'/'..city..'.json'
local config=assert(io.popen(cmd))
local location = config:read('*all')
config:close()
local jsonLocation = json:decode(location)
-- get the results from the call
local latitude = jsonLocation.current_observation.display_location.latitude
local longitude = jsonLocation.current_observation.display_location.longitude
local altitude = jsonLocation.current_observation.display_location.elevation
local observation_timestamp = jsonLocation.current_observation.observation_time_rfc822
local temperature = jsonLocation.current_observation.temp_c
local temperature_realfeel = jsonLocation.current_observation.feelslike_c
local humidity = jsonLocation.current_observation.relative_humidity
local pressure = jsonLocation.current_observation.pressure_mb
local pressure_trend = jsonLocation.current_observation.pressure_trend
local visibility = jsonLocation.current_observation.visibility_km
local wind_dir = jsonLocation.current_observation.wind_dir
local wind_deg = jsonLocation.current_observation.wind_degrees
local wind_speed = jsonLocation.current_observation.wind_kph
local wind_gust = jsonLocation.current_observation.wind_gust_kph
local rain_today = jsonLocation.current_observation.precip_today_metric
local rain_hour = jsonLocation.current_observation.precip_1hr_metric
local uv = jsonLocation.current_observation.UV
local solar = jsonLocation.current_observation.solarradiation
local weather = jsonLocation.current_observation.weather
With the above variables i am now successfully setting the below (virtual) devices;
Code: Select all
----------------------------
-- update virtual sensors --
----------------------------
-- For more information on the json commands, see: https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's
if ((idxTemperature) and (temperature)) then
commandArray[indexArray] = {['UpdateDevice'] = tostring(idxTemperature)..'|0|'..tostring(temperature)}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Temperature device updated with value: "..tostring(temperature)) end
end
if ((idxTemperature_realfeel) and (temperature_realfeel)) then
commandArray[indexArray] = {['UpdateDevice'] = tostring(idxTemperature_realfeel)..'|0|'..tostring(temperature_realfeel)}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Temperature realfeel device updated with value: "..tostring(temperature_realfeel)) end
end
if ((idxHumidity) and (humidity)) then
-- Humidity_status can be one of:
-- 0=Normal 1=Comfortable 2=Dry 3=Wet
commandArray[indexArray] = {['UpdateDevice'] = tostring(idxHumidity)..'|'..humidity..'|0'}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Humidity device updated with value: "..tostring(humidity)) end
end
if ((idxPressure) and (pressure)) then
if (pressure_trend) then trend=tostring(pressure_trend) else trend="0" end
pressure = tonumber(pressure)
-- This 'trend' is still experimental! --
-------------------------------------------
-- mapping for trend
-- Barometer forecast can be one of:
-- 0 = No info
-- 1 = Sunny
-- 2 = Partly cloudy
-- 3 = Cloudy
-- 4 = Rain
if (trend == "0") then
if (pressure > 1013) then trend = "1"
elseif (pressure > 987) then trend = "2"
elseif (pressure < 987) then trend = "3"
end
elseif (trend == "+") then
if (pressure > 1013) then trend = "1"
elseif (pressure > 987) then trend = "2"
elseif (pressure < 987) then trend = "2"
end
elseif (trend == "-") then
if (pressure > 1013) then trend = "3"
elseif (pressure > 987) then trend = "4"
elseif (pressure < 987) then trend = "4"
end
else
trend = "0"
end
-- This 'humidity' is still experimental! --
-------------------------------------------
-- Humidity_status can be one of:
-- 0=Normal 1=Comfortable 2=Dry 3=Wet
humidity_status = 0
commandArray[indexArray] = {['UpdateDevice'] = idxPressure..'|0|' .. pressure .. '; '.. humidity .. ';' .. humidity_status .. '; '.. temperature ..';'.. trend}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Pressure device updated with value: "..tostring(pressure)) end
end
if ((idxVisibility) and (visibility)) then
commandArray[indexArray] = {['UpdateDevice'] = tostring(idxVisibility)..'|0|'..visibility}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Visibility device updated with value: "..tostring(visibility)) end
end
if ((idxSolar) and (solar)) then
commandArray[indexArray] = {['UpdateDevice'] = tostring(idxSolar)..'|0|'..tonumber(solar)}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Solar device updated with value: "..tostring(solar)) end
end
if ((idxWeather) and (weather)) then
commandArray[indexArray] = {['UpdateDevice'] = tostring(idxWeather)..'|0|'..tostring(weather)}
indexArray=indexArray+1
if( DEBUG == 1) then print("| Weather device updated with value: "..tostring(weather)) end
end
How to set the BELOW (virtual) devices through UpdateDevice commandArray?
When setting through JSON this will result in an error telling that the script has been running over 10 seconds and after a while Domoticz will even crash!! (hangs)
Other than the JSON URL's i haven't been able to find any info on setting these devices, so help is very much welcome and appreciated
Code: Select all
if (idxRain) then
-- old command
-- cmd = 'http://192.168.2.100:8080/json.htm?type=command¶m=udevice&idx='..idxRain..'&nvalue=0&svalue='..tostring(rain_hour)..';'..tostring(rain_today)
-- result = os.execute('curl "'..cmd..'"')
-- command = '/json.htm?type=command¶m=udevice&idx='..idxRain..'&nvalue=0&svalue='..tostring(rain_hour)..';'..tostring(rain_today)
-- result = process_device_values(command)
result = "halted"
if (result == nil) then result = "FAILED! (command: "..cmd..")" else result = tostring(result) end
if( DEBUG == 1) then print("| Rain device updated, result: "..result) end
end
if ((idxUv) and (uv)) then
-- old command
-- commandArray[indexArray] = {['UpdateDevice'] = tostring(idxUv)..'|'..uv..'|1'}
-- indexArray=indexArray+1
-- command = '/json.htm?type=command¶m=udevice&idx='..idxUv..'&nvalue=0&svalue='..uv..';0'
-- result = process_device_values(command)
result = "halted"
if (result == nil) then result = "FAILED! (command: "..cmd..")" else result = tostring(result) end
if( DEBUG == 1) then print("| Rain device updated, result: "..result) end
end
if ((idxWind) and (wind_speed)) then
-- old command
-- cmd = 'http://192.168.2.100:8080/json.htm?type=command¶m=udevice&idx='..idxWind..'&nvalue=0&svalue='..wind_deg..';'..wind_dir..';'..wind_speed..';'..wind_gust..';22;24'
-- result = os.execute('curl "'..cmd..'"')
-- command = '/json.htm?type=command¶m=udevice&idx='..idxWind..'&nvalue=0&svalue='..wind_deg..';'..wind_dir..';'..wind_speed..';'..wind_gust..';22;24'
-- result = process_device_values(command)
result = "halted"
if (result == nil) then result = "FAILED! (command: "..cmd..")" else result = tostring(result) end
if( DEBUG == 1) then print("| Rain device updated, result: "..result) end
-- still need to process and set: wind_dir ("..wind_dir..") wind_deg ("..wind_deg..") wind_speed ("..wind_speed..") wind_gust ("..wind_gust..")")
end