Author Topic: Read data from one device and use as a variable in another?  (Read 474 times)

Offline vitmar

  • Full Member
  • ***
  • Posts: 121
  • Karma: +0/-0
Read data from one device and use as a variable in another?
« on: March 10, 2017, 12:53:55 pm »
I've set up my Vera to use an old smartphone with the ImperiHome app and made it talk.
Now I use it to let me know if a door is left open etc, but I would like to make it say the outdoor temperature, if it starts to rainin etc.
I already have the Netatmo as a virtual device so is it possible to read this and use with the ImperiHome Vera app?

Offline amg0

  • Moderator
  • Sr. Hero Member
  • *****
  • Posts: 2484
  • Karma: +153/-8
Re: Read data from one device and use as a variable in another?
« Reply #1 on: March 10, 2017, 01:00:59 pm »
Hummm I do not know this is application and do not see the relation with altui unless I miss Something.

Offline vitmar

  • Full Member
  • ***
  • Posts: 121
  • Karma: +0/-0
Re: Read data from one device and use as a variable in another?
« Reply #2 on: March 10, 2017, 03:39:48 pm »
I can use a scene or workflow to trigger the ImperiHome virtual device.


Offline amg0

  • Moderator
  • Sr. Hero Member
  • *****
  • Posts: 2484
  • Karma: +153/-8
Re: Read data from one device and use as a variable in another?
« Reply #3 on: March 10, 2017, 03:46:21 pm »
if your question is can I read a device variable then have ImperiHome speaks it, then I believe it should be possible as a scene / lua

local value = luup.variable_get(... )
then luup.call_action(... , value )

Offline vitmar

  • Full Member
  • ***
  • Posts: 121
  • Karma: +0/-0
Re: Read data from one device and use as a variable in another?
« Reply #4 on: March 10, 2017, 04:07:55 pm »
I can make it talk but it wont say the variable, it just says the variable name. How can I make the luup understand its a variable?

Code: [Select]
local CurrentOutdoorTemp = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 161)
luup.call_action("urn:imperihome-com:serviceId:ImperiHomeDevice1", "SayTTS", {Text = "Test CurrentOutdoorTemp", Volume = 80},164);

Offline akbooer

  • Master Member
  • *******
  • Posts: 5128
  • Karma: +221/-67
  • "Less is more"
Re: Read data from one device and use as a variable in another?
« Reply #5 on: March 10, 2017, 04:55:49 pm »
I can make it talk but it wont say the variable, it just says the variable name. How can I make the luup understand its a variable?

...you need to be more fluent in Lua.  try this:

Code: [Select]
local CurrentOutdoorTemp = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 161)
luup.call_action("urn:imperihome-com:serviceId:ImperiHomeDevice1", "SayTTS", {Text = "Test " .. CurrentOutdoorTemp, Volume = 80},164);

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P.
Razberry, MySensors Arduino, HomeWave, AltUI, DataYours, openLuup, ZWay, ZeroBrane Studio.

Offline vitmar

  • Full Member
  • ***
  • Posts: 121
  • Karma: +0/-0
Re: Read data from one device and use as a variable in another?
« Reply #6 on: March 14, 2017, 02:30:28 pm »
Yes, I'm a lua noob.

(I know we are getting out of topic...)

I've made som progress but for some reason I cannot make this IF statement to work (everything else does).
What am I doing wrong?
I can make it say all the variables, and they are all numbers...
I've done IF statements like this before so I am very confused.

Code: [Select]
local CurrentOutdoorTemp = math.floor((tonumber((luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 161)))) + .5)
local Raining = tonumber((luup.variable_get("urn:micasaverde-com:serviceId:GenericSensor1", "Rain", 54)))
local WindStrength = math.floor((tonumber((luup.variable_get("urn:micasaverde-com:serviceId:GenericSensor1", "WindStrength", 51)))) + .5)

local MorningMessage1 = "Good morning! It is " .. CurrentOutdoorTemp .. " degrees outside,"
local MorningMessage2 = ""
if ( Raining > 0 ) then local MorningMessage2 = "it's raining," end
local MorningMessage3 = ""
if ( WindStrength > 6 ) then local MorningMessage3 = "and it is windy." end

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

Offline jswim788

  • Sr. Member
  • ****
  • Posts: 484
  • Karma: +23/-2
Re: Read data from one device and use as a variable in another?
« Reply #7 on: March 14, 2017, 03:06:38 pm »
Quote
if ( Raining > 0 ) then local MorningMessage2 = "it's raining," end
I'm learning Lua (slowly), but that 'local' might be a problem.  Won't it set a variable inside the "if", and not set the MorningMessage2 that is used in the message?  I.e, maybe you want this?
Code: [Select]
if ( Raining > 0 ) then MorningMessage2 = "it's raining," end

Offline amg0

  • Moderator
  • Sr. Hero Member
  • *****
  • Posts: 2484
  • Karma: +153/-8
Re: Read data from one device and use as a variable in another?
« Reply #8 on: March 14, 2017, 03:49:37 pm »
Do not combine tonumber() and Luup. Variable_get() on the same code line. Separate with a local val intermediate variable,

One expect multiple arguments , the other one returns several results ( lua feature .., ) and it messes up things


Offline vitmar

  • Full Member
  • ***
  • Posts: 121
  • Karma: +0/-0
Re: Read data from one device and use as a variable in another?
« Reply #9 on: March 15, 2017, 06:01:38 pm »
Code: [Select]
if ( Raining > 0 ) then MorningMessage2 = "it's raining," end

Thank you, that did the trick!

I have to read more about what local does. I've mostly copied lua code from the net and for some reason most of them have local variables.

Offline vitmar

  • Full Member
  • ***
  • Posts: 121
  • Karma: +0/-0
Re: Read data from one device and use as a variable in another?
« Reply #10 on: March 16, 2017, 04:40:34 pm »
This is what I'm running now as a morning weather report.
I translated the text to English, so it may sound strange and may not be the best language (my original is in Swedish).


Code: [Select]
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);