Page 1 of 1

Another washing machine

Posted: Friday 20 January 2023 13:55
by Varazir
Hi,
I have searched and found some example scripts but they look way to complex and I have a hard time to follow them.
Example this one viewtopic.php?t=23798
Have a combo machine so both dryer and washer.

I have one plug that also measure consumption.
Name:
"Tvättmaskin: Current Watt Meter" ( Current watt )
"Tvättmaskin" (Status: On or Off)

This is the pattern for my machine is this ( 1515 - 1930 )
Image

0 watt = off
>6 watt = low cycle
2200 watt = High cycle

So I created this, gives me a error "unexpected symbol" }
But will this work?

Code: Select all

return {
	on = {
		devices = {
			'Tvättmaskin: Current Watt Meter'
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'Tvättmaskin',
	},
	data ={ 
        CurrentWatt = { initial = 0 }
    },
	execute = function(dz, device)
	if device.actualWatt > 0 then
        dz.data.CurrentWatt = device.actualWatt
	else
        if dz.data.CurrentWatt > 0 and dz.data.CurrentWatt.lastUpdate.minutesAgo then
            dz.notify("Tvätt klar",1,dz.PRIORITY_NORMAL,dz.NSS_HTTP)
            dz.data.CurrentWatt = 0 
        end
    end
}

Re: Another washing machine

Posted: Friday 20 January 2023 14:03
by waltervl
There is a double "return {" on the top of your script. 1 will do ;-)

Re: Another washing machine

Posted: Friday 20 January 2023 14:05
by Varazir
waltervl wrote: Friday 20 January 2023 14:03 There is a double "return {" on the top of your script. 1 will do ;-)
Checked that is just a copy past error.

Re: Another washing machine  [Solved]

Posted: Friday 20 January 2023 14:24
by waltervl
i think you are missing an end

Re: Another washing machine

Posted: Friday 20 January 2023 16:48
by lost
Varazir wrote: Friday 20 January 2023 13:55 So I created this, gives me a error "unexpected symbol" }
A long time ago, when I learned C programming language, compilers were very slow... So the habit to use "linters" to have a basic check before running the compiler. Errors were also easier to understand from linter output than from complier.

Now, that's no more useful for natively compiled programs (compiler/build chains are quick on current hardware+compiler output more straightforward to understand errors, if any). But for anything scripted/interpreted, I'm really back using linters again: I really miss what the compiler verifies before execution... that is only done during execution on such languages.

So, for Lua, I advise you to install this package:

Code: Select all

sudo apt install lua-check
Then, in your domoticz/lua, run "luacheck" with the source file to check as an argument. Take care that package and executable name differ by a hyphen.

You'll have some warnings due to Domoticz Lua support (like commandArray: will be seen as an non-standard global variable) that is unknown from linter but errors should be spotted much more easily than at execution!

For python, there's pylint also. This one usually needs a bit configuration to avoid being to much verbose on some style python wants to enforce (like functions/variables etc naming in the form my_toto_function instead of more concise myTotoFunction I personally prefer).

If you use internal event editor, as you have no script file (everything in DB), this will not be possible: You may have to migrate your script as a separate one, using the right naming (time or device script, see exemples in lua directory). Or just copy in a file just for the check!

This makes me think Lua linter integration may be a nice addition to internal event interface. Maybe worth a suggestion.

Re: Another washing machine

Posted: Saturday 21 January 2023 11:57
by gizmocuz
This is the script I use for my dryer/washingmachine. Maybe it is of some use

Code: Select all

-- Script to check if appliances are ready. Based on the work of Felix63
-- In this script the 'SC-Wasdroger verbruik' is the energy device, and 'Wasdroger' is a virtual dummy switch
local USAGE_DEVICES = {
    ['SC-Wasdroger verbruik'] = 'Wasdroger',		-- You need to have a inline wall plug that measures energy,
    ['SC-Wasmachine verbruik'] = 'Wasmachine',  	-- here you make the link between the energy device and the wall plug.
}

local USAGE_SwitchTimeOutMinutes = {
    ['Wasdroger'] = 10,						-- Here you define how long no power is used per device.
    ['Wasmachine'] = 5,						-- The period is in minutes. Adjust to your needs. Between every add a ",".
}

local USAGE_MaxWatt = {
    ['Wasdroger'] = 150,						-- Here you define the maximum amount of power a device uses when it is in standby.
    ['Wasmachine'] = 3,						-- Some devices uses a little amount of power. Test it and a slightly higher usage.
}

local USAGE_Notify = {
    ['Wasdroger'] = 'Yes',						-- In some cases you want to be notified when a device is turned on and off.
    ['Wasmachine'] = 'Yes',					-- Adjust to your needs. Between every line you need to add a ",".
}

return {
 	logging = {
-- 		level = domoticz.LOG_INFO, 			-- Uncomment to override the dzVents global logging setting
-- 		marker = 'POW'
 	},

 	on = {
		timer = { 'every 1 minutes' },
 		devices = {						-- Make sure that the devices are the same as above
 			'SC-Wasdroger verbruik',
 			'SC-Wasmachine verbruik',
 		},
 	},
 	data = { 								-- use exact device names to match USAGE_DEVICES
 		['SC-Wasdroger verbruik'] = { history = true, maxMinutes = 10 },
 		['SC-Wasmachine verbruik'] = { history = true, maxMinutes = 10 }
 	},

 	execute = function(domoticz, device)
		for usage, machine in pairs(USAGE_DEVICES) do
            local actual = domoticz.devices(usage).WhActual
 			local threshold = USAGE_MaxWatt[machine]
            local switch = domoticz.devices(machine)
            domoticz.data[usage].add(actual)
            local history = domoticz.data[usage]
            local average = history.avg()
            local timeout = USAGE_SwitchTimeOutMinutes[machine]

--            domoticz.log(machine .. " actual = " .. actual)
--            domoticz.log(machine .. " average = " .. tostring(average))
--            domoticz.log(machine .. " switch state = " .. tostring(switch.active))
--            domoticz.log(machine .. " switch last update: " .. tostring(switch.lastUpdate.minutesAgo))

            if (switch.active) then
                if (history.avg() <= threshold and actual <= threshold and switch.lastUpdate.minutesAgo >= timeout) then
                    domoticz.log(machine .. " machine in idle state, we assume it's ready, turning switch off.")
                    switch.switchOff().checkFirst()
                    domoticz.data[usage].reset()
                end
            else
                if (actual > threshold) then
                    domoticz.log(machine .. " power usage is over treshold, turning switch on.")
                    switch.switchOn().checkFirst()
                end
            end
        end
	end
}
 
And on the switch I set a notification (actually I call a script to announce to Alexa it's ready)

Re: Another washing machine

Posted: Saturday 21 January 2023 20:36
by Varazir
gizmocuz wrote: Saturday 21 January 2023 11:57 This is the script I use for my dryer/washingmachine. Maybe it is of some use

And on the switch I set a notification (actually I call a script to announce to Alexa it's ready)
Ya I saw that in the post I linked.
I started to modify it to work for me but as I said it was complex.

I haven't tested mine yet.


I'm not sure bout the minutesAgo on the variabler

Re: Another washing machine

Posted: Thursday 26 January 2023 17:16
by Varazir
waltervl wrote: Friday 20 January 2023 14:24 i think you are missing an end
I'm getting this error

2023-01-26 16:44:08.193 Error: dzVents: Error: (3.1.8) Tvättmaskin: An error occurred when calling event handler Tvättmaskin
2023-01-26 16:44:08.193 Error: dzVents: Error: (3.1.8) Tvättmaskin: ...oticz/scripts/dzVents/generated_scripts/Tvättmaskin.lua:18: attempt to index a number value (field 'CurrentWatt')

Code: Select all

return {
	on = {
		devices = {
			'Tvättmaskin: Current Watt Meter'
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'Tvättmaskin',
	},
	data ={ 
        CurrentWatt = { initial = 0 }
    },
	execute = function(dz, device)
    	if device.actualWatt > 0 then
            dz.data.CurrentWatt = device.actualWatt
    	else
            if dz.data.CurrentWatt > 0 and dz.data.CurrentWatt.lastUpdate.minutesAgo then
                dz.notify("Tvätt klar",1,dz.PRIORITY_NORMAL,dz.NSS_HTTP)
                dz.data.CurrentWatt = 0 
            end
        end
    end
}

Re: Another washing machine

Posted: Thursday 26 January 2023 23:22
by waltervl
I don't know if data supports .lastupdate.minutesago I thought it was only for Domoticz devices (but I could be wrong and too lazy to go to the wiki).

Re: Another washing machine

Posted: Friday 27 January 2023 13:58
by Varazir
I'll remove it and see what happens, should be fine. I was just trying to prevent looping but setting the variable to 0 it should ignore it.

Code: Select all

return {
	on = {
		devices = {
			'Tvättmaskin: Current Watt Meter'
		}
	},
	logging = {
		marker = 'Tvättmaskin',
	},
	data ={ 
        CurrentWatt = { initial = 0 }
    },
	execute = function(dz, device)
    if device.actualWatt > 0 then
            dz.data.CurrentWatt = device.actualWatt
    	else
            if dz.data.CurrentWatt > 0 then
                dz.notify("Tvätt klar",1,dz.PRIORITY_NORMAL,dz.NSS_HTTP)
                dz.data.CurrentWatt = 0 
            end
        end
    end
}