Page 1 of 3
Persistent data as a variable
Posted: Thursday 07 December 2017 20:35
by wizzard72
I'm building a general script. The script stores persistent data.
In the first part of the script I have defined the persistent data:
Code: Select all
return {
active = true,
logging = {
level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
marker = 'TEST '
},
on = {
devices = {
'Wasmachine (W)',
'Wasmachine',
'Slaapkamer Paul Schakelaar',
'Slaapkamer Paul Schakelaar (W)',
'Dimmer WC 1ste',
'Dimmer WC 1ste (W)',
'Dimmer WC BG',
'Dimmer WC BG (W)',
'Espressomachine',
'Espressomachine (W)'
},
},
data = {
WattWasmachine = { history = true, maxMinutes = 6 },
WattSlaapkamerPaulSchakelaar = { history = true, maxMinutes = 10 },
WattDimmerWC1ste = { history = true, maxMinutes = 10 },
WattDimmerWCBG = { history = true, maxMinutes = 10 },
WattEspressomachine = { history = true, maxMinutes = 10 }
},
The main part of the script contains code to store the actual KwH:
domoticz.data.WattWasmachine.add(sensor.WhActual)
This works. But I want more flexibility.
The part 'WattWasmachine' in 'domoticz.data.WattWasmachine.add(sensor.WhActual)' is the part that changes. So for my 5 persistent data entries I have to create 5 lines of code:
Code: Select all
domoticz.data.WattWasmachine.add(sensor.WhActual)
domoticz.data.WattSlaapkamerPaulSchakelaar.add(sensor.WhActual)
domoticz.data.WattDimmerWC1ste.add(sensor.WhActual)
domoticz.data.WattDimmerWCBG.add(sensor.WhActual)
domoticz.data.WattEspressomachine.add(sensor.WhActual)
I would rather have something like this:
Code: Select all
domoticz.data.persistentdata(WattWasmachine).add(sensor.WhActual)
So 'WattWasmachine' becomes more like a variable. With variables I can do cool stuff. Like:
Code: Select all
WattDevice = "Watt" .. string.gsub(device.name, "%s+", "")
domoticz.data.persistentdata(WattDevice).add(sensor.WhActual)
Is something like this possible? Or over time in a new version?
Re: Persistent data as a variable
Posted: Thursday 07 December 2017 20:55
by dannybloe
There’s no need actually. Just create a table with all your data and put that in your persistent variable. ...data.myvar.add({ a=1, b=2 })
You can access it like: ...data.myVar.a or data.myVar.b.
Re: Persistent data as a variable
Posted: Thursday 07 December 2017 21:02
by dannybloe
Well, it's not entirely correct what I wrote. You are using historical data so that's a bit different:
Adding:
Code: Select all
domoticz.data.myVar.add({ a=1, b=2, c=3})
Using:
Code: Select all
local latest = domoticz.data.myVar.getLatest()
print(latest.b)
-- update
latest.b = latest.b + 1
domoticz.data.myVar.add( latest ) -- add a new table to the history.
But, you only have one history chain for myVar. Not per item in the table. So if you need the historical data then you do need separate variables.
Re: Persistent data as a variable
Posted: Thursday 07 December 2017 22:02
by wizzard72
I have a little trouble understanding it.
My code:
Code: Select all
return {
active = true,
logging = {
level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
marker = 'TEST '
},
on = {
devices = {
'Wasmachine (W)',
'Wasmachine',
'Slaapkamer Paul Schakelaar',
'Slaapkamer Paul Schakelaar (W)',
'Dimmer WC 1ste',
'Dimmer WC 1ste (W)',
'Dimmer WC BG',
'Dimmer WC BG (W)',
'Espressomachine',
'Espressomachine (W)'
},
},
data = {
WattWasmachine = { history = true, maxMinutes = 6 },
WattSlaapkamerPaulSchakelaar = { history = true, maxMinutes = 10 },
WattDimmerWC1ste = { history = true, maxMinutes = 10 },
WattDimmerWCBG = { history = true, maxMinutes = 10 },
WattEspressomachine = { history = true, maxMinutes = 10 }
},
-- actual event code
-- in case of a timer event or security event, device == nil
execute = function(domoticz, device)
-- Variables to customize ------------------------------------------------
-- Variables -------------------------------------------------------------
local CurrentTime = os.time()
-- Function --------------------------------------------------------------
-- MAIN ------------------------------------------------------------------
if (device.state == "On") then
domoticz.notify("ACTIVATE",device.name .. " activated#I let you know when device " .. device.name .. " is turned off",2,SOUND_SIREN)
elseif (device.state == "Off") then
domoticz.notify("DEACTIVATED",device.name .. " is deactivated",2,SOUND_SIREN)
end
domoticz.changedDevices().forEach(function(device)
if (device.name == "Wasmachine (W)") then
-- Store KwH in persistent data
domoticz.data.WattWasmachine.add(device.WhActual)
-- calculate average
local average = domoticz.data.WattWasmachine.avg()
-- if last 10 reading Watt is 0, then switchoff the device
if (average == 0) then
domoticz.devices('Wasmachine').switchOff().checkFirst()
end
elseif (device.name == "Slaapkamer Paul Schakelaar (W)") then
-- Store KwH in persistent data
domoticz.data.WattSlaapkamerPaulSchakelaar.add(device.WhActual)
-- calculate average
local average = domoticz.data.WattSlaapkamerPaulSchakelaar.avg()
-- if last 10 reading Watt is 0, then switchoff the device
if (average == 0) then
domoticz.devices('Slaapkamer Paul Schakelaar').switchOff().checkFirst()
end
elseif (device.name == "Dimmer WC 1ste (W)") then
-- Store KwH in persistent data
domoticz.data.WattDimmerWC1ste.add(device.WhActual)
-- calculate average
local average = domoticz.data.WattDimmerWC1ste.avg()
-- if last 10 reading Watt is 0, then switchoff the device
if (average == 0) then
domoticz.devices('Dimmer WC 1ste').switchOff().checkFirst()
end
elseif (device.name == "Dimmer WC BG (W)") then
-- Store KwH in persistent data
domoticz.data.WattDimmerWCBG.add(device.WhActual)
-- calculate average
local average = domoticz.data.WattDimmerWCBG.avg()
-- if last 10 reading Watt is 0, then switchoff the device
if (average == 0) then
domoticz.devices("Dimmer WC BG").switchOff().checkFirst()
end
elseif (device.name == "Espressomachine (W)") then
-- Store KwH in persistent data
domoticz.data.WattEspressomachine.add(device.WhActual)
-- calculate average
local average = domoticz.data.WattEspressomachine.avg()
-- if last 10 reading Watt is 0, then switchoff the device
if (average == 0) then
domoticz.devices('Espressomachine').switchOff().checkFirst()
end
end
end)
end
}
I want to simplify my code. I have a lot of 'elseif' statements. Every 'elseif' statement is almost the same, except the device/sensor name. My logic says that it can be made easyer

Re: Persistent data as a variable
Posted: Friday 08 December 2017 8:46
by dannybloe
Ok, here is my simplified version (for dzVents 2.3.0). Couple of comments. You don't have to use the changed devices collection anymore (I'm removing it from the documentation for 2.4.0) as there is always only one device in that collection and that's the device that was changed (second parameter of your execute function). So, I created a lookup table to match the energy device to the switch and made sure the persistent variables exactly match the name of the energy device (and removed some unnecessary code and corrected some errors):
Code: Select all
-- create a lookup table that matches a usage
-- device to the accompanying switch
local USAGE_DEVICES = {
['Wasmachine (W)'] ='Wasmachine',
['Slaapkamer Paul Schakelaar (W)']= 'Slaapkamer Paul Schakelaar',
['Dimmer WC 1ste (W)'] = 'Dimmer WC 1ste',
['Dimmer WC BG (W)'] = 'Dimmer WC BG',
['Espressomachine (W)'] = 'Espressomachine'
}
return {
logging = {
level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
marker = 'TEST '
},
on = {
devices = {
'Wasmachine (W)',
'Wasmachine',
'Slaapkamer Paul Schakelaar',
'Slaapkamer Paul Schakelaar (W)',
'Dimmer WC 1ste',
'Dimmer WC 1ste (W)',
'Dimmer WC BG',
'Dimmer WC BG (W)',
'Espressomachine',
'Espressomachine (W)'
},
},
data = { -- use exact device names to match USAGE_DEVICES
['Wasmachine (W)'] = { history = true, maxMinutes = 6 },
['Slaapkamer Paul Schakelaar (W)'] = { history = true, maxMinutes = 10 },
['Dimmer WC 1ste (W)'] = { history = true, maxMinutes = 10 },
['Dimmer WC BG (W)'] = { history = true, maxMinutes = 10 },
['Espressomachine (W)'] = { history = true, maxMinutes = 10 }
},
execute = function(domoticz, device)
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = domoticz.devices(USAGE_DEVICES[device.name])
local history = domoticz.data[device.name]
history.add(device)
if (switch.active and history.avg() == 0) then
switch.switchOff()
end
else
-- device is a switch
if (device.active) then
domoticz.notify(
"ACTIVATE",
device.name .. " activated#I let you know when device " .. device.name .. " is turned off",
domoticz.PRIORITY_EMERGENCY,
domoticz.SOUND_SIREN
)
else
domoticz.notify(
"DEACTIVATED",
device.name .. " is deactivated",
domoticz.PRIORITY_EMERGENCY,
domoticz.SOUND_SIREN
)
end
end
end
}
Disclaimer: this is untested code
Side note: I'm wondering what the logic is for this script. If there's no energy usage for a device in the past 10 minutes isn't the device then also switched off? Or doesn't the switch report back to Domoticz its current state (like a Kaku switch)?
Re: Persistent data as a variable
Posted: Friday 08 December 2017 10:05
by wizzard72
Thanks for the simplified version. I'm going to analyse your code.
The purpose of the script is to disable devices that don't use any energy. For example: when my washing machine is finished and it uses 0 watt for 6 minutes, the the device is turned off and I get a message when ready. So I can never forget to hang my clothes to dry. I can't assume that one 0 watt means that the washing machine is finished. Sometimes the machine doesn't do anything, the zwave device measure 0 watt, but its not finished yet. Thats why I want a few minutes 0 watt. Then I know for sure its finished.
Re: Persistent data as a variable
Posted: Friday 08 December 2017 10:13
by dannybloe
Interesting. I can do the same for my washing machine.
Re: Persistent data as a variable
Posted: Friday 08 December 2017 12:11
by wizzard72
dannybloe wrote:Interesting. I can do the same for my washing machine.
Glad I could help

Re: Persistent data as a variable
Posted: Wednesday 14 November 2018 22:44
by pvklink
Hi,
I used the above script and did some modifications.
I get my notifications double, with the above script and with my own modifications...
Somebody an idea?
The rest of the script is working great!
Code: Select all
-- https://www.domoticz.com/forum/viewtopic.php?t=20786#p160725
-- https://github.com/Wizzard72/domoscript/blob/master/dz_0-energy-turn-device-off
local USAGE_DEVICES = {
['tvslaapkamer_kwh'] = 'tvslaapkamer' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchTimeOutMinutes = {
['tvslaapkamer'] = 1 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['tvslaapkamer'] = 4 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['tvslaapkamer'] = 'Yes' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['tvslaapkamer'] = 'Yes'
}
return {
--logging = {
-- level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
-- marker = '0-energy'
--},
on = {
devices = { -- Make sure that the devices are the same as above
'tvslaapkamer_kwh'
},
},
data = { -- use exact device names to match USAGE_DEVICES
['tvslaapkamer_kwh'] = { history = true, maxMinutes = 10 }
},
execute = function(domoticz, device)
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = domoticz.devices(USAGE_DEVICES[device.name])
local history = domoticz.data[device.name]
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
local onoff = USAGE_SwitchOnOff[USAGE_DEVICES[device.name]]
local notify = USAGE_Notify[USAGE_DEVICES[device.name]]
domoticz.log("timeout = " .. timeout)
domoticz.log("watt = " .. watt)
history.add(device.WhActual)
if (switch.active and history.avg() <= watt and switch.lastUpdate.minutesAgo >= timeout) then
if (onoff == 'Yes') then
switch.switchOff().checkFirst()
end
if (notify == "Yes") then
print ('TV is klaar') -- This notification is printed twice in the log !!!!!! ?????
--domoticz.notify(
-- "DEACTIVATED",
-- device.name .. " is klaar!",
-- domoticz.PRIORITY_EMERGENCY,
-- domoticz.SOUND_SIREN
--)
end
end
end
end
}
Re: Persistent data as a variable
Posted: Wednesday 14 November 2018 23:20
by waaren
pvklink wrote: Wednesday 14 November 2018 22:44
I get my notifications double, with the above script and with my own modifications...
Somebody an idea?
My guess is that you won't see the log twice if you change
Code: Select all
if (onoff == 'Yes') then
switch.switchOff().checkFirst()
end
to
Code: Select all
if (onoff == 'Yes') then
switch.switchOff().checkFirst().silent()
end
Re: Persistent data as a variable
Posted: Wednesday 14 November 2018 23:24
by pvklink
ok, Thanks!
Still another question:
When i dont turn the switch off, but only give a notification that the device is ready (like the wasmachine is done washing) is my script then continously giving notications ?
Re: Persistent data as a variable
Posted: Thursday 15 November 2018 0:47
by waaren
pvklink wrote: Wednesday 14 November 2018 23:24
ok, Thanks!
Still another question:
When i dont turn the switch off, but only give a notification that the device is ready (like the wasmachine is done washing) is my script then continously giving notications ?
Can you show us what you see if you do a
Code: Select all
sudo cat "your domoticz dir"/scripts/dzVents/data/*"your script name"*
if you receive this continous stream of notifications ?
I suspect that the device still reports (small) energy usage causing the script to trigger. The logic in the script is that this will cause a notification on every time the device does this.
Re: Persistent data as a variable
Posted: Thursday 15 November 2018 23:12
by pvklink
Hi try to save values as uservariables... they do not change,
get --> local sensor = tonumber(domoticz.variables('sensor1').value)
set --> domoticz.variables('status').set(tostring(history.avg()))
Code: Select all
-- https://www.domoticz.com/forum/viewtopic.php?t=20786#p160725
-- https://github.com/Wizzard72/domoscript/blob/master/dz_0-energy-turn-device-off
local USAGE_DEVICES = {
['tvslaapkamer_kwh'] = 'tvslaapkamer' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchTimeOutMinutes = {
['tvslaapkamer'] = 1 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['tvslaapkamer'] = 2 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['tvslaapkamer'] = 'Yes' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['tvslaapkamer'] = 'Yes'
}
return {
--logging = {
-- level = domoticz.LOG_INFO, -- Uncomment to override the dzVents global logging setting
-- marker = '0-energy'
--},
on = {
devices = { -- Make sure that the devices are the same as above
'tvslaapkamer_kwh'
},
},
data = { -- use exact device names to match USAGE_DEVICES
['tvslaapkamer_kwh'] = { history = true, maxMinutes = 10 }
},
execute = function(domoticz, device)
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = domoticz.devices(USAGE_DEVICES[device.name])
local history = domoticz.data[device.name]
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
local onoff = USAGE_SwitchOnOff[USAGE_DEVICES[device.name]]
local notify = USAGE_Notify[USAGE_DEVICES[device.name]]
local sensor = tonumber(domoticz.variables('sensor1').value)
domoticz.log("timeout = " .. timeout)
domoticz.log("watt = " .. watt)
history.add(device.WhActual)
print('ERR01:Sensor '.. sensor .. ' xxx')
if (sensor >= watt and history.avg() <= watt and switch.lastUpdate.minutesAgo >= timeout) then
if (onoff == 'Yes') then
switch.switchOff().checkFirst() -- krijg ik niet heel veel emails als ik de switch niet off zet?
end
if (notify == "Yes") then
--print ('TV is klaar') -- This notification is printed twice in the log !!!!!! ????? krijg je als emailverzend is ontvangs ???
domoticz.notify(
"DEACTIVATED",
device.name .. " is klaar!",
domoticz.PRIORITY_EMERGENCY,
domoticz.SOUND_SIREN
)
end
domoticz.variables('status').set(tostring(history.avg()))
elseif history.avg() > watt then
domoticz.variables('status').set(tostring(history.avg()))
else
-- doe niets is waarschijnlijk al uit of zit in de timer
end
end
end
}
Re: Persistent data as a variable
Posted: Friday 16 November 2018 0:44
by waaren
pvklink wrote: Thursday 15 November 2018 23:12
Hi try to save values as uservariables... they do not change,
get --> local sensor = tonumber(domoticz.variables('sensor1').value)
set --> domoticz.variables('status').set(tostring(history.avg()))
The fact that the value does not change does not guarantee that the script is not triggered. If the device is updated with the exact same value the script wil still trigger. If you have a problem doing the sudo cat command I asked for in my previous reply, please send me a PM.
Re: Persistent data as a variable
Posted: Friday 16 November 2018 7:16
by pvklink
i will try it in the weekend.
thanks for so far..
Re: Persistent data as a variable
Posted: Saturday 17 November 2018 20:10
by pvklink
I changed my script from lua to dzvents.
Still got two emails when i use the normal notification.
When using the active notification with pushover i also get two notifications on my device!!
Code: Select all
-- https://www.domoticz.com/forum/viewtopic.php?t=20786#p160725
-- https://github.com/Wizzard72/domoscript/blob/master/dz_0-energy-turn-device-off
-- uservariabele sensor en sensoraction door data parameters vervangen!!!
local USAGE_DEVICES = {
['nachtlamp_kwh'] = 'nachtlamp' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchTimeOutMinutes = {
['nachtlamp'] = 10 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['nachtlamp'] = 2 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['nachtlamp'] = 'Yes' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['nachtlamp'] = 'Yes'
}
local USAGE_Pushoverreceivers = {
['nachtlamp'] = ''
}
return {
logging = {
level = domoticz.LOG_INFO -- Uncomment to override the dzVents global logging setting
},
on = {
devices = { -- Make sure that the devices are the same as above
'nachtlamp_kwh'
},
},
data = { -- use exact device names to match USAGE_DEVICES
sensor = {initial=0},
sensoraction = {initial='Off'}
},
execute = function(domoticz, device)
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = domoticz.devices(USAGE_DEVICES[device.name])
local history = domoticz.data[device.name]
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
local onoff = USAGE_SwitchOnOff[USAGE_DEVICES[device.name]]
local notify = USAGE_Notify[USAGE_DEVICES[device.name]]
local receivers = USAGE_Pushoverreceivers[USAGE_DEVICES[device.name]]
local huidwaarde = device.WhActual
domoticz.log("00 Sensor device = " .. device.name)
domoticz.log("00 Sensor onoff = " .. onoff)
domoticz.log("00 Sensor notify = " .. notify)
domoticz.log("00 Sensor timeout = " .. timeout)
domoticz.log("00 Sensor watt = " .. watt)
domoticz.log("00 Sensor vorige waarde= " .. tostring(domoticz.data.sensor))
domoticz.log("00 Sensor huidwaarde = " .. tostring(huidwaarde))
domoticz.log("00 Sensor sensoraction = " .. tostring(domoticz.data.sensoraction))
domoticz.log("---------------------------------------")
if (huidwaarde <= watt and domoticz.data.sensor <= watt) then -- check dat ze er beide onder zitten zodat n piekje niet direct iets veranderd
domoticz.log("01 Sensor info = Oude en huidige waarde zijn onder norm")
if (domoticz.data.sensoraction == 'On') then
domoticz.log("02 Sensor info = Action staat aan")
if (onoff == 'Yes') then
switch.switchOff().checkFirst().silent()
end
if (notify == "Yes") then
-- domoticz.notify(
-- "Apparaat is uitgezet",
-- device.name .. " en is waarschijnlijk klaar!",
-- domoticz.PRIORITY_EMERGENCY,
-- domoticz.SOUND_SIREN
-- )
domoticz.notify('Domoticz: Nachtlamp is gereed',device.name,PRIORITY_NORMAL,'siren',receivers,domoticz.NSS_PUSHOVER)
end
domoticz.log("03 Sensor info = Action op uit zetten")
domoticz.data.sensoraction = 'Off' -- global en huidige waarde zit onder norm en als status on was dus klaar en weer op off zetten anders meer notificaties
end
elseif (huidwaarde > watt) then
domoticz.log("04 Sensor info = Huidig boven norm dus ophogen en action op on zetten")
domoticz.data.sensoraction = 'On' --aanzetten er is afname gemeten
else
domoticz.log("05 Sensor info = Doe niets") -- doe niets is waarschijnlijk al uit of zit in de timer
end
domoticz.log("06 Sensor info = op huidige waarde zetten")
domoticz.data.sensor = huidwaarde -- waarde zetten altijd
end
end
}
Re: Persistent data as a variable
Posted: Saturday 17 November 2018 21:07
by waaren
pvklink wrote: Saturday 17 November 2018 20:10
hi @waaren
sudo cat /home/pi/domoticz/scripts/dzVents/generated_scripts/nachtlampmeter.lua
on my /home/pi/domoticz/scripts/dzVents/data folder there is no nachtlampmeter.lua, so i took the generated_script folder
My changed my script from lua to dzvents and still got two emails when i use the normal notification. When using the currewnct notification to pushover i also get tw notifications on my device!!
Peter
Code: Select all
-- https://www.domoticz.com/forum/viewtopic.php?t=20786#p160725
-- https://github.com/Wizzard72/domoscript/blob/master/dz_0-energy-turn-device-off
local USAGE_DEVICES = {
['nachtlamp_kwh'] = 'nachtlamp' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchTimeOutMinutes = {
['nachtlamp'] = 10 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['nachtlamp'] = 15 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['nachtlamp'] = 'Yes' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['nachtlamp'] = 'Yes'
}
local USAGE_Pushoverreceivers = {
['nachtlamp'] = ''
}
return {
logging = {
level = domoticz.LOG_INFO -- Uncomment to override the dzVents global logging setting
},
on = {
devices = { -- Make sure that the devices are the same as above
'nachtlamp_kwh'
},
},
data = { -- use exact device names to match USAGE_DEVICES
['nachtlamp_kwh'] = { history = true, maxMinutes = 1 }
},
execute = function(domoticz, device)
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = domoticz.devices(USAGE_DEVICES[device.name])
local history = domoticz.data[device.name]
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
local onoff = USAGE_SwitchOnOff[USAGE_DEVICES[device.name]]
local notify = USAGE_Notify[USAGE_DEVICES[device.name]]
local receivers = USAGE_Pushoverreceivers[USAGE_DEVICES[device.name]]
local sensor = tonumber(domoticz.variables('sensor1').value)
local sensoraction = domoticz.variables('sensor1action').value
local huidwaarde = device.WhActual
local histwaarde = history.avg()
history.add(device.WhActual)
domoticz.log("00 Sensor device = " .. device.name)
domoticz.log("00 Sensor onoff = " .. onoff)
domoticz.log("00 Sensor notify = " .. notify)
domoticz.log("00 Sensor timeout = " .. timeout)
domoticz.log("00 Sensor watt = " .. watt)
domoticz.log("00 Sensor vorige waarde= " .. tostring(sensor))
domoticz.log("00 Sensor huidwaarde = " .. tostring(huidwaarde))
domoticz.log("00 Sensor histwaarde = " .. tostring(histwaarde))
domoticz.log("00 Sensor sensoraction = " .. tostring(sensoraction))
domoticz.log("---------------------------------------")
if (huidwaarde <= watt and sensor <= watt) then -- check dat ze er beide onder zitten zodat n piekje niet direct iets veranderd
domoticz.log("01 Sensor info = Oude en huidige waarde zijn onder norm")
if (sensoraction == 'On') then
domoticz.log("02 Sensor info = Action staat aan")
if (onoff == 'Yes') then
switch.switchOff().checkFirst().silent()
end
if (notify == "Yes") then
-- domoticz.notify(
-- "Apparaat is uitgezet",
-- device.name .. " en is waarschijnlijk klaar!",
-- domoticz.PRIORITY_EMERGENCY,
-- domoticz.SOUND_SIREN
-- )
domoticz.notify('Domoticz: Nachtlamp is gereed',device.name,PRIORITY_NORMAL,'siren',receivers,domoticz.NSS_PUSHOVER)
end
domoticz.log("03 Sensor info = Action op uit zetten")
domoticz.variables('sensor1action').set('Off') -- global en huidige waarde zit onder norm en als status on was dus klaar en weer op off zetten anders meer notificaties
end
elseif (huidwaarde > watt) then
domoticz.log("04 Sensor info = Huidig boven norm dus ophogen en action op on zetten")
domoticz.variables('sensor1action').set('On') --aanzetten er is afname gemeten
else
domoticz.log("05 Sensor info = Doe niets")
-- doe niets is waarschijnlijk al uit of zit in de timer
end
domoticz.log("06 Sensor info = op huidige waarde zetten")
domoticz.variables('sensor1').set(tostring(huidwaarde)) -- waarde zetten altijd
end
end
Send you a PM on this
Re: Persistent data as a variable
Posted: Saturday 17 November 2018 23:36
by pvklink
I solved the problem with the two notifications!
Instead of using uservariables i use variables in the data section.
Still one final issue.
With the local USAGE_DEVICES = {
['nachtlamp_kwh'] = 'nachtlamp'
}
With the above construct i can define more sensors for each local variable. Within the dataconstruct i only have one set of variables. Is that going to be a problem when i add more devices in the local USAGE***** variables ???
Here is de data dump.
dump data part
root@RPI_DOM:/home/pi/domoticz/scripts/dzVents/data# sudo cat /home/pi/domoticz/scripts/dzVents/data/*_data_nachtlampmeter.lua*
-- Persistent Data
local multiRefObjects = {
} -- multiRefObjects
local obj1 = {
["sensor"] = 0;
["sensoraction"] = "Off";
}
return obj1
Code: Select all
-- https://www.domoticz.com/forum/viewtopic.php?t=20786#p160725
-- https://github.com/Wizzard72/domoscript/blob/master/dz_0-energy-turn-device-off
-- uservariabele sensor en sensoraction door data parameters vervangen!!!
local USAGE_DEVICES = {
['nachtlamp_kwh'] = 'nachtlamp' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchTimeOutMinutes = {
['nachtlamp'] = 10 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['nachtlamp'] = 2 -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['nachtlamp'] = 'Yes' -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['nachtlamp'] = 'Yes'
}
local USAGE_Pushoverreceivers = {
['nachtlamp'] = ''
}
return {
logging = {
level = domoticz.LOG_INFO -- Uncomment to override the dzVents global logging setting
},
on = {
devices = { -- Make sure that the devices are the same as above
'nachtlamp_kwh'
},
},
data = { -- use exact device names to match USAGE_DEVICES
sensor = {initial=0},
sensoraction = {initial='Off'}
},
execute = function(domoticz, device)
if (USAGE_DEVICES[device.name] ~= nil) then
-- we have a usage sensor here
local switch = domoticz.devices(USAGE_DEVICES[device.name])
local history = domoticz.data[device.name]
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_DEVICES[device.name]]
local watt = USAGE_MaxWatt[USAGE_DEVICES[device.name]]
local onoff = USAGE_SwitchOnOff[USAGE_DEVICES[device.name]]
local notify = USAGE_Notify[USAGE_DEVICES[device.name]]
local receivers = USAGE_Pushoverreceivers[USAGE_DEVICES[device.name]]
local huidwaarde = device.WhActual
domoticz.log("00 Sensor device = " .. device.name)
domoticz.log("00 Sensor onoff = " .. onoff)
domoticz.log("00 Sensor notify = " .. notify)
domoticz.log("00 Sensor timeout = " .. timeout)
domoticz.log("00 Sensor watt = " .. watt)
domoticz.log("00 Sensor vorige waarde= " .. tostring(domoticz.data.sensor))
domoticz.log("00 Sensor huidwaarde = " .. tostring(huidwaarde))
domoticz.log("00 Sensor sensoraction = " .. tostring(domoticz.data.sensoraction))
domoticz.log("---------------------------------------")
if (huidwaarde <= watt and domoticz.data.sensor <= watt) then -- check dat ze er beide onder zitten zodat n piekje niet direct iets veranderd
domoticz.log("01 Sensor info = Oude en huidige waarde zijn onder norm")
if (domoticz.data.sensoraction == 'On') then
domoticz.log("02 Sensor info = Action staat aan")
if (onoff == 'Yes') then
switch.switchOff().checkFirst().silent()
end
if (notify == "Yes") then
-- domoticz.notify(
-- "Apparaat is uitgezet",
-- device.name .. " en is waarschijnlijk klaar!",
-- domoticz.PRIORITY_EMERGENCY,
-- domoticz.SOUND_SIREN
-- )
domoticz.notify('Domoticz: Nachtlamp is gereed',device.name,PRIORITY_NORMAL,'siren',receivers,domoticz.NSS_PUSHOVER)
end
domoticz.log("03 Sensor info = Action op uit zetten")
domoticz.data.sensoraction = 'Off' -- global en huidige waarde zit onder norm en als status on was dus klaar en weer op off zetten anders meer notificaties
end
elseif (huidwaarde > watt) then
domoticz.log("04 Sensor info = Huidig boven norm dus ophogen en action op on zetten")
domoticz.data.sensoraction = 'On' --aanzetten er is afname gemeten
else
domoticz.log("05 Sensor info = Doe niets") -- doe niets is waarschijnlijk al uit of zit in de timer
end
domoticz.log("06 Sensor info = op huidige waarde zetten")
domoticz.data.sensor = huidwaarde -- waarde zetten altijd
end
end
}
Re: Persistent data as a variable
Posted: Sunday 18 November 2018 3:04
by waaren
pvklink wrote: Saturday 17 November 2018 23:36
With the local USAGE_DEVICES = {
['nachtlamp_kwh'] = 'nachtlamp'
}
With the above construct i can define more sensors for each local variable. Within the dataconstruct i only have one set of variables. Is that going to be a problem when i add more devices in the local USAGE***** variables ???
Yes. To handle this, the script should be modified a bit.
Below is an example of how that could look like.
Code: Select all
local USAGE_SENSORS = {
['kw_1'] = 'kw_1', -- Adjust to your needs. Between every line you need to add a ",".
['kw_2'] = 'kw_2', -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SWITCHES = {
['kw_1'] = 'switch device 1', -- Name of device that switch kw_1 On/ Off
['kw_2'] = 'switch device 2', -- Name of device that switch kw_2 On/ Off
}
local USAGE_SwitchTimeOutMinutes = {
['kw_1'] = 10, -- Adjust to your needs. Between every line you need to add a ",".
['kw_2'] = 10, -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['kw_1'] = 2, -- Adjust to your needs. Between every line you need to add a ",".
['kw_2'] = 2, -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['kw_1'] = 'Yes', -- Adjust to your needs. Between every line you need to add a ",".
['kw_2'] = 'Yes', -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['kw_1'] = 'Yes',
['kw_2'] = 'Yes',
}
local USAGE_Pushoverreceivers = {
['kw_1'] = '',
['kw_2'] = '',
}
return {
on = { devices = USAGE_SENSORS },
logging = { level = domoticz.LOG_DEBUG , -- Uncomment to override the dzVents global logging setting
marker = actOnIdleDevices
},
data = { sensors = { initial = {} } },
execute = function(dz, device)
local name = device.name
if dz.data.sensors[name] == nil then
dz.data.sensors[name] = 0
end
local switch = dz.devices(USAGE_SWITCHES[name])
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_SENSORS[name]]
local watt = USAGE_MaxWatt[USAGE_SENSORS[name]]
local onoff = USAGE_SwitchOnOff[USAGE_SENSORS[name]]
local notify = USAGE_Notify[USAGE_SENSORS[name]]
local receivers = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
local huidwaarde = device.WhActual
dz.log("00 Sensor device = " .. name)
dz.log("00 Sensor onoff = " .. onoff)
dz.log("00 Sensor notify = " .. notify)
dz.log("00 Sensor timeout = " .. timeout)
dz.log("00 Sensor watt = " .. watt)
dz.log("00 Sensor vorige waarde= " .. tostring(dz.data.sensors[name]))
dz.log("00 Sensor huidwaarde = " .. tostring(huidwaarde))
dz.log(dz.data.sensors[name],dz.LOG_DEBUG)
dz.log("---------------------------------------")
if huidwaarde <= watt then
dz.data.sensors[name] = dz.data.sensors[name] + 1
-- check dat ze er beide onder zitten zodat n piekje niet direct iets veranderd
if dz.data.sensors[name] == 2 then
dz.log("01 Sensor info = Oude en huidige waarde zijn onder norm")
if onoff == 'Yes' then
dz.log("02 Sensor info = Action staat aan")
switch.switchOff().checkFirst().silent()
dz.log("03 Sensor info = Action op uit zetten")
end
if notify == 'Yes' then
dz.notify( 'dz: ' .. name .. ' is gereed',
name,
PRIORITY_NORMAL,
'siren',
receivers,
dz.NSS_PUSHOVER
)
end
elseif dz.data.sensors[name] == 1 then
dz.log("04 Sensor info = Nu nog geen actie omdat pas 1 keer de huidige waarde laag was")
elseif dz.data.sensors[name] > 2 then
dz.log("04 Sensor info = Nu geen actie omdat actie al is gedaan")
end
else
dz.data.sensors[name] = 0
dz.log("05 Sensor info = Huidig boven norm dus reset van idlecounter")
end
end
}
Re: Persistent data as a variable
Posted: Sunday 18 November 2018 11:32
by pvklink
@waaren
I changed the variables in the script with 2 devices and both work! THANKKKKKS
Is it ok that USAGE_SENSORS uses the same values on the left and right side of the comparison?
I also used SwitchTimeOutMinutes for the timeout period. i use the counter for this and i adjust the hard values =1, =2 and >2 for the counter
and
i made the notificationsounds flexibel.
Can you have a quick look if this makes sense ?
Only thing i cant get to work is that when my wasmachine_kwh is ready wasmachine is used as text for notify instead of wasmachine_kwh (or other device_kwh names)
Peter
Code: Select all
local USAGE_SENSORS = {
['buro_kwh'] = 'buro_kwh', -- Adjust to your needs. Between every line you need to add a ",".
['wasmachine_kwh'] = 'wasmachine_kwh', -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SWITCHES = {
['buro_kwh'] = 'buro', -- Name of device that switch kw_1 On/ Off
['wasmachine_kwh'] = 'wasmachine', -- Name of device that switch kw_1 On/ Off
}
local USAGE_SwitchTimeOutMinutes = {
['buro_kwh'] = 4, -- Adjust to your needs. Between every line you need to add a ",".
['wasmachine_kwh'] = 4, -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_MaxWatt = {
['buro_kwh'] = 2, -- Adjust to your needs. Between every line you need to add a ",".
['wasmachine_kwh'] = 2, -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_SwitchOnOff = {
['buro_kwh'] = 'Yes', -- Adjust to your needs. Between every line you need to add a ",".
['wasmachine_kwh'] = 'Yes', -- Adjust to your needs. Between every line you need to add a ",".
}
local USAGE_Notify = {
['buro_kwh'] = 'Yes',
['wasmachine_kwh'] = 'Yes',
}
local USAGE_Pushoverreceivers = {
['buro_kwh'] = '',
['wasmachine_kwh'] = '',
}
local USAGE_Pushoversound = {
['buro_kwh'] = 'bike',
['wasmachine_kwh'] = 'bike',
}
return {
on = { devices = USAGE_SENSORS },
logging = { level = domoticz.LOG_DEBUG , -- Uncomment to override the dzVents global logging setting
marker = actOnIdleDevices
},
data = { sensors = { initial = {} } },
execute = function(dz, device)
local name = device.name
if dz.data.sensors[name] == nil then
dz.data.sensors[name] = 0
end
local switch = dz.devices(USAGE_SWITCHES[name])
local timeout = USAGE_SwitchTimeOutMinutes[USAGE_SENSORS[name]]
local watt = USAGE_MaxWatt[USAGE_SENSORS[name]]
local onoff = USAGE_SwitchOnOff[USAGE_SENSORS[name]]
local notify = USAGE_Notify[USAGE_SENSORS[name]]
local receivers = USAGE_Pushoverreceivers[USAGE_SENSORS[name]]
local sound = USAGE_Pushoversound[USAGE_SENSORS[name]]
local huidwaarde = device.WhActual
dz.log("00 Sensor device = " .. name)
dz.log("00 Sensor onoff = " .. onoff)
dz.log("00 Sensor notify = " .. notify)
dz.log("00 Sensor receivers = " .. receivers)
dz.log("00 Sensor sound = " .. sound)
dz.log("00 Sensor timeout = " .. timeout)
dz.log("00 Sensor watt = " .. watt)
dz.log("00 Sensor teller eronder= " .. tostring(dz.data.sensors[name]))
dz.log("00 Sensor huidwaarde = " .. tostring(huidwaarde))
dz.log(dz.data.sensors[name],dz.LOG_DEBUG)
dz.log("---------------------------------------")
if huidwaarde <= watt then
dz.log("01 Sensor info = Oude en huidige waarde zijn onder de norm")
dz.data.sensors[name] = dz.data.sensors[name] + 1
if dz.data.sensors[name] < timeout then
dz.log("02 Sensor info = Nu nog geen actie omdat de timeout limiet nog niet is bereikt")
elseif dz.data.sensors[name] == timeout then
dz.log("03 Sensor info = Oude en huidige waarde zijn onder norm en gelijk aan de timeout dus actie")
if onoff == 'Yes' then
dz.log("03a Sensor info = Oude en huidige waarde zijn onder norm en gelijk aan de timeout, device wordt uitgezet")
switch.switchOff().checkFirst().silent()
end
if notify == 'Yes' then
dz.log("03b Sensor info = Oude en huidige waarde zijn onder norm en gelijk aan de timeout, er wordt een notificatie gestuurd")
dz.notify( 'dz: ' .. name .. ' is gereed',
name,
PRIORITY_NORMAL,
sound,
receivers,
dz.NSS_PUSHOVER
)
end
elseif dz.data.sensors[name] > timeout then
dz.log("04 Sensor info = Oude en huidige waarde zijn onder norm maar acties zijn al gedaan dus niets meer doen")
end
else
dz.data.sensors[name] = 0
dz.log("05 Sensor info = Huidig waarde boven norm dus reset van idlecounter")
end
end
}