Trigger script at specific value

Moderator: leecollings

Post Reply
Enz0jacco
Posts: 45
Joined: Tuesday 03 March 2020 8:15
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Trigger script at specific value

Post by Enz0jacco »

Hey guys

I would like to turn of my tv plug when there is no usage.
I wrote a script and it’s working but the script checks every 10sec or so because the plug sends its usage every 10sec.
Is there a way to trigger the script only when the usage value is below let’s say 20 Watts?
Thnx Jacco


Sent from my iPad using Tapatalk
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Trigger script at specific value

Post by waaren »

Enz0jacco wrote: Wednesday 25 November 2020 21:39 Is there a way to trigger the script only when the usage value is below let’s say 20 Watts?
Yes. If you share the script I can show you how.
Can you also please state the domoticz / dzVents version you use?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Enz0jacco
Posts: 45
Joined: Tuesday 03 March 2020 8:15
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Trigger script at specific value

Post by Enz0jacco »

thnx for answering.
Version: 2020.2 (build 12494)
dzVents Version: 3.0.15

here is the script

Code: Select all

return {
	on = {
		variables = {17},
		devices = {237},
	},
	execute = function(dz, item)
	    local STEKKER = dz.devices(238)
        local TVverbruik = dz.devices(237).actualWatt
        local TVstatusvariable = dz.variables(17)
        

                if  item.isDevice                                   and
                    TVverbruik < 20                                 and
                    TVstatusvariable.value ~= 'Off'                 then
                        dz.openURL('http://127.0.0.1:8084/json.htm?type=command&param=updateuservariable&idx=17&vname=TV&vtype=2&vvalue=Off').afterSec(30)
                        dz.log('TV gaat uit over 30 sec')
                        
                
                elseif  item.isDevice                               and
                        TVverbruik > 20                             and
                        TVstatusvariable.value ~= 'On'              then
                            TVstatusvariable.set('On')
                            cancelQueuedCommands()
                            dz.log('TV is AAN')
                        
                elseif  item.isVariable                             and
                        TVstatusvariable.value == 'Off'             then
                            STEKKER.switchOff().checkFirst()
                            dz.log('Stroom TV uit')
                end
                
                            
end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Trigger script at specific value

Post by waaren »

Enz0jacco wrote: Wednesday 25 November 2020 23:57 Version: 2020.2 (build 12494) / dzVents Version: 3.0.15
Below script should do it. Don't know if you use the TVstatusvariable elsewhere so I left it in but script can do the same without it
using

Code: Select all

STEKKER.switchOff().afterSec(delay)

Code: Select all

return
{
    active = function(domoticz)
                return domoticz.devices(237).actualWatt < 20 
            end,

    on =
    {
        variables =
        {
            17,
        },
        devices =
        {
            237,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all OK
        marker = 'TV control',
    },

    execute = function(dz, item)
        local STEKKER = dz.devices(238)
        local TVstatusvariable = dz.variables(17)
        local delay = 30

        if  item.isDevice and TVstatusvariable.value ~= 'Off' then
            local offTime = dz.time.addSeconds(delay).rawTime
            TVstatusvariable.set('Scheduled for Off at ' .. offTime)
            TVstatusvariable.set('Off').afterSec(delay)
            dz.log('TV gaat uit over ' .. delay .. ' seconden (' .. offTime .. ').', dz.LOG_DEBUG )
        elseif item.isVariable and TVstatusvariable.value == 'Off' then
            STEKKER.switchOff().checkFirst()
            dz.log('Stroom TV uit', dz.LOG_DEBUG )
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Enz0jacco
Posts: 45
Joined: Tuesday 03 March 2020 8:15
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Trigger script at specific value

Post by Enz0jacco »

thnx for the quick reply Waaren!

it is working but now when the tv is off and thus the usage is less than 20W the script is still triggered every 10sec.
is it possible that the script just triggers once and then finished until tv is back on/off again?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Trigger script at specific value

Post by waaren »

Enz0jacco wrote: Thursday 26 November 2020 9:51 thnx for the quick reply Waaren!

it is working but now when the tv is off and thus the usage is less than 20W the script is still triggered every 10sec.
is it possible that the script just triggers once and then finished until tv is back on/off again?
Yes, Change line 4 to

Code: Select all

return domoticz.devices(238).active and domoticz.devices(237).actualWatt < 20 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Enz0jacco
Posts: 45
Joined: Tuesday 03 March 2020 8:15
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Trigger script at specific value

Post by Enz0jacco »

working perfect Waaren thank u so much!
never used the active line before in dzvent but defenitively gonna do it some more!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Trigger script at specific value

Post by waaren »

Enz0jacco wrote: Thursday 26 November 2020 14:42 ... but definitively gonna do it some more!
It's there to help you but please be aware that the function code used in the active segment is executed every minute and at every device change in the system so best to limit yourself and don't put a lot of logic in that function.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest