set variable using os.execute command output

Hi,

I am trying to set a virtual switch variable using the output from a os.execute command (cat of a file containing a numerical value, i.e 0 or 1). I have a veralite running UI5.

My first attempt was this :)…

lum=os.execute(‘cat /tmp/lum.txt’)
luup.variable_set(“urn:schemas-upnp-org:device:VSwitch:1”, “test”, “lum”, 44)

The first line define a variable with the linux cat command. The file contains the string “0”.
The second line tries to define the variable “test” to the value retrieved with the cat command.

It did not work… it sets the variable to “lum” instead to “0”
I am new on this and not a programmer at all so apologies if the above looks silly. I thought it would be a good way to explain what I wanted to achieve :slight_smile:

Is there any way to do the above?

Thanks!

I think os.execute() only returns an execution status… I’d suggest that you rather read the file using lua file handling extensions and assign the variable from reading the file… I am pretty sure you can search around for some code examples (am on the road so cannot send one for the time being)

Probably easier to read the file directly. Only use os.execute() as a last resort.

So something like

local lum
local f =io.open "/tmp/lum.txt"
if f then
  lum = f:read "*l"
  f:close()
end

You’re also using a device URN and not the right serviceId in the luup.variable_set()

Many thanks for the answers.

How do I set the variable “lum”? I have used the following,

luup.variable_set(“urn:upnp-org:serviceId:VSwitch1”, “test”, ‘lum’, 46)

but I get the string “lum” instead of the value in the file. Which syntax I should use? I have used “lum” and ‘lum’ with the same result.

Cheers

luup.variable_set("urn:upnp-org:serviceId:VSwitch1", "test", lum, 46)

Excellent. Works!!

Many thanks for the help :slight_smile: