Page 1 of 1

Stop a device if power too low

Posted: Wednesday 09 November 2022 10:03
by Patricen
Hello,

I'm trying to set-up a script that would stop a device if the power consumption is below a minimum level for a few minutes (practically, I want to switch off the smart plug if the devices that are connected to it are in standby mode).

I'm trying to implement this using dzVents but no success as I do have issues extracting the actual power from the device (Zwave plug NAS-WR01Z Power plug 12A+).
Any clue?
Thanks!

Code: Select all

return 
{
    on =
    {
        devices = 
        {
            'Bureau-kWh Meter',
        },
    timer = {'Every 10 seconds'},
    },
    
    --data =
    --{
    --    lastupdated-- =
        --{
        --    initial = 0,
        --},
    --},

    logging = 
    {
        level = domoticz.LOG_DEBUG, --LOG_DEBUG, -- change to domoticz.LOG_ERROR when script is tested and OK
        marker = 'Bureau-kWh Meter',
    },

    execute = function(dz, item)
        local Time = require('Time')
        local pwr = dz.devices('Bureau-kWh Meter').actualWatt
        local lowp
        local seuil = 8
        local lastupdated = Time
                
        if pwr > seuil then
			    dz.log('=========================consommation utilisation')
			    lowp = 0
			    lastupdated = Time(dz.time)
			    dz.log('=========================consommation utilisation' .. lastupdated)
                    -- on s'arrĂȘte lĂ 
            --return
        end
        if pwr < seuil  then
                dz.log('=========================basse consommation')
                if lowp == 1 then
                lastupdated = Time(dz.time)
                lowp = l
                dz.log('=========================basse consommation' .. lastupdated)
                return
        end
    end
end
}

Re: Stop a device if power too low

Posted: Wednesday 09 November 2022 12:31
by waltervl
So what is exactly your problem?
Do you get an error? Is the value pwr empty?
Is your device supporting the actualWatt function?

Re: Stop a device if power too low

Posted: Thursday 10 November 2022 6:34
by Patricen
Hello,
Below is the log. 700 is the id of the device, I've no idea if it supports actualWatt function, but for sure there is a power feedback on Domoticz control panel.

Code: Select all

2022-11-10 06:29:23.079 Status: dzVents: Info: Bureau-kWh Meter: ------ Start internal script: Stop bureau: Device: "Bureau-kWh Meter (OpenZWave)", Index: 700
2022-11-10 06:29:23.080 Status: dzVents: Info: Bureau-kWh Meter: =========================consommation utilisation
2022-11-10 06:29:23.080 Status: dzVents: Info: Bureau-kWh Meter: ------ Finished Stop bureau
2022-11-10 06:29:23.080 Error: dzVents: Error: (3.1.8) Bureau-kWh Meter: An error occurred when calling event handler Stop bureau
2022-11-10 06:29:23.080 Error: dzVents: Error: (3.1.8) Bureau-kWh Meter: /home/pi/domoticz/dzVents/runtime/Time.lua:28: bad argument #1 to 'match' (string expected, got table)

Re: Stop a device if power too low

Posted: Thursday 10 November 2022 22:27
by waltervl
Read the docs about using time
https://www.domoticz.com/wiki/DzVents:_ ... ime_object

I think line

Code: Select all

local lastupdated = Time

should be

Code: Select all

local lastupdated = Time()

Re: Stop a device if power too low

Posted: Saturday 12 November 2022 0:13
by HvdW
Maybe this post viewtopic.php?p=293865#p293865 is helpful for you.
If you have any questions: ask

Re: Stop a device if power too low

Posted: Monday 14 November 2022 18:59
by Patricen
waltervl wrote: Wednesday 09 November 2022 12:31 So what is exactly your problem?
Do you get an error? Is the value pwr empty?
Is your device supporting the actualWatt function?
Do you have any idea about the way to find if a device supports actualWatt, or any other function?

Re: Stop a device if power too low

Posted: Tuesday 15 November 2022 0:27
by waltervl
Find the device type in the device widget and then look the device up in the DzVents documentation.
Also check the wiki with device examples https://www.domoticz.com/wiki/Dummy_for ... counter.29

Re: Stop a device if power too low

Posted: Tuesday 15 November 2022 14:01
by Patricen
waltervl wrote: Tuesday 15 November 2022 0:27 Find the device type in the device widget and then look the device up in the DzVents documentation.
Also check the wiki with device examples https://www.domoticz.com/wiki/Dummy_for ... counter.29
Ok it looks like the issue is related to
local pwr = dz.devices('Bureau-kWh Meter').WhActual
that leads to the following error : Error: dzVents: Error: (3.1.8) Bureau-kWh Meter: /home/pi/domoticz/dzVents/runtime/Time.lua:28: bad argument #1 to 'match' (string expected, got table)

as
dz.log('WhActual ' .. dz.devices('Bureau-kWh Meter').WhActual .. ' W')
gives the following log Status: dzVents: Info: Bureau-kWh Meter: WhActual 46.12 W

If I can go over the mismatch between string and table -that I do need help for-, I do figure out that there will be an issue afterwards when comparing a string to an integer, but this will be another story...

Re: Stop a device if power too low

Posted: Wednesday 16 November 2022 16:19
by waltervl
From the dzVents documentation
https://www.domoticz.com/wiki/DzVents:_ ... _scripting
Electric usage
actualWatt: Number. Current Watt usage.
WhActual: Number. Current Watt usage. (please use actualWatt)
So what if you use

Code: Select all

dz.devices('Bureau-kWh Meter').actualWatt

Re: Stop a device if power too low

Posted: Wednesday 16 November 2022 16:34
by waltervl
Additional, do you have more devices named 'Bureau-kWh Meter' as DzVents cannot handle multiple device with the same name. Device names should be unique!

Re: Stop a device if power too low

Posted: Thursday 17 November 2022 18:59
by Patricen
waltervl wrote: Wednesday 16 November 2022 16:34 Additional, do you have more devices named 'Bureau-kWh Meter' as DzVents cannot handle multiple device with the same name. Device names should be unique!
No, this is the only one

Re: Stop a device if power too low

Posted: Thursday 17 November 2022 19:50
by waltervl
Did you try .actualWatt instead of .WhActual as advised by the documentation?

Re: Stop a device if power too low

Posted: Saturday 19 November 2022 18:30
by Patricen
Hello, yes, I tried, and I finally found out that the issue is coming from the pretty tricky time functions.
local Time = require('Time')
The issue is now clearer and I need to have power below x Watts for a "certain time" that I'm not able to code...