Extract Energy values to local variable

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Extract Energy values to local variable

Post by EdddieN »

Hello,

I used to have this LUA script working on the stable version, but not anymore on the beta so trying to migrate it to dzVents. Below is my poor mans attempt so far:

Code: Select all

--[[ 
   updateEnergy
]] --

return {
	on = { 
		--devices = { 'House Energy'} , 
        		timer = { 'every 1 minutes' },             -- timer or device to trigger the script. Can be both or one of them           
    },
	
    execute = function(domoticz, device, trigger)
	
local d1_sensor = 'House Energy'
local d2_sensor = 'Oil meter (A)'

local d1_values = otherdevices_svalues[d1_sensor]
local d2_values = otherdevices_svalues[d2_sensor]

local power,energy = d1_values:match("([^,]+);([^,]+)")
local datastring = "d1," .. tostring(energy) ..";d2," .. tostring(d2_values) 

print (datastring)

local myUrl= 'customuserurl' .. datastring
                domoticz.openURL(myUrl)
                
end
}
I looked into the dzVents wiki but could not find a way to read the value of a meter. It was reference to update its current value for a virtual device, but not to read it. Any ideas?
11101101 - www.machinon.com
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract Energy values to local variable

Post by waaren »

@EdddieN

it depends on what kind of meter you have. For most meters a dzVents device adapter is available.

you can always find if such an adapter is available (and much more) by using domoticz.devices(nnn).dump() in your script
or in dzVents >= 2.4

Code: Select all

local _ = domoticz.utils._
_.print(domoticz.devices(nnn)._adapters) 
domoticz.log( domoticz.devices(nnn).deviceType .. "; state: " .. domoticz.devices(nnn).state)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Extract Energy values to local variable

Post by dannybloe »

And of course, if a new adapter is needed I'm happy to add it. The more the merrier.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Extract Energy values to local variable

Post by EdddieN »

Thanks, but I think I'm been a bit clumsy here, you mean something like this:

Code: Select all

return {
	on = { 		
        	timer = { 'every 1 minutes' },             -- timer or device to trigger the script. Can be both or one of them           
    },	
    execute = function(domoticz, device, trigger)
	
local energy_meter = domoticz.utils.d1_sensor
energy_meter.print(domoticz.devices(energy)._adapters) 
domoticz.log( domoticz.devices(energy).deviceType .. "; state: " .. domoticz.devices(energy).state)
print ("ENERGY ============= " ..energy_meter)
11101101 - www.machinon.com
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract Energy values to local variable

Post by waaren »

@EddieN,

No my code snippet was literal; you only have to replace the nnn with the actual device numbers.

it would help me understand if you could send me the original lua and a screenprint of the [SETUP] --> Devices screen for the devices at hand
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Extract Energy values to local variable

Post by EdddieN »

Of course! Thanks.

This my device (idx 130)
Screen Shot 2018-05-30 at 20.01.33.png
Screen Shot 2018-05-30 at 20.01.33.png (105.02 KiB) Viewed 2256 times
and I took your code and replaced the nnn value:

Code: Select all

local _ = domoticz.utils._
_.print(domoticz.devices(130)._adapters) 
domoticz.log( domoticz.devices(130).deviceType .. "; state: " .. domoticz.devices(130).state)
which gives me a LUA error code: attempt to index field 'utils' (a nil value)

what I'm trying to do is just to push that value (kWh from the house meter) to an external server, something like
local myurl = 'http://www.server.com/savemydata.php?data=' .. ThekWhEnergyfromHouseEnergy
domoticz.openURL(myUrl)

that way I can see the house energy usage without having to have remote access to Domoticz.
11101101 - www.machinon.com
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Extract Energy values to local variable

Post by waaren »

@EddieN,

OK sounds not to complicated but I first have to know how dzVents see this device. Can you please send me the log output of this script.

Code: Select all

return {
    on      =   { timer = { 'every minute' }},
    logging =   { level =   domoticz.LOG_DEBUG, marker = "updateEnergy" },   

    execute = function(dz, trigger)
        local houseEnergyDevice = dz.devices("House Energy")
        houseEnergyDevice.dump()
        dz.log("\n\nDevicetype     :" .. houseEnergyDevice.deviceType .. 
               "\nDevicesubtype: :" .. houseEnergyDevice.deviceSubType .."\n",dz.LOG_DEBUG)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: Extract Energy values to local variable

Post by EdddieN »

Thanks a lot for helping on this.

I had this working using LUA except for the Co2 sensor:
Spoiler: show
local d1_values = otherdevices_svalues['House Energy']
local d2_values = otherdevices_svalues['Oil meter (A)']
local d3_values = otherdevices_utility['Co2']
local d4_values = otherdevices_svalues['TH / RH Co2']

local t1 = os.date("%M")
-- returns a date time like 2013-07-11 17:23:12

commandArray = {}

if t1%10 == 0 then

local power,energy = d1_values:match("([^,]+);([^,]+)")
print ("ENERGY ======> " ..energy)
local temperature,humidity = d4_values:match("([^,]+);([^,]+);([^,]+)")
print ("TEMP ======== ".. temperature)
print ("humidity===== ".. humidity)

end

return commandArray
Until I found that that for svalue was working for meters but not Co2, it had to be utility...
Anyway, since dzVents seems a lot easier to use, I was hoping to replicate the above in dzVents but cannot get my head around how to read the svalue of devices in dzVents. That is all I need :)
11101101 - www.machinon.com
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest