Script To Check Multiple Values And React Differently

Hi

I am trying to script something that will check to see if 3 lights are on or not, if not then do XXXX , if any are on then do YYYYY

The code I pulled together was as follows, but I get an error on the first line, and I can’t work out why?

[code]local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 42
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

local v = local function checkLightsOnOff()

local livingroomlight = luup.variable_get(SEC_SERV, "Status",LI_DEV2)
    local bedroomlight = luup.variable_get(SEC_SERV, "Status",LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, "Status",LI_DEV3)

if(livingroomlight == "0")then
    if(bedroomlight == "0")then
if(halllight == "0")then

luup.log(“v is " … v)
luup.call_action(HA_SERV”,“RunScene”,{SceneNum=44},0)

else

luup.call_action(HA_SERV",“RunScene”,{SceneNum=43},0)

end
end
end[/code]

The error is as follows.

01 11/17/12 13:19:39.998 LuaInterface::LoadCode: [string “local LI_DEV1 = 82…”]:7: unexpected symbol near ‘local’ <0x2f7c8680>
01 11/17/12 13:19:40.000 JobHandler_LuaUPnP::RunLua failed: local LI_DEV1 = 82

Looks like it’s not an error on the first line but on line 7:

With my limited knowledge it would have to be something like:

[code]local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 42
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

–local v = local function checkLightsOnOff()

local livingroomlight = luup.variable_get(SEC_SERV, "Status",LI_DEV2)
local bedroomlight = luup.variable_get(SEC_SERV, "Status",LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, "Status",LI_DEV3)

if(livingroomlight == "0")then
    if(bedroomlight == "0")then
if(halllight == "0")then

–luup.log("v is " … v)
luup.call_action(HA_SERV,“RunScene”,{SceneNum=“44”},0)

else

luup.call_action(HA_SERV,“RunScene”,{SceneNum=“43”},0)

end
end
end
[/code]

I’ve commented out the logging and function part, not sure about those. I think in your luup.call_action you have extra " after HA_SERV and the scenenNum is missing them “43”

Thank so much - that’s been bugging me…

Now this may well be the partially sighted, leading the blind here, but you commented out the function I was after, so i changed it to this.

[code]local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 42
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

–local v =

local function checkLightsOnOff()

local livingroomlight = luup.variable_get(SEC_SERV, "Status",LI_DEV2)
local bedroomlight = luup.variable_get(SEC_SERV, "Status",LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, "Status",LI_DEV3)

if(livingroomlight == "0")then
    if(bedroomlight == "0")then
if(halllight == "0")then

–luup.log("v is " … v)
luup.call_action(HA_SERV,“RunScene”,{SceneNum=“44”},0)

else

luup.call_action(HA_SERV,“RunScene”,{SceneNum=“43”},0)

end
end
end
end
[/code]

I also missed an ‘end’ at the end;)

This shows as Message Sent Successfully yet neither of the scenes are run, so it is not processing as I hoped it would…?

Humm, any more ideas ???

Shouldn’t be better to test all the lights at one time with an “AND” condition ?
Also, you created a function but you don’t call it anywhere… The function is useless here, code it directly, no ?

[code]local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 42
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

local livingroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV2)
local bedroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, “Status”,LI_DEV3)

if ((livingroomlight == “0”) and (bedroomlight == “0”) and (halllight == “0”)) then
luup.log(“All light are off”)
luup.call_action(HA_SERV,“RunScene”,{SceneNum=“44”},0)
else
luup.log(“One light is on”)
luup.call_action(HA_SERV,“RunScene”,{SceneNum=“43”},0)
end[/code]

Thanks so much Shaigan, that works a treat !!

The reason I wrote it as a function was with the idea that I could do a series of group checks, such as lights on , sockets on, doors opened etc. - I’m very new to any sort of coding, so i’m still finding my feet. :slight_smile: - so the idea was that I could do function checks, rather than check individual devices.

There is one other thing I want to do, and thats to add more ‘ifs’
Let me see if I can make that work, I’ll post my attempt here later.

OK, here’s my next hurdle…

I’m just using a luup.log command at the moment to test it out first, but for some reason when i run the following only logs the results of the first two ‘if’ requests, not the third?

[code]
local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 53
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

local livingroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV2)
local bedroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, “Status”,LI_DEV3)

if (livingroomlight == “0”) then
luup.log(“living light is off”)
else
luup.log(“living light is on”)

if (bedroomlight == “0”) then
luup.log(“bedroom light is off”)
else
luup.log(“bedroom light is on”)

if (halllight == “0”) then
luup.log(“hall light is off”)
else
luup.log(“hall light is on”)

end
end
end[/code]

[quote=“parkerc, post:7, topic:173319”]OK, here’s my next hurdle…

I’m just using a luup.log command at the moment to test it out first, but for some reason when i run the following only logs the results of the first two ‘if’ requests, not the third?

[code]
local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 53
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

local livingroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV2)
local bedroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, “Status”,LI_DEV3)

if (livingroomlight == “0”) then
luup.log(“living light is off”)
else
luup.log(“living light is on”)
End

if (bedroomlight == “0”) then
luup.log(“bedroom light is off”)
else
luup.log(“bedroom light is on”)
End

if (halllight == “0”) then
luup.log(“hall light is off”)
else
luup.log(“hall light is on”)

end
[/code][/quote]

I think this should work. By having the end statements after every if block you avoid them being part of the preceding else clause. Am I making sense?

Thanks djs73

However I get this if I try it that way.

01 11/17/12 21:17:43.703 LuaInterface::LoadCode: [string “local LI_DEV1 = 82…”]:17: ‘=’ expected near ‘if’ <0x2f7c8680>
01 11/17/12 21:17:43.705 JobHandler_LuaUPnP::RunLua failed: local LI_DEV1 = 82

I think it might be the capital E’s in the End statements (typing on iPad, drives me nuts)

(Just tried your code on my system, don’t see any problem so far)

I share your pain :wink:

But success, it was the capital Es

[code]local LI_DEV1 = 82
local LI_DEV2 = 22
local LI_DEV3 = 53
local SEC_SERV = “urn:upnp-org:serviceId:SwitchPower1”
local HA_SERV = “urn:micasaverde-com:serviceId:HomeAutomationGateway1”

local livingroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV2)
local bedroomlight = luup.variable_get(SEC_SERV, “Status”,LI_DEV1)
local halllight = luup.variable_get(SEC_SERV, “Status”,LI_DEV3)

if (livingroomlight == “0”) then
luup.log(“living light is off”)
else
luup.log(“living light is on”)
end

if (bedroomlight == “0”) then
luup.log(“bedroom light is off”)
else
luup.log(“bedroom light is on”)
end

if (halllight == “0”) then
luup.log(“hall light is off”)
else
luup.log(“hall light is on”)

end[/code]

Now I’m probably pushing my luck now, and also stepping outside my own comfort zone, but rather than logging that text to the log file. Any ideas how I can capture those same words to memory so i can refer to them again ? I would like to use them in a single Sonos ‘Say’ command?

Not sure I follow…

You want to have the strings, i.e. “Living light is on” stored? Why not just invoke the SAY command from this script? Or you could declare global variables and store the status in those, or the Variable container plugin?

Hi djs73

I’m working on creating some dynamic TTS scripts (http://forum.micasaverde.com/index.php/topic,12408.msg90882.html#msg90882) and I’m curious how I can generate some text (e.g “Lounge Light is on”) and store it as a variable so I can then call it in a Sonos ‘Say’ command.

There may be a far better way of doing it, I’m not sure…