Wemos D1 mini (v4) + EasyESP > Energy Meter with persistent data

Everything about esp8266 and more.

Moderator: leecollings

Post Reply
Sateetje
Posts: 24
Joined: Monday 07 December 2015 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Wemos D1 mini (v4) + EasyESP > Energy Meter with persistent data

Post by Sateetje »

Because EasyESP doesn't support EEPROM variable storage. I've created two scripts to retrieve the last TotalWh from Domoticz when EasyESP is connected to Wifi.

What this does:
- When EasyESP gets conntect to Wifi, it starts a 10sec timer
- This timer starts updating a variable in Domoticz with his IP Address and the IDX configured at the Device (Energy)
- When Wh is retrieved (> 0), the timer stops
- The script supports Minimum Wattage, to reset Watt is 0 when 0 pulses are received

I hope this will people help connecting EasyESP to Domoticz when "persistent" data is needed, like Wh in this case. This is more accurate than the option "Computed" in Domoticz. Any comments are welcome.

Config EasyESP:
- Create Generic - Pulse counter (Name = EnergyMeter, Counter Type = Delta/Total/Time, Single event with all values = TRUE)
- Create Generic - Dummy Device (Name = Energy, Output Data Type = Dual, Values: 1=Watt [0 decimals] 2=Wh [1 decimals])
- In this case you must use "Send to Controller" on the "Generic - Dummy Device" to update the Domoticz device

Config Domoticz:
- Create virtual device "Energy (Actual + Counter)"
- Set "Energy Read" to "From Device" at the device

Rules:

Code: Select all

On System#Boot Do  
  // Settings
  Let,0,800     // Pulses per kWh (2000 = 2 pulse/Wh)
  Let,1,10       // Minimum Watt
  Let,2,192     // Domoticz IP first byte
  Let,3,168     // Domoticz IP second byte
  Let,4,100     // Domoticz IP third byte
  Let,5,13       // Domoticz IP fourth byte
  Let,6,8080   // Domoticz Port

  // Calculate when timer needs to expire
  Let,20,(3600*(1000/[int#0]))/[int#1]
  Let,21,0

  // Store Domoticz IDX, Controller needs to be enabled
  Let,30,[Energy#settings.Controller1.Idx]
  
  // Disable controller on boot and enable when data is received from Domoticz
  ControllerDisable,1
Endon

On WiFi#Connected Do
  LoopTimerSet,1,10
Endon

On EnergyMeter#all Do
  If %eventvalue1% > 0 And [Energy#Wh] > 0
    TimerSet,2,0
    Let,21,0
    
    // Actual Watt
    TaskValueSet,Energy,Watt,(3600000*(1000/[int#0]))/%eventvalue3%
    
    // Actual Watt smaller than Minimum Watt? Set to 0
    If [Energy#Watt] < [int#1]
       TaskValueSet,Energy,Watt,0
    Endif
    
    // Wh
    TaskValueSet,Energy,Wh,[Energy#Wh]+((1000/[int#0])*%eventvalue1%)
  Elseif %eventvalue3% = 0
    // Start timer to reset Watt
    If [int#21] = 0
      TimerSet,2,[int#20]
      Let,21,1
    Endif
  Endif
Endon

On Rules#Timer=1 Do
  // Timer to get initial data from Domoticz
  If [Energy#Wh] > 0 
    LoopTimerSet,1,0
  
    // Enable controller for updating Domoticz
    ControllerEnable,1  
    
    // Need to save settings to activate the Controller
    Save
  Else
    // Request Wh Total from Domoticz
    SendToHTTP [int#2].[int#3].[int#4].[int#5],[int#6],/json.htm?type=command&param=updateuservariable&vname=ESPEasyWifiConnect&vtype=String&vvalue=%ip%|[int#30]
  Endif
Endon

On Rules#Timer=2 Do
  Let,21,0

  // Timer to reset Watt to 0 after expiry (min. Watt)
  TaskValueSet,Energy,Watt,0
Endon

On UpdateTotal Do
  // Set Watt to 0
  TaskValueSet,2,1,0
  
  // Received Wh
  TaskValueSet,2,2,%eventvalue%
Endon
Config Domoticz:
- Create a Variable named ESPEasyWifiConnect as String
- Create a dzVents script

Domoticz Script:

Code: Select all

return
{
    on =
    {
        variables = { 'ESPEasyWifiConnect' },
        httpResponses = { 'ESPEasyUpdate' }
    },
    logging = {
        level = domoticz.LOG_INFO,
        marker = "esp_easy_wifi_connect"
        },
    execute = function(domoticz, item)
        if (item.isVariable) then
            local utils = require("Utils")
            local IPAddressAndIDX = utils.stringSplit(item.value,"|")
            
            local IPAddress = IPAddressAndIDX[1]
            domoticz.log("IPAddress: " .. IPAddress)
            
            local IDX = tonumber(IPAddressAndIDX[2])
            domoticz.log("IDX: " .. IDX)
            
            local Wh = domoticz.devices(IDX).WhTotal
            domoticz.log("Wh: " .. Wh)
            
            if (Wh <= 0) then
                Wh = 1 -- at least 1 Wh to stop requests from ESP Easy
                
                domoticz.log("Wh is 0 or lower, will send 1 instead to stop requesting")
            end
        
            local urlWh = 'http://' .. IPAddress .. '/control?cmd=event,UpdateWh=' .. Wh

            domoticz.log(urlWh)
                        
            domoticz.openURL({
            url = urlWh,
            method = 'GET',
            callback = 'ESPEasyUpdate'
            })
        elseif (item.isHTTPResponse) then
            domoticz.log(item.callback .. ": " .. item.statusCode .. "-" .. item.statusText)
        end            
    end
}
Last edited by Sateetje on Wednesday 13 December 2023 11:36, edited 12 times in total.
User avatar
themistervac
Posts: 1
Joined: Tuesday 09 January 2018 13:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Wemos D1 mini (v4) + EasyESP > Energy Meter with persistent data

Post by themistervac »

Hello

I was intrigued by your script because I installed a Norax 3 electricity meter.
I wanted to use it but I have a question.

What device to create in Domoticz ?
Energy meter, incremental counter ?

Where is name of Domiticz device we created in script ?

What this script line should look like

Is the IPAddress the IP espEasy?

local urlWh = 'http://' .. IPAddress .. '/control?cmd=event,UpdateWh=' .. Wh
local urlIDX = 'http://' .. IPAddress .. '/control?cmd=event,UpdateIDX=' .. IDX

Is this correct

In IP I changed . with , due to this forum restrictions.

local urlWh = 'http://10,1,1,100/control?cmd=event,UpdateWh='
local urlIDX = 'http://10,1,1,100/control?cmd=event,UpdateIDX='

Can you share with full information.

Thaknks in advance.
Sateetje
Posts: 24
Joined: Monday 07 December 2015 15:22
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Wemos D1 mini (v4) + EasyESP > Energy Meter with persistent data

Post by Sateetje »

Please check the updated post.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest