2017-03-16

Vera lua script - Spoken weather report using ImperiHome app

This is the lua code I use for my morning report with my Vera Plus.
Make sure you change the device ID numbers.
The spoken text was translated from Swedish and may not be proper English, change to whatever sounds good to you.

The dev_imperihome device is the virtual Vera app (App id: 5606) and a smartphone with the ImperiHome app connected to the Vera system.


-- Code begins here --
local dev_outdoor_temp = 161
local dev_outdoor_rain = 54
local dev_outdoor_wind = 51
local dev_imperihome = 164

local CurrentOutdoorTemp = (luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", dev_outdoor_temp))
if (string.sub(CurrentOutdoorTemp, 1, 1) == "-") then Minusgrader = 1 else Minusgrader = 0 end

local TempString1, TempString2 = CurrentOutdoorTemp:match("(%d+).(%d+)")
local Temp1, Temp2 = tonumber(TempString1), tonumber(TempString2)

local CurrentRaining = (luup.variable_get("urn:micasaverde-com:serviceId:GenericSensor1", "Rain", dev_outdoor_rain))
local RainString1, RainString2 = CurrentRaining:match("(%d+).(%d+)")
local Rain1, Rain2 = tonumber(RainString1), tonumber(RainString2)

local WindStrength = (luup.variable_get("urn:micasaverde-com:serviceId:GenericSensor1", "WindStrength", dev_outdoor_wind))
local WindString1, WindString2 = WindStrength:match("(%d+).(%d+)")
local Wind1, Wind2 = tonumber(WindString1), tonumber(WindString2)

if ( Minusgrader == 1 ) then
MorningMessage1 = "Good morning! Outdoor temperature is -" .. Temp1 .. "," .. Temp2 .. " degrees cold. "
else 
MorningMessage1 = "Good morning! Outdoor temperature is " .. Temp1 .. "," .. Temp2 .. " degrees warm. "
end

if ( Rain1 > 0 and Rain2 > 0 ) then 
MorningMessage2 = "It is raining " .. Rain1 .. "," .. Rain2 .. " millimeter per hour. "
else
MorningMessage2 = "There is no rain at the moment. "
end

MorningMessage3 = "The wind speed is, " .. Wind1 .. "," .. Wind2 .. " , meters per second."

luup.call_action("urn:imperihome-com:serviceId:ImperiHomeDevice1", "SayTTS", {Text = MorningMessage1 .. MorningMessage2 .. MorningMessage3, Volume = 50},dev_imperihome);
-- Code ends here --