We have moved at community.getvera.com

Author Topic: setting devices to desired state at startup?  (Read 2810 times)

Offline jswim788

  • Hero Member
  • *****
  • Posts: 809
  • Karma: +58/-2
setting devices to desired state at startup?
« on: January 30, 2017, 01:00:04 pm »
I had a couple of extended power outages and I realize that the Z wave switches dutifully retain their last state, but that is not always what I need.  With non Z wave switches it is easy to go around and turn off lights and other items that I don't want to come back on when the power is restored.  Not so with Z wave.  So I would like to be able to automatically set a few switches and other devices to desired states on power up.

Can this be done in the lua startup code?  Is everything initialized by the time that code runs so I can simply set the state of the select devices?

Or should this be done in some other place?

Offline RichardTSchaefer

  • Community Beta
  • Master Member
  • ******
  • Posts: 10091
  • Karma: +764/-143
Re: setting devices to desired state at startup?
« Reply #1 on: January 30, 2017, 01:32:28 pm »
I would have the Startup LUA run a scene ...

Then put all of your Z-Wave actions in the Scene.
You can also run the scene manually at other times if you need to.

Offline jswim788

  • Hero Member
  • *****
  • Posts: 809
  • Karma: +58/-2
Re: setting devices to desired state at startup?
« Reply #2 on: January 30, 2017, 02:11:20 pm »
I'm guessing this might run any time Vera is restarted or reloads as well?  I only want to set the state of devices when starting from a cold boot.  That might be harder to detect?

thanks

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: setting devices to desired state at startup?
« Reply #3 on: January 30, 2017, 02:39:14 pm »
You can pick the system boot time from the machine's metafiles and check whether this reload is the first since then.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline cybrmage

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1104
  • Karma: +113/-44
Re: setting devices to desired state at startup?
« Reply #4 on: January 30, 2017, 02:56:42 pm »
I only want to set the state of devices when starting from a cold boot.  That might be harder to detect?

You can have your lua code read the system uptime into a lua variable... If the number is sufficiently small, then the system has rebooted recently...

ie:

Code: [Select]
function shellExecute(cmd, Output)
if (Output == nil) then Output = true end
local file = assert(io.popen(cmd, 'r'))
if (Output == true) then
local cOutput = file:read('*all')
file:close()
return cOutput
else
file:close()
return
end
end

local uptime = tonumber(shellExecute("cat /proc/uptime|cut -d' ' -f1",true),10)

-- check if runtime is sufficiently small - most systems take between 1 and 3 minutes to boot - the 200 should be tweaked to your specific device
if (uptime and (uptime < 200)) then
  -- the system has recently rebooted - allow the scene to run
  return true
end
-- if we get here, the system did not reboot recently - abort
return false

Offline kwieto

  • Hero Member
  • *****
  • Posts: 624
  • Karma: +27/-16
Re: setting devices to desired state at startup?
« Reply #5 on: January 30, 2017, 03:07:57 pm »
Another approach is to set device to be off after power outage by default (I don't know if it is possible for all, but for some devices it is configurable parameter)
In this case, after Vera reboot you have to to run your scenes again to switch devices back on if needed.

I have such setting for wifi switches which don't "remember" their status by default. For those which I need to be switched on after power failure I have scenes running frequently enough to keep things going, or I added certain scenes to be run during startup

Offline jswim788

  • Hero Member
  • *****
  • Posts: 809
  • Karma: +58/-2
Re: setting devices to desired state at startup?
« Reply #6 on: January 30, 2017, 04:14:27 pm »
@cybrmage: Wow!  Really great.  Thank you.  Have you thought of this before?

@kwieto - I haven't found such a default setting in the switches I typically use, mostly GE/Jasco switches.  I'll have to take a look.  Thanks.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: setting devices to desired state at startup?
« Reply #7 on: January 30, 2017, 04:56:16 pm »
It's even easier than that, since Lua can read those files directly... no need to execute a shell command:

Code: [Select]
local uptime
for x in io.lines "/proc/uptime" do uptime = uptime or x: match "%d+" end
...gives uptime in seconds.

This is what I meant when I said:

You can pick the system boot time from the machine's metafiles and check whether this reload is the first since then.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline kwieto

  • Hero Member
  • *****
  • Posts: 624
  • Karma: +27/-16
Re: setting devices to desired state at startup?
« Reply #8 on: January 31, 2017, 03:12:22 am »
@kwieto - I haven't found such a default setting in the switches I typically use, mostly GE/Jasco switches.  I'll have to take a look.  Thanks.

See in the device's manual or ask support if it is configurable for your switches.
In Vera you have to add parameter in the "Device options" section of the specified device, you won't see it if you don't add it.
I have neocoolcam wallplug which has such parameter available to be set and some Fibaro switches all with this functionality,
But I don't see it for example in Aeon G2 switch manual, don't know if it is not possible or they just don't share this information with users.

Offline jswim788

  • Hero Member
  • *****
  • Posts: 809
  • Karma: +58/-2
Re: setting devices to desired state at startup?
« Reply #9 on: January 31, 2017, 12:15:20 pm »
This:
Code: [Select]
x: match "%d+" is equivalent to
Code: [Select]
string.match(x, "%d+") and equivalent to
Code: [Select]
x:match("%d+")?

I found the second two forms in the lua web sites, but is there a name for the first format so I can learn about it?  I'm not able to find that syntax described.  Thanks.

Offline akbooer

  • Beta Testers
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: setting devices to desired state at startup?
« Reply #10 on: January 31, 2017, 02:53:47 pm »
You need to take a deeper look at Lua syntax in general.

The 'colon' syntax is a standard way to invoke functions embedded in tables.

Your prime reference should be https://www.lua.org/pil/ which has a free downloadable version.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.