<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Virtual_meter</id>
	<title>Virtual meter - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Virtual_meter"/>
	<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Virtual_meter&amp;action=history"/>
	<updated>2026-09-19T00:25:00Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://wiki.domoticz.com/index.php?title=Virtual_meter&amp;diff=15986&amp;oldid=prev</id>
		<title>Walter vl: /* Create virtual meter */</title>
		<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Virtual_meter&amp;diff=15986&amp;oldid=prev"/>
		<updated>2021-05-17T14:13:18Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Create virtual meter&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Smart meters only measure net electricity consumption/production, i.e. Electricity consumption - Electricity production. This script subtracts electricity production from smart meter readings to get electricity consumption. It&amp;#039;s triggered each time the smart meter or PV meter update.&lt;br /&gt;
&lt;br /&gt;
=Create virtual meter=&lt;br /&gt;
&lt;br /&gt;
#Create dummy hardware and device&lt;br /&gt;
##If no Dummy Hardware already available, go to Setup -&amp;gt; Hardware. Else go to step 3 &lt;br /&gt;
###Name: anything, e.g. &amp;quot;Virtual Devices&amp;quot; (this name is only used internally)&lt;br /&gt;
###Type: &amp;quot;Dummy (does nothing, use for virtual switches only)&amp;quot;&lt;br /&gt;
##Click on &amp;#039;Add&amp;#039;&lt;br /&gt;
##Click on &amp;#039;Create Virtual Sensors&amp;#039; from the table of hardware above&lt;br /&gt;
###Name: anything, e.g. &amp;quot;Power Consumption&amp;quot; (this name is displayed on the dashboard etc.)&lt;br /&gt;
###Sensor type: &amp;quot;Electric (Instant+Counter)&amp;quot;&lt;br /&gt;
##Click &amp;#039;Ok&amp;#039;&lt;br /&gt;
#Get name/id of relevant sensors&lt;br /&gt;
##Note down the name of your smart meter and solar meter devices (not hardware)&lt;br /&gt;
##Note the name and ID of your virtual sensor&lt;br /&gt;
#Update and copy the script below&lt;br /&gt;
##Update meter / sensor names&lt;br /&gt;
##Go to Setup -&amp;gt; More Options -&amp;gt; Events&lt;br /&gt;
##Enter name (e.g. PowerConsumption), Select &amp;#039;Lua&amp;#039; and &amp;#039;Device&amp;#039;&lt;br /&gt;
##Toggle event active, save&lt;br /&gt;
#Update utility/dashboard accordingly&lt;br /&gt;
&lt;br /&gt;
=Lua script=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 -- Power consumption device script - SunnyBoy 20180523&lt;br /&gt;
 --&lt;br /&gt;
 -- Smart meters only measure net electricity consumption/production, i.e. &lt;br /&gt;
 -- Electricity consumption - Electricity production. This script subtracts &lt;br /&gt;
 -- electricity production from smart meter readings to get electricity &lt;br /&gt;
 -- consumption. It&amp;#039;s triggered each time the smart meter or PV meter update.&lt;br /&gt;
 --&lt;br /&gt;
 -- 1. Create dummy hardware and device&lt;br /&gt;
 -- 1.1 Go to Setup -&amp;gt; Hardware &lt;br /&gt;
 --     Name: anything, e.g. &amp;quot;Ele_Consumption&amp;quot; (this name is only used internally)&lt;br /&gt;
 --     Type: &amp;quot;Dummy (does nothing, use for virtual switches only)&amp;quot;&lt;br /&gt;
 -- 1.2 Click on &amp;#039;Add&amp;#039;&lt;br /&gt;
 -- 1.3 Click on &amp;#039;Create Virtual Sensors&amp;#039; from the table of hardware above&lt;br /&gt;
 --     Name: anything, e.g. &amp;quot;Power Consumption&amp;quot; (this name is displayed on the dashboard etc.)&lt;br /&gt;
 --     Sensor type: &amp;quot;Electric (Instant+Counter)&amp;quot;&lt;br /&gt;
 -- 1.4 Click &amp;#039;Ok&amp;#039;&lt;br /&gt;
 -- 2. Get name/id of relevant sensors&lt;br /&gt;
 -- 2.1. Note down the name of your smart meter and solar meter devices (not hardware)&lt;br /&gt;
 -- 2.2. Note the name and ID of your virtual sensor&lt;br /&gt;
 -- 3. Update and copy the script below&lt;br /&gt;
 -- 3.1. Update meter / sensor names&lt;br /&gt;
 -- 3.2. Go to Setup -&amp;gt; More Options -&amp;gt; Events&lt;br /&gt;
 -- 3.3. Enter name (e.g. PowerConsumption), Select &amp;#039;Lua&amp;#039; and &amp;#039;Device&amp;#039;&lt;br /&gt;
 -- 3.4. Toggle event active, save&lt;br /&gt;
 -- 4. Update utility/dashboard accordingly&lt;br /&gt;
 &lt;br /&gt;
 commandArray = {}&lt;br /&gt;
 debug = false&lt;br /&gt;
 &lt;br /&gt;
 -- for debugging, use this file&lt;br /&gt;
 if (debug) then&lt;br /&gt;
     file = io.open(&amp;quot;/home/pi/lua_test.log&amp;quot;, &amp;quot;a&amp;quot;) -- append&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 -- Update these lines before use&lt;br /&gt;
 smartmeter = &amp;quot;Power (net)&amp;quot; -- name of smart meter device&lt;br /&gt;
 solarmeter = &amp;quot;Solar Power&amp;quot; -- name of solar meter device&lt;br /&gt;
 dummymeter = &amp;quot;Power Consumption&amp;quot; -- name of virtual sensor&lt;br /&gt;
 dummymeter_id = 20 -- ID of virtual sensor&lt;br /&gt;
 dummymeter_trigger = solarmeter -- name of the meter to trigger the dummy meter update. Use the slowest meter here: if your smart meter updates every 10 sec and PV every 100 sec, you get 9 values using potentially outdated PV data. In my case cloudy days gave negative consumption because of this.&lt;br /&gt;
 &lt;br /&gt;
 -- debug to check what devices are named (but better use &amp;#039;Show current states&amp;#039; in&lt;br /&gt;
 -- online editor)&lt;br /&gt;
 if (debug) then&lt;br /&gt;
     file:write(&amp;#039;other devices states:\n&amp;#039;)&lt;br /&gt;
     for i, v in pairs(otherdevices) do file:write(i, v) end&lt;br /&gt;
     file:write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
     file:write(&amp;#039;other devices svalues:\n&amp;#039;)&lt;br /&gt;
     for i, v in pairs(otherdevices_svalues) do file:write(i, v) end&lt;br /&gt;
     file:write(&amp;quot;\n&amp;quot;)&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 -- loop through all the changed devices, only update during day if solar meter&lt;br /&gt;
 -- is triggered, or during the night if smart meter is triggered.&lt;br /&gt;
 for deviceName,deviceValue in pairs(devicechanged) do&lt;br /&gt;
     if (debug) then&lt;br /&gt;
         file:write(&amp;quot;Device based event fired on &amp;#039;&amp;quot;..deviceName..&amp;quot;&amp;#039;, value &amp;#039;&amp;quot;..tostring(deviceValue)..&amp;quot;&amp;#039;\n&amp;quot;);&lt;br /&gt;
     end&lt;br /&gt;
     &lt;br /&gt;
     -- Looks like: Device based event fired on &amp;#039;Power (net)&amp;#039;, value &amp;#039;2058750;1175597;790812;1911744;251;0&amp;#039;&lt;br /&gt;
     -- Syntax: USAGE1;USAGE2;RETURN1;RETURN2;CONS;PROD https://www.domoticz.com/wiki/Domoticz_API/JSON_URL&amp;#039;s#Electricity_P1_smart_meter&lt;br /&gt;
     -- Counter: T1: 2058.750, T2: 1175.671&lt;br /&gt;
     -- Counter Return: R1: 790.812, R2: 1911.744&lt;br /&gt;
     -- Or: Device based event fired on &amp;#039;Solar Power&amp;#039;, value &amp;#039;0.000;3755165.000&amp;#039;&lt;br /&gt;
     -- Counter: 3755.165&lt;br /&gt;
     -- Time of script consumption counter: 2058.750 + 1175.671 + 3755.165 - 790.812 - 1911.744&lt;br /&gt;
     if (deviceName == solarmeter and timeofday[&amp;#039;Daytime&amp;#039;]) or (deviceName == smartmeter and timeofday[&amp;#039;Nighttime&amp;#039;]) then&lt;br /&gt;
         -- Smart meter: Match six integers separated by semicolum&lt;br /&gt;
         -- first four are energy meters, last two are power meters?&lt;br /&gt;
         smart_e1, smart_e2, smart_e3, smart_e4, smart_p1, smart_p2 = otherdevices_svalues[smartmeter]:match(&amp;quot;([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)&amp;quot;)&lt;br /&gt;
         -- Solar meter power;counter: 0.000;3755165.000&lt;br /&gt;
         solar_p, solar_e = otherdevices_svalues[solarmeter]:match(&amp;quot;([^;]+);([^;]+)&amp;quot;)&lt;br /&gt;
         &lt;br /&gt;
         -- Calculate power consumption and update dummy device. Consumption is &lt;br /&gt;
         -- always positive, so we add our solar power to the smart meter power&lt;br /&gt;
         -- if solar generates 1kW, smart meter shows -0.5kW, real consumption is 0.5kW (=solar - return)&lt;br /&gt;
         -- if solar generates 1kW, smart meter shows 0.5kW, real consumption is 1.5kW (=solar + consumption)&lt;br /&gt;
         consumption_p = smart_p1 + solar_p - smart_p2&lt;br /&gt;
         consumption_e = smart_e1 + smart_e2 + solar_e - smart_e3 - smart_e4&lt;br /&gt;
         &lt;br /&gt;
         -- Populate commandArray with update command&lt;br /&gt;
         commandArray[1] = {[&amp;#039;UpdateDevice&amp;#039;] = dummymeter_id .. &amp;quot;|0|&amp;quot; .. consumption_p .. &amp;quot;;&amp;quot; .. consumption_e}&lt;br /&gt;
         if (debug) then&lt;br /&gt;
             file:write(&amp;quot;Update device&amp;quot;.. dummymeter_id .. &amp;quot;|0|&amp;quot; .. consumption_p .. &amp;quot;;&amp;quot; .. consumption_e .. &amp;quot;\n&amp;quot;);&lt;br /&gt;
         end&lt;br /&gt;
     end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 if (debug) then&lt;br /&gt;
     file:close()&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 return commandArray&lt;br /&gt;
&lt;br /&gt;
=References= &lt;br /&gt;
&lt;br /&gt;
*[https://www.domoticz.com/forum/viewtopic.php?t=18705 Virtual meter that is the sum of two other meters - forum thread]&lt;br /&gt;
*[https://www.domoticz.com/forum/viewtopic.php?t=10559 Create virtual hardware - forum thread]&lt;br /&gt;
*[https://www.domoticz.com/wiki/Scripting_in_Domoticz#Lua_scripts Scripting in Domoticz - Lua scripts]&lt;br /&gt;
*[https://www.domoticz.com/wiki/Events#Lua_.28.26_Python.29_Interface Events interface in Domoticz]&lt;/div&gt;</summary>
		<author><name>Walter vl</name></author>
	</entry>
</feed>