How to creat a calculating virtual device

Hello everyone ,

I would need a little help to create a virtual device.

This virtual device will display the calculation result of the efficiency of a controlled mechanical ventilation double flow .

This is a calculation device with temperatures of a universal sensor of fibaro .

The calculation is ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200



The solution :

Creat a device with a % for the unit (I have choose Humidity sensor)
Go to Apps → Develop apps → Create device

and put

Device type = urn:schemas-micasaverde-com:device:HumiditySensor:1 Description = VMC efficiency Upnp Device Filename = D_HumiditySensor1.xml

Reload engin
Now, you can watch your new device
Clic on the arrow at the right of the device and go to advanced
Remember the ID number

Go to the scene creator
and creat a scene like the picture i add

In the “Also, execute the following Luup code:” section, put this code

[code]-- VMC efficiency calculation

local ID = {
Outside = 21, – the new outside air
Supply = 20, – the new supply air
Dirty = 22, – the dirty indoor air
Result = 23, – PICK YOUR OUTPUT DEVICE ID HERE
}

local SID = {
temperature = “urn:upnp-org:serviceId:TemperatureSensor1”,
efficiency = “urn:micasaverde-com:serviceId:HumiditySensor1”,
}

local function get_temp (id)
local t = luup.variable_get (SID.temperature,“CurrentTemperature”,id)
return t
end

local outside_temp = get_temp (ID.Outside)
local supply_temp = get_temp (ID.Supply)
local dirty_temp = get_temp (ID.Dirty)

if dirty_temp > outside_temp then
local VMC_efficiency = ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200
local round = “%0.0f”
VMC_efficiency = round:format (VMC_efficiency)
luup.variable_set (SID.efficiency, “CurrentLevel”, VMC_efficiency, ID.Result)
end[/code]

Change the 20,21,22 and 23 with your ID

Be carful, the dirty temp must be > at the outside temp to work

Use existing plugins …

Use the PLEG plugin for the computation
Use the MultiString plugin for the display

Sorry but i don’t want instal 2 plugin just for one Device.
If someone have creat one device with calculating, can him post the file. Maybe i can do something with it

The simplest thing is to write a small amount of Lua code that runs in the Lua Startup area, or a scene. It would either watch or poll your temperature values, calculate your required parameter and write it back to, say, the master Fibaro universal sensor device as a new variable.

There are many examples of such code in other posts, but ask if you need more.


Edit: Here’s one, for example:

http://forum.micasaverde.com/index.php/topic,37464.msg280070.html#msg280070

Thanks for your reply, i will try to write something and sumit it here

Finaly, if it’s possible to show me an exemple
I have search on the forum but nothing,
I should probably look bad

Perhaps you missed reading my edit to an ealier post…

[quote=“akbooer, post:4, topic:192683”]Edit: Here’s one, for example:

http://forum.micasaverde.com/index.php/topic,37464.msg280070.html#msg280070[/quote]

Sorry, I had not seen your edit .

The best for me is that everything is only at the virtual device
Apps → Develop apps → Create device
with

Device type = urn:schemas-micasaverde-com:device:TemperatureSensor:1 or something else ( I would in % therefore humidity maybe) Description = VMC_Efficiency Upnp Device Filename = D_vmcefficiency.xml

I tinkered it but I don’t know if it’s good and it is certainly not complete

[code]local Outside_air_ID = 21 – the new outside air
local Supply_air_ID = 20 – the new supply air
local dirty_air_ID = 22 – the dirty indoor air

local Outside_air_temp = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”,“CurrentTemperature”,Outside_air_ID)
local Supply_air_temp = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”,“CurrentTemperature”,Supply_air_ID)
local dirty_air_temp = luup.variable_get(“urn:schemas-micasaverde-com:device:TemperatureSensor:1”,“CurrentTemperature”,dirty_air_ID)

local outside_temp = tonumber(Outside_air_temp)
local supply_temp = tonumber(Supply_air_temp)
local dirty_temp = tonumber(dirty_air_temp)

local VMC_efficiency = ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200[/code]

For the file D_vmcefficiency.xml
I don’t know if i must creat mine with this code or take a generic one ( D_TemperatureSensor1.xml / D_LightSensor1.xml / D_HumiditySensor1.xml)
And i don’t know how to finish the code

Anyway, thank you in advance for your help

Good job - your code is almost complete.

I have re-written it slightly, mostly a question of style.

I strongly suggest you do not start out by writing a plugin, or even creating a device for the result. Get the simplest things going first. In the following code you need to change the result device number 999 to something real (as before, I might suggest the Generic Fibaro device which is the parent of these temperature readings.)

It writes the result, at the moment, to a GenericSensor serviceId, giving it the understandable name of “Efficiency”. There is no need, by the way, to convert the strings to number: coercion is automatic in Lua.

All you need to do with this code is put it in the Lua section of a scene which is scheduled to run at a similar rate as the temperature readings update, or as often as you need it.

-- VMC efficiency calculation

local ID = {
  Outside = 21,	  -- the new outside air
  Supply  = 20,	  -- the new supply air
  Dirty   = 22,   -- the dirty indoor air
  Result  = 999,  -- PICK YOUR OUTPUT DEVICE ID HERE
}

local SID = {
  temperature = "urn:schemas-micasaverde-com:device:TemperatureSensor:1",
  efficiency  = "urn:micasaverde-com:serviceId:GenericSensor1",
}

local function get_temp (id)
  local t = luup.variable_get (SID.temperature,"CurrentTemperature",id)
  return t
end

local outside_temp  = get_temp (ID.Outside)
local supply_temp   = get_temp (ID.Supply)
local dirty_temp    = get_temp (ID.Dirty)

if dirty_temp > outside_temp then
  local VMC_efficiency   = ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200
  luup.variable_set (SID.efficiency, "Efficiency", VMC_efficiency, ID.Result)
end

-----

More sophisticated operation can be added once this is working.

Big thank
Your are right , i will try your solution when i Come at home
Your are the chief :wink:

PS: sorry for my bad english

hi @akbooer
I realized the scene as you told me write (well I think) and unfortunately it does not work .

As screen is better than any speech , I have put several catches.

where I have a doubt , it is in the choice of " device action "

I modified the code as such

[code]-- VMC efficiency calculation

local ID = {
Outside = 21, – the new outside air
Supply = 20, – the new supply air
Dirty = 22, – the dirty indoor air
Result = 17, – PICK YOUR OUTPUT DEVICE ID HERE
}

local SID = {
temperature = “urn:schemas-micasaverde-com:device:TemperatureSensor:1”,
efficiency = “urn:micasaverde-com:serviceId:GenericSensor1”,
}

local function get_temp (id)
local t = luup.variable_get (SID.temperature,“CurrentTemperature”,id)
return t
end

local outside_temp = get_temp (ID.Outside)
local supply_temp = get_temp (ID.Supply)
local dirty_temp = get_temp (ID.Dirty)

if dirty_temp > outside_temp then
local VMC_efficiency = ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200
luup.variable_set (SID.efficiency, “Efficiency”, VMC_efficiency, ID.Result)
end[/code]

Moreover , I fully agree with you on the choice to reuse the universal device module but it appears to me ( arm / disarm ), how can I remove it ?

I expect that it is working, and that when run (with reasonable input values) it will produce a new device variable on your chosen output device.

[ul][li]you will not see that value displayed on the device panel unless you do something different.[/li]
[li]I note that your input temperatures are almost the same[/li]
[li]why do you have a device action set in your scene?[/li][/ul]

If you actually want to see the result then, indeed, you will have to choose an existing device type which displays a variable and write the result to there. A generic device will do, but your initial suggestion of humidity would also work.

OK, i have try to creat a device

Apps → Develop apps → Create device

Device type = urn:schemas-micasaverde-com:device:HumiditySensor:1 Description = Rendement VMC Upnp Device Filename = D_HumiditySensor1.xml

Reload engin

I have shoot the “device action” in the scene

But nothing on the device when i start the scene

I have put “urn:schemas-micasaverde-com:device:HumiditySensor:1” instead of “urn:micasaverde-com:serviceId:GenericSensor1” in the code but nothing
Maybe the problem come with the new device config !!?

PS 1: The three temp are same because i have planned to put them in my VMC when the efficiency device will work, so they are on my office :wink: next to each other
PS 2: Do you know how i can delete a device I can’t watch on the device panel (i creat him by error an i have luup error now ID25) Thank
→ GET_LANG(system_error,System error) : Device: 25. Fail to load implementation file D_HumiditySensor1.json

Since you are now using a Humidity device, you have to write to the Humidity variable in order to see it displayed (with the right serviceId too.)

You can delete any device with an HTTP request documented here http://wiki.micasaverde.com/index.php/Luup_Requests#device

Thanks, it is right i am a newbie :o

So i must put

luup.variable_set("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", ID 23)

Somewhere
23 Is the ID of my device and the ID of result

Just for my mind

[code]-- VMC efficiency calculation

local ID = {
Outside = 21, – the new outside air
Supply = 20, – the new supply air
Dirty = 22, – the dirty indoor air
Result = 23, – PICK YOUR OUTPUT DEVICE ID HERE
}

local SID = {
temperature = “urn:schemas-micasaverde-com:device:TemperatureSensor:1”,
efficiency = “urn:schemas-micasaverde-com:device:HumiditySensor:1”,
}

local function get_temp (id)
local t = luup.variable_get (SID.temperature,“CurrentTemperature”,id)
return t
end

local outside_temp = get_temp (ID.Outside)
local supply_temp = get_temp (ID.Supply)
local dirty_temp = get_temp (ID.Dirty)

if dirty_temp > outside_temp then
local VMC_efficiency = ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200
luup.variable_set (SID.efficiency, “Efficiency”, VMC_efficiency, ID.Result)
end[/code]

No, just change this line

luup.variable_set (SID.efficiency, "Efficiency", VMC_efficiency, ID.Result)

to this

luup.variable_set (SID.efficiency, "CurrentLevel", VMC_efficiency, ID.Result)

it don’t work to with this

[code]-- VMC efficiency calculation

local ID = {
Outside = 21, – the new outside air
Supply = 20, – the new supply air
Dirty = 22, – the dirty indoor air
Result = 23, – PICK YOUR OUTPUT DEVICE ID HERE
}

local SID = {
temperature = “urn:schemas-micasaverde-com:device:TemperatureSensor:1”,
efficiency = “urn:schemas-micasaverde-com:device:HumiditySensor:1”,
}

local function get_temp (id)
local t = luup.variable_get (SID.temperature,“CurrentTemperature”,id)
return t
end

local outside_temp = get_temp (ID.Outside)
local supply_temp = get_temp (ID.Supply)
local dirty_temp = get_temp (ID.Dirty)

if dirty_temp > outside_temp then
local VMC_efficiency = ((supply_temp - outside_temp)/(dirty_temp - outside_temp))*200
luup.variable_set (SID.efficiency, “CurrentLevel”, VMC_efficiency, ID.Result)
end[/code]

It’s so infuriating, the solution is almost found
Maybe a parameter in the virtual device !!!

This is my fault… I gave you the wrong serviceIds in the first place. It is the bane of all developers for UPnP…

local SID = {
  temperature = "urn:upnp-org:serviceId:TemperatureSensor1",
  efficiency  = "urn:micasaverde-com:serviceId:HumiditySensor1",
}
This is my fault... I gave you the wrong serviceIds in the first place. It is the bane of all developers for UPnP...

And this is ONE of the reasons for the success of PLEG

Hello,
I’m sorry for my absence but I had a weekend extremely loaded with Hellfest … in France :wink:

So I have created the device and realized the scene but it does not work yet.
I searched a lot before I realized that there was a condition in the code (dirty air > Outdoor air)
Necessarily , the condition is not met by my devices.
I’ll install the probes tonight and see if it works

Sure, I 'll let you know directly