Page 1 of 1
checkFirst()
Posted: Sunday 06 January 2019 11:18
by poudenes
Hi All,
I use checkFirst() lots of time. because it makes my scripts run faster. When it need to turn on or off lights for example.
But when you have other actions you cannot put the checkFirst() behind it.
Would it be nice that you can add checkFirst() always, no matter what?
Maybe that is possible to build?
Re: checkFirst()
Posted: Sunday 06 January 2019 12:33
by jake
For other actions you have to know what the intended reaction is, before you can perform a 'checkfirst()'
Re: checkFirst()
Posted: Sunday 06 January 2019 13:57
by poudenes
When you do a action with .Kelvin() then it will turn on the light and then set Kelvin. When you add check first, it will skip the on action and directly set Kelvin. This is the same for RGB. Or create a new action CheckOnOff to check if a device is already on or off before doing the action.
I did some tests and when you want turn off 13 bulbs with the check first its much faster, because the script doesn't send for every bulb a off signal of its already off
Re: checkFirst()
Posted: Sunday 06 January 2019 15:08
by waaren
poudenes wrote: Sunday 06 January 2019 11:18
I use checkFirst() lots of time. because it makes my scripts run faster. When it need to turn on or off lights for example.
The speed advantage you see here is comparing the switchOff() function to switching regardless (without checking first if the device is already in the required state). To get a fair comparison, you should compare it with a user coded check.
I checked both methods with a loop over +/- 100 devices with state Off and both methods take approximately the same amount of time (on average 275 milliseconds)
First test was with code using
Code: Select all
if device ~= "Off" then
device.switchOff()
end
- Spoiler: show
- 2019-01-06 14:12:01.073 Status: dzVents: Info: ------ Start external script:
2019-01-06 14:12:01.331 Status: dzVents: !Info: OS Time passed during check with if state ~= Off : 265 msecs for 100 devices.
2019-01-06 14:12:01.332 Status: dzVents: Info: ------ Finished
2nd test was using
- Spoiler: show
- 2019-01-06 14:15:00.336 Status: dzVents: Info: ------ Start external script:
2019-01-06 14:15:00.618 Status: dzVents: !Info: OS Time passed during device.switchOff().checkFirst() : 284 msecs for 101 devices.
2019-01-06 14:15:00.619 Status: dzVents: Info: ------ Finished
But when you have other actions you cannot put the checkFirst() behind it. Would it be nice that you can add checkFirst() always, no matter what?
Maybe that is possible to build?
For some (sub)devices it would be possible but not very easy to do as the current implementation of this function is quite strongly linked to the switch devicetypes. Assuming there will also be no response improvement compared to the if construct like I showed in examples above a remaining reason would be to prevent the if construct in user code.
I see the value for switches because this requirement is there in many examples, but I have doubts about the added value for other devices.
As always; YMMD
Re: checkFirst()
Posted: Sunday 06 January 2019 15:12
by poudenes
Thanks for the reply and explanation

Re: checkFirst()
Posted: Thursday 09 September 2021 20:57
by rrozema
For me the main reason to use checkFirst() is a totally different one, still speed related though: if you do an update of a value, that may trigger all sorts of scripts to activate, even if you're setting the same value that was already there. So the main reason why I put checkFirst in my scripts is to avoid activating any triggers when the value doesn't change.