Page 1 of 1

how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 11:17
by abdolhamednik
hello everyone ;)

how can i write a script in order to run once only? like setup() in most mcu's :?: :idea:

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 11:36
by jvdz
I am a bit puzzled about the requirement for a scheduled script which needs to run only one time ?
Can you define the condition/occasion/circumstances this script would need to run?

Jos

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 11:57
by abdolhamednik
jvdz wrote: Wednesday 21 October 2020 11:36 I am a bit puzzled about the requirement for a scheduled script which needs to run only one time ?
Can you define the condition/occasion/circumstances this script would need to run?

Jos
exactly like mcu's where there is a setup() which runs to do some settings only once

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 12:25
by jvdz
I had seen that bit, but that doesn't explain the purpose in domoticz yet for me.
So you want to run a script at the start of the domoticz service and run it one time?

Jos

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 13:35
by abdolhamednik
jvdz wrote: Wednesday 21 October 2020 12:25 I had seen that bit, but that doesn't explain the purpose in domoticz yet for me.
So you want to run a script at the start of the domoticz service and run it one time?

Jos
yeah

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 13:39
by waaren
abdolhamednik wrote: Wednesday 21 October 2020 13:35 yeah
You might want to look at dzVents system event Start

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 13:42
by jvdz
The easiest way I would do it is to shell a script from the domoticz.sh script started by the service to do the required tasks. probably add a sleep in there for a couple of seconds to give Domoticz time to startup and then perform the tasks required.
Still not sure what it is you need to do a startup so am just thinking out loud here.

Jos

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 14:34
by abdolhamednik
jvdz wrote: Wednesday 21 October 2020 13:42 The easiest way I would do it is to shell a script from the domoticz.sh script started by the service to do the required tasks. probably add a sleep in there for a couple of seconds to give Domoticz time to startup and then perform the tasks required.
Still not sure what it is you need to do a startup so am just thinking out loud here.

Jos
maybe it is required to shut a window at first to let the system know what the window state is

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Wednesday 21 October 2020 14:47
by jvdz
abdolhamednik wrote: Wednesday 21 October 2020 14:34
jvdz wrote: Wednesday 21 October 2020 13:42 The easiest way I would do it is to shell a script from the domoticz.sh script started by the service to do the required tasks. probably add a sleep in there for a couple of seconds to give Domoticz time to startup and then perform the tasks required.
Still not sure what it is you need to do a startup so am just thinking out loud here.

Jos
maybe it is required to shut a window at first to let the system know what the window state is
Not sure how your reply is an answer to my post. :?
It shouldn't be that hard to define what it is you want to accomplish at startup of Domoticz.

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 7:07
by abdolhamednik
I would be thankful to know how :) "to shut a window whenever the system starts, i.e. a power loss and return"
you know, i don't have a sensor on the window to know its status. so i guess shutting first is the only way to make sure about it

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 7:16
by abdolhamednik
waaren wrote: Wednesday 21 October 2020 13:39
abdolhamednik wrote: Wednesday 21 October 2020 13:35 yeah
You might want to look at dzVents system event Start
thanks waaren. i'm reading that... :geek:

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 7:52
by abdolhamednik
waaren wrote: Wednesday 21 October 2020 13:39
abdolhamednik wrote: Wednesday 21 October 2020 13:35 yeah
You might want to look at dzVents system event Start
i followed it and wrote this short script, test, but it was not successful...

Code: Select all

return {
        on = {
          devices = {
             '<exact name of the switch>'
          }
        },
        execute = function(domoticz, switch)
          if (switch.state == 'On') then
             domoticz.log('Hey! I am on!')
          else
             domoticz.log('Hey! I am off!')
          end
        end
    }
of course i made these settings:
Using dzVents with Domoticz

In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the check-box ‘dzVents disabled’ is not checked. Also make sure that in the Security section in the settings (Setup > Settings > System > Local Networks (no username/password) you allow 127.0.0.1 (and / or ::1 when using IPv6 ) to not need a password. dzVents does use this port to get the location settings and to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state.

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 7:56
by waaren
abdolhamednik wrote: Thursday 22 October 2020 7:52 i followed it and wrote this short script, test, but it was not successful...
Can you define not successful.. ?

Did you replace <exact name of the switch > with the name of your switch ?
What did you expect and hat happened. What is in the log?

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 8:12
by abdolhamednik
waaren wrote: Thursday 22 October 2020 7:56
abdolhamednik wrote: Thursday 22 October 2020 7:52 i followed it and wrote this short script, test, but it was not successful...
Can you define not successful.. ?

Did you replace <exact name of the switch > with the name of your switch ?
What did you expect and hat happened. What is in the log?
Yes, Of course. The switch is: R1(1.52)<8-3>[3]*

when i switched on and off that in the switches tab of domoticz, i hoped to see: "Hi! i am on or off" in the log. but saw only " ... admin switched on R1(1.52)<8-3>[3]* ... " or sth like that

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 8:40
by waaren
abdolhamednik wrote: Thursday 22 October 2020 8:12 when i switched on and off that in the switches tab of domoticz, i hoped to see: "Hi! i am on or off" in the log. but saw only " ... admin switched on R1(1.52)<8-3>[3]* ... " or sth like that
When I use that name in the script and save it as dzVents, I see

Code: Select all

2020-10-22 08:34:38.683 Status: User: Admin initiated a switch command (672/R1(1.52)<8-3>[3]*/On)
2020-10-22 08:34:38.911 Status: dzVents: Info: Handling events for: "R1(1.52)<8-3>[3]*", value: "On"
2020-10-22 08:34:38.911 Status: dzVents: Info: ------ Start internal script: Script #6: Device: "R1(1.52)<8-3>[3]* (Virtual)", Index: 672
2020-10-22 08:34:38.913 Status: dzVents: Info: Hey! I am on!
2020-10-22 08:34:38.914 Status: dzVents: Info: ------ Finished Script #6
Can you have a look in [Setup][Settings] [Other] and check that both dzVents and EventSystem are set as enabled and loglevel to "Errors + minimal execution info" ?

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 9:39
by abdolhamednik
you are right! i think there is a problem with my device which the manufacturer has installed domoticz on, he has renamed it as Avisa... :!:
Here is what my log is:

2020-10-22 11:04:44.345 (hsb) Light/Switch (R1(1.52)<8-3>[3]*)
2020-10-22 11:04:44.304 Status: User: Admin initiated a switch command (3/R1(1.52)<8-3>[3]*/Off)
.
.
.
2020-10-22 11:04:44.594 Error: EventSystem: in /home/pi/avisa/dzVents/runtime/dzVents.lua: /home/pi/avisa/dzVents/runtime/Avisa.lua:39: attempt to perform arithmetic on field 'sunsetInMinutes' (a nil value)
.
.
.

Re: how to tell a script to run once? like setup() in most mcu's

Posted: Thursday 22 October 2020 9:46
by waaren
abdolhamednik wrote: Thursday 22 October 2020 9:39 2020-10-22 11:04:44.594 Error: EventSystem: in /home/pi/avisa/dzVents/runtime/dzVents.lua: /home/pi/avisa/dzVents/runtime/Avisa.lua:39: attempt to perform arithmetic on field 'sunsetInMinutes' (a nil value)
Did you set your location details in [Setup][Settings][Systems] ? If not domoticz / dzVents cannot determine the local time and everything derived from it.
Also kind of hard to help you if someone renamed system files in the dzVents runtime folder (and maybe on other places ?)