This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Change Active Status of a Script
#1
Hi

Is it possible to use a script to change the active state of another script?  

I tried the following code, however it doesn't seem to change the active state / icon in the script list view. (I'm running a Schneider SHAC with the older logic machine firmware which might be the problem)

Code:
-- Change active status of script
function enableScript(scriptname, active)
 
  if active == true then
   script.enable('scriptname')
   log('Script "' .. scriptname .. '" is active')
  else
   script.disable('scriptname')
    log('Script "' .. scriptname .. '" is disabled')
  end
 
end


I successfully run the following the following code which directly interacts with the database to change the status of schedules - is something similar required to activate / deactivate scripts?

Code:
-- Change active status of schedule
function enableSchedule(schedulername, active)
 
  query = 'SELECT * FROM schedulers'
  for _, scheduler in ipairs(db:getall(query)) do
    if scheduler.name == schedulername then
      if active == true then
        db:update('schedulers', { active = 1 }, { id = scheduler.id })
        log('Schedule "' .. schedulername .. '" is active')
      else
        db:update('schedulers', { active = 0 }, { id = scheduler.id })
        log('Schedule "' .. schedulername .. '" is disabled')
      end
    end
  end
   
end 

-- Clear old schedules after updating enable state in database with enableSchedule
function updateSchedulers()
  local files = io.ls('/tmp')
  for _, file in ipairs(files) do
    if file:find('lm-scheduler-', 1, true) then
      os.remove('/tmp/' .. file)
    end
  end
end

Many thanks in advance

Kind Regards
James
Reply
#2
Does the correct status show up after the page reload? Periodical script status reload (every 6 seconds) has been implemented for some time already.
Reply
#3
Unfortunately no, the refresh doesn't seem to make a difference (even after 30 secs).

I'm referring to the scripts by name (ie: 'Tesla Powerwall - Auto Set Reserved Percentage') - is this ok or should I be using an ID or something different?

is there an alternative way to change the status with the old firmware by directly updating the database.

Or is there a way I can test if the script.disable() is supported on my firmware?

Kind Regards
James
Reply
#4
Changing the DB value won't help because the script itself will not be stopped or started.
See what the enable/disable call returns. If script is found then enable returns true, disable returns false. Otherwise you will get nil + error message.
Code:
res, err = script.enable('SCRIPT NAME HERE')
log(res, err)
Reply
#5
Hi

Here is the output of the your test code - it's working

Code:
TEST 13.05.2021 18:43:24
* arg: 1
  * bool: true
* arg: 2
  * nil

I found the error in my earlier code too .. working function below

Code:
-- Change active status of script
function enableScript(scriptname, active)
 
  if active == true then
   script.enable(scriptname)
   log('Script "' .. scriptname .. '" is active')
  else
   script.disable(scriptname)
    log('Script "' .. scriptname .. '" is disabled')
  end
 
end

Many thanks

Kind Regards
James
Reply
#6
(13.05.2021, 08:46)jamesng Wrote: Hi

Here is the output of the your test code - it's working

Code:
TEST 13.05.2021 18:43:24
* arg: 1
  * bool: true
* arg: 2
  * nil

I found the error in my earlier code too .. working function below

Code:
-- Change active status of script
function enableScript(scriptname, active)
 
  if active == true then
   script.enable(scriptname)
   log('Script "' .. scriptname .. '" is active')
  else
   script.disable(scriptname)
    log('Script "' .. scriptname .. '" is disabled')
  end
 
end

Many thanks

Kind Regards
James

Im just a beginner, but i hope you will help me.

I will activate and disable a 'Resident script' depending on a feedback object of a output.
the group object is 1/4/53 (bedroom [fb])
when this object is true i will run a script to activate the 'Resident script' called 'bedroom [fb], turn off'
and the same but turn it off by the same group adress
when this object is false i will run a script to disable the 'Resident script' called 'bedroom [fb], turn off'

I saw you will do the same, but i dont know how to use this script.
Reply
#7
Simply create an event script attached to a 1-bit boolean object and put this into the editor.
Code:
enable = event.getvalue()
script.set('bedroom [fb], turn off', enable)
Reply
#8
(12.10.2021, 12:22)admin Wrote: Simply create an event script attached to a 1-bit boolean object and put this into the editor.
Code:
enable = event.getvalue()
script.set('bedroom [fb], turn off', enable)

WoW thanks, so fast i wish i was good as you are  Heart



just another question, just curious, what is the different between the 2 scripts.
or what does do the other script of Topic Starter?
Reply
#9
I tried to enable and disable a resident PID script with a event script on a virtual object but i can't get it to work.

Code:
enable = event.getvalue('32/1/59')
script.set('PID, turn off', enable)
Reply
#10
Your resident script probably is not called "PID, turn off". Also the group address is not needed for event.getvalue().
Reply
#11
(21.01.2022, 22:46)KoBra Wrote: I tried to enable and disable a resident PID script with a event script on a virtual object but i can't get it to work.

Code:
enable = event.getvalue('32/1/59')
script.set('PID, turn off', enable)

Thanks, that was the only combination i didn't test.
Reply


Forum Jump: