Page 1 of 1
Energy in dummy device in LUA
Posted: Monday 06 March 2017 19:29
by journeyman
Hello,
I have working Energy usage device (Dummy based on Pulse and ESP Easy).
In my home system I would like to se in IMperiHOME my daily usage. So the shortest way is to create another device

- Bez tytułu.png (29.07 KiB) Viewed 5625 times
How in LUA can I take Daily usage from my first counter and put it to second "Daily" one?
Re: Energy in dummy device in LUA
Posted: Monday 06 March 2017 23:44
by supermat
Hello,
I made something similar but with two tarif.
Sorry, it's in french :
https://matdomotique.wordpress.com/2017 ... f-virtuel/
Re: Energy in dummy device in LUA
Posted: Tuesday 07 March 2017 10:14
by journeyman
Unfortunately, completely I do not understand French but that is exactly what I need. Only if it will work with pulse counter from ESPEasy?
I would like to have insight into total consumption, monthly, daily, along with the enumeration of costs - divided into two fares. Tariff change me quite often.
Re: Energy in dummy device in LUA
Posted: Tuesday 07 March 2017 11:16
by journeyman
I try to use your script, I put my ID devices. but I get an error
Code: Select all
2017-03-07 11:09:00.279 Error: EventSystem: in Elektrownia: [string "function log(p_message)..."]:21: bad argument #1 to 'pairs' (table expected, got nil)
2017-03-07 11:09:12.790 Error: EventSystem: in Elektrownia: [string "function log(p_message)..."]:29: attempt to perform arithmetic on global 'EnergyImportLow' (a nil value)
Re: Energy in dummy device in LUA
Posted: Tuesday 07 March 2017 12:59
by supermat
Hello,
OK I think it is because your "idxCompteurIntelligent" is new and it has now initial value, so it return a nil value.
For the first time, the new CompteurIntelligent need to be initialize.
Can you add these lines before : EnergyImport = EnergyImportLow + EnergyImportHigh
-- For the first time, the value are not initilize in the smart counter, we set 0 by default.
if(EnergyImportLow == nil)then
EnergyImportLow =0
EnergyImportHigh =0
EnergyExportLow = 0
EnergyExportHigh =0
PowerImport = 0
PowerExport = 0
end
It must be use only the first time the script is call.
Re: Energy in dummy device in LUA
Posted: Tuesday 07 March 2017 15:08
by journeyman
Thank you helped.
I ran once, then removed.
Now is:
"EventSystem: Script event triggered"
But do not change the meter reading, is still 0.0
Re: Energy in dummy device in LUA
Posted: Tuesday 07 March 2017 15:18
by journeyman
There was my mistake with one of ID.
Its look like it took to T2 all of my already taken 22kWh. I will make some test now
Thank you!
OK Its working perfect!
What is the easiest way to put some daily/monthly data of usage or costs to another dummy or tex device? To show it in some external system like ImperiHome?
Re: Energy in dummy device in LUA
Posted: Tuesday 07 March 2017 19:05
by supermat
You can show that directly in domoticz. Inside the device.
Envoyé de mon iPhone en utilisant Tapatalk
Re: Energy in dummy device in LUA
Posted: Sunday 19 March 2017 10:22
by Abbadon
supermat wrote:You can show that directly in domoticz. Inside the device.
Envoyé de mon iPhone en utilisant Tapatalk
can you tell us more?
Re: Energy in dummy device in LUA
Posted: Sunday 19 March 2017 13:07
by Abbadon
journeyman wrote:There was my mistake with one of ID.
Its look like it took to T2 all of my already taken 22kWh. I will make some test now
Thank you!
OK Its working perfect!
What is the easiest way to put some daily/monthly data of usage or costs to another dummy or tex device? To show it in some external system like ImperiHome?
idk if that possible in LUA, i made it in bash, take your counter IDX
Add dummy text sensor, take IDX
Code: Select all
#!/bin/bash
data=`curl --silent -is "http://ip:port/json.htm?type=devices&rid=IDX" |grep "CounterToday" |sed 's/"//g' |awk '{print $3}'`
string="http://ip:port/json.htm?type=command¶m=udevice&IDX=120&nvalue=0&svalue="$data"kWh"
curl --silent $string
make that script executable with
and then add to cron like (every 1 min, for 5mins interval change first '*' with '*/5'
Code: Select all
* * * * * sh /home/pi/domoticz/scripts/daily.sh
in imperihome add it, change icon and its ok.
ive made the same script which do the same every day at 23:55 and show me usage from last day (left usage is prev day, right 'this' day)
Re: Energy in dummy device in LUA
Posted: Monday 20 March 2017 15:59
by journeyman
Thats great!

Thank you!
But how can i split it in to T1 and T2? To make calculation and show "Today costs"
Re: Energy in dummy device in LUA
Posted: Monday 20 March 2017 19:26
by Abbadon
no idea, probably need to call database
Re: Energy in dummy device in LUA
Posted: Thursday 13 July 2017 0:44
by Brutus
How can I extract in LUA the daily usage of a energie sensor?
I want to extract the daily solar power generation and send it through a notification with maybe some other information.
A simple extraction like this code will not work because it gives me only the total energy and current power.
Code: Select all
sEnergyPower, sEnergyTotal = otherdevices_svalues['Energie']:match("([^;]+);([^;]+)")
I can't extract the part from the French LUA because I don't understand it.
Re: Energy in dummy device in LUA
Posted: Thursday 03 August 2017 9:40
by journeyman
After yesterday update to Beta 3.8197 This counter with T1 T2 is not working anymore
Im getting this in log
Code: Select all
2017-08-03 09:38:00.277 Error: EventSystem: in Elektrownia: [string "function log(p_message)..."]:21: bad argument #1 to 'pairs' (table expected, got nil)
What can I do to solve that? Its stop working after update
Code: Select all
function log(p_message)
-- print("[HPHC] " .. p_message)
end
function getdevname4idx(deviceIDX)
for i, v in pairs(otherdevices_idx) do
if v == deviceIDX then
return i
end
end
return 0
end
commandArray = {}
DEBUG = false
local nomCompteurPrincipal = getdevname4idx(129) --OWL
local idxCompteurIntelligent = 133
local nomCompteurIntelligent = getdevname4idx(idxCompteurIntelligent)--HPHC
local nomHeureCreuse = getdevname4idx(134) -- Switch Heures Creuses
for deviceName,deviceValue in pairs(devicechanged) do
if (deviceName==nomCompteurPrincipal) then
-- On récupère la valeur du compteur unique => consEnergy
ConsPower, consEnergy = string.match(otherdevices_svalues[nomCompteurPrincipal], "(%d+%.*%d*);(%d+%.*%d*)")
log(ConsPower.."-"..consEnergy)
--on somme les compteurs HP et HC => EnergyImport
--http://www.domoticz.com/wiki/Upload_energy_data_to_PVoutput
EnergyImportLow, EnergyImportHigh, EnergyExportLow, EnergyExportHigh, PowerImport, PowerExport = otherdevices_svalues[nomCompteurIntelligent]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
EnergyImport = EnergyImportLow + EnergyImportHigh
EnergyExport = EnergyExportLow + EnergyExportHigh
log(" ----- PowerImport = " .. PowerImport .. " W");
log(" ----- EnergyImportLow = " .. EnergyImportLow .. " Wh");
log(" ----- EnergyImportHigh = " .. EnergyImportHigh .. " Wh");
log(" ----- EnergyImport = " .. EnergyImport .. " Wh");
log(" ----- PowerExport = " .. PowerExport .. " W");
log(" ----- EnergyExportLow = " .. EnergyExportLow .. " Wh");
log(" ----- EnergyExportHigh = " .. EnergyExportHigh .. " Wh");
log(" ----- EnergyExport = " .. EnergyExport .. " Wh");
--On calcule le delta avec le compteur actuel
delta = consEnergy - EnergyImport
--On ajoute le delta sur le bon tarif
if (otherdevices[nomHeureCreuse] == 'On') then
EnergyImportLow = EnergyImportLow + delta
else
EnergyImportHigh = EnergyImportHigh + delta
end
--On modifie le compteur intelligent
commandArray['UpdateDevice'] = idxCompteurIntelligent.."|0|"..EnergyImportLow..";"..EnergyImportHigh..";0;0;"..ConsPower..";0"
end
end
return commandArray
Re: Energy in dummy device in LUA
Posted: Sunday 11 February 2018 11:25
by jeroenkl
Brutus wrote: ↑Thursday 13 July 2017 0:44
How can I extract in LUA the daily usage of a energie sensor?
I want to extract the daily solar power generation and send it through a notification with maybe some other information.
A simple extraction like this code will not work because it gives me only the total energy and current power.
Code: Select all
sEnergyPower, sEnergyTotal = otherdevices_svalues['Energie']:match("([^;]+);([^;]+)")
I can't extract the part from the French LUA because I don't understand it.
Hi Brutus,
Just came along your update and need the answer as well. Did you get an update or/and do you know to get the daily usage with LUA?
Hope you can help me.
regards,
Jeroen
Re: Energy in dummy device in LUA
Posted: Sunday 11 February 2018 14:11
by McMelloW
@Brutus, @Jeroenkl
This is not in Lua but in dzVents 2.4. You can try the following
Code: Select all
local Energy = domoticz.devices(Energie).rawData
domoticz.devices(Power).updateCounter(Energy[1])
domoticz.devices(TotalEnergy).updateCounter(Energy[2])
Of course this is somewhere in the middle of a dzVents script.
This works for me to parse all 6 values form the Electricity devicce of the P1 Smart Meter and update to different devices.
Correct me if I am wrong