Page 1 of 2

How to set a device on for 5 minutes and then off for 5 minutes

Posted: Wednesday 07 December 2022 14:07
by hemant5400z
Hi,

I'm trying to make a blocky work based on a virtual switch.

If the virtual switch is on then
device x needs to on for 5 min
device x not to go off for 5 min

Until virtual switch is turned off.

I cannot make it work in blockly either device quickly goes on/off or stays off.
I tried to use on for 300 seconds
and also tried for 5 minutes

with both block If ..Do and using If then Else, is it even possible with Blockly?

Thanks, looking for some expertise.


Cheers,
Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Wednesday 07 December 2022 14:37
by willemd
In blockly the else part is not only executed when the if condition is false (like in most programming languages) but is always executed sequentially after the if part has finished, so if you don't want that you have to add another if-condition to the else part.

viewtopic.php?p=225141#p225141

Maybe that causes your problem?

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Wednesday 07 December 2022 18:20
by HvdW
Dive into dzvents.
Time spent studying dzvents is time well spent.

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Wednesday 07 December 2022 20:21
by hemant5400z
Hi,

Yes working on dzevents to get this done my only issues is i want the trigger the event when mu virtual device is on then it should trigger the other device every 5 minutes.

I used device and timer but it fires de on off every 5 minutes or if virtual device changes state while i only want to do this when the virtual device state is on.

I think i need to get state of virtual device as a local read out and than if local is on do the timer every 5 min switch the target device. But not familiar that much with dzevents so looking for an example with a trigger on time only when a specific switch is set on.

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Wednesday 07 December 2022 23:22
by waltervl
Use in dzvents something like

Code: Select all

if item.isTimer and domoticz.devices('your_Switch').active then
Edit:
If you have only timer trigger (every 5 minutes) in your script you can also only use the the .active check. Following will switch on/off (=toggle) the device your_light every 5 minutes.

Code: Select all

if domoticz.devices('your_Switch').active then
   domoticz.devices('your_light').toggle
end

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 10:58
by hemant5400z
Hi Thanks for the response.

I tried as a tes:

return
{
on =
{
timer =
{
'every 1 minutes',
}
},

execute = function(dz)

if item.isTimer and domoticz.devices('Virtual').active then
domoticz.devices('Sonoff').toggle
end
end
}


But getting the following error:

/config/scripts/dzVents/generated_scripts/CV_regeling.lua:15: syntax error near 'end' which seems to be this part:
if item.isTimer and domoticz.devices('Virtual').active then
domoticz.devices('Sonoff').toggle
end

Getting the eact same error with:

if domoticz.devices('your_Switch').active then
domoticz.devices('your_light').toggle
end

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 11:03
by willemd
item is not defined
try:
execute = function(dz,item)

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 11:09
by hemant5400z
return
{
on =
{
timer =
{
'every 1 minutes',
}
},

execute = function(dz,item, domoticz)
if item.isTimer and domoticz.devices('Virtual').active then
domoticz.devices('Sonoff').toggle
end
end
}


Still the same error

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 12:38
by willemd
You need to use either dz or domoticz, not a mix.

Try this:

Code: Select all

return
{
on =
{
timer =
{
'every 1 minutes',
}
},

execute = function(dz,item)
if item.isTimer and dz.devices('Virtual').active then
dz.devices('Sonoff').toggle
end
end
}

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 12:51
by hemant5400z
Hi Willem,

I get:

lua:14: syntax error near 'end'

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:06
by hemant5400z
Thanks,

I copied an pasted from diferents scripts, now have a working version, slightly different but does the trick.

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:16
by willemd
hemant5400z wrote: Thursday 08 December 2022 12:51 Hi Willem,

I get:

lua:14: syntax error near 'end'

Hemant
The first thing to do when you get an error is look at the line indicated, in this case 14.
There is only "end" so probably it is the line before.
There it states dz.devices('Sonoff').toggle
And then you check documentation of dzvents and you find there is no "toggle", only "toggleSwitch()"

So what happens it you try that change?

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:24
by hemant5400z
Thanks Willemd,

That was indeed the issue, the toggleSwitch() did the trick and now it is working as it should.

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:27
by waltervl
Ah, sorry for that toggle thing.... But glad it works.

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:28
by hemant5400z
One more question.

Let's say, i switch of the virtual device, while the triggered device was On, will it go off? Or stay on forever?

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:45
by waltervl
With this script it will stay in the status it was in.
You can add else statement to the if then to switch off Sonoff when Virtual is Off.

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 13:47
by hemant5400z
Yes,

but not sure of what the counter part is of this line if item.isTimer and dz.devices('Virtual').active as it is a boolean.

dz.devices('Virtual').active.true vs dz.devices('Virtual').active.false?

Hemant

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 14:23
by waltervl
Please post your working script else it will be difficult to advise.

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 14:36
by Dave21w
I have this for DZ but don't have an external control switch as I don't need one
dz toggle.jpg
dz toggle.jpg (13.84 KiB) Viewed 2785 times
Or you could use blockly and look at this post

viewtopic.php?p=290984

Re: How to set a device on for 5 minutes and then off for 5 minutes

Posted: Thursday 08 December 2022 14:54
by hemant5400z
waltervl wrote: Thursday 08 December 2022 14:23 Please post your working script else it will be difficult to advise.
return
{
on ={ timer = {'every 1 minutes',}
},

execute = function(dz,item)
if item.isTimer and dz.devices('Virtual').active then
dz.devices('Sonoff').toggleSwitch()
end
end
}



Above is working and toggles on/off, what I want is a failsafe so if Virtual is Off, while the Sonoff is still on (triggered), it should go Off, currently it stays on infinite.

So indeed need an Else statement with: if item.isTimer and dz.devices('Virtual').active-not-active then

Hemant