Converting blocky to LUA

Moderator: leecollings

Post Reply
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Converting blocky to LUA

Post by Melotron »

Greetings.
I'am trying to convert a few of my blockys to LUA, but its not working out.

Here are the blocky original:
Blocky Vanligt.jpg
Blocky Vanligt.jpg (58.19 KiB) Viewed 2254 times
Here my LUA code :

Code: Select all

commandArray = {}
--
-- Turn on the Day scene
--
if (devicechanged['Virt_Day'] == 'On' and 
    otherdevices['Hemma'] == 'On' and 
    otherdevices['Virt_Dark'] == 'Off' and 
    otherdevices['Virt_Sleep'] == 'Off' and 
    otherdevices['Virt_TV'] == 'Off' ) then
    commandArray['Scene:Day'] = 'On'
    print('Day')
end
 --
 -- Turn on the Dusk light scene
 --
elseif (devicechanged['Virt_Dusk_light'] == 'On' and 
        otherdevices['Hemma'] == 'On'  and 
        otherdevices ['Virt_Dark'] == 'Off' and 
        otherdevices ['Virt_Sleep'] == 'Off'  and 
        otherdevices ['Virt_TV'] == 'Off' and 
        otherdevices ['Virt_Dusk'] == 'Off') then
        commandArray['Scene:Dusk light'] = 'On'
        print('Dusk light')
end
  --
  -- Turn on the Dusk scene
  --
elseif (devicechanged['Virt_Dusk'] == 'On' and 
        otherdevices ['Hemma'] == 'On' and 
        otherdevices ['Virt_Dark'] == 'Off' and 
        otherdevices ['Virt_Sleep'] == 'Off' and 
        otherdevices ['Virt_TV'] == 'Off' and 
        otherdevices ['Virt_Dusk_light'] == 'Off') then
        commandArray['Scene:Dusk'] = 'On'
        print('Dusk')
end
 --
 -- Turn on the Dark scene
 --
elseif (devicechanged['Virt_Dark'] == 'On' and 
        otherdevices ['Hemma'] == 'On'  and 
        otherdevices ['Virt_Dusk'] == 'Off' and  
        otherdevices ['Virt_Sleep'] == 'Off'  and 
        otherdevices ['Virt_TV'] == 'Off' and 
        otherdevices ['Virt_Dusk_light'] == 'Off') then
        commandArray['Scene:Dark'] = 'On'
        print('Dark')
end
Should I rename all "otherdevices" to "devicechanged" as some switches might change or should I make a new elseif for each one that can change ?
If anyone have a good page for the first few steps on how LUA are working please send it to me =)
I've tried to read the lua guide on the wiki, but its going to fast over my head on how to start LOL

Regards Magnus Svensson
User avatar
heggink
Posts: 980
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Converting blocky to LUA

Post by heggink »

I have just converted 42+ blockies to Lua, first as individual files (and saw my performance go down the toilet with domoticz running 95% on rpi3 core) and then integrating a bunch of them into eventually 6 larger lua scrips (one which is my mother of all lua scrips having a crazy number of elseifs).

In the process, I noticed that it's not as simple as just putting another elseif especially if you want to retest a changed device in a different condition. if's earlier in the chain could prevent the elseif from firing (hence the 6 files eventually).

my first command (after the commandArray = {}) is a simple dev=next(devicechanged) to get the device that caused the script trigger

then I have the crazy number of:
if (dev == 'SomeDevice' and otherdevice[dev] == 'On' or otherdevices['SomeOtherDevice'] == 'SomeOtherCondition' )
then
do silly stuff..
(no end here)
elseif (dev == 'Anotherdevice' and ...

As long as I keep checking on different conditions then the next elseif will trigger.

In your case, remove the end's before the elseifs and add the dev=next(...) also in order to type less.

Let me know if that works. Good luck!

H
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: Converting blocky to LUA

Post by Melotron »

Thanks alot!

I'am waiting on a few items for my lux meter after that so will I try to make one environment script and then one for each room.
I have a few smaler lua scripts with only one device and that works, but I haven't been able to move past two devices.
This will with out any doubt move me along way on to getting away from blokies and over to more lua =)

How are the speed on having the luas external compared to in the database ?

Regards Magnus Svensson
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Converting blocky to LUA

Post by mjdb »

Using 'DeviceChanged' is relevant for an event-triggered script.
I assume you only want to change the scene when the time is right and not at every action of some device.

So, first change the script to a time-driven script. That will run every minute.

I suggest to split the conditions and the actions.

-- Henna should be always On. So, it is is off, do nothing
-- Also, if TV is On or Sleep is On, do nothing
-- Day, Dusk-Light, Dusk and Dark will be mutual exclusive

TargetScene = '' -- Will contain the Scene to set
if otherdevices['Henna'] == 'Off' then
-- do nothing
elseif otherdevices['Virt_TV'] == 'On' then
-- do nothing
elseif otherdevices['Virt_Sleep'] == 'On' then
-- do nothing
elseif otherdevices['Virt_Day'] == 'On' then
TargetScene = 'Day'
elseif otherdevices['Virt_Dusk_light'] == 'On' then
TargetScene = 'Dusk light'
elseif otherdevices['Virt_Dusk'] == 'On' then
TargetScene = 'Dusk'
elseif otherdevices['Virt_Dark'] == 'On' then
TargetScene = 'Dark'
elseif
print('Strange to be here. Some Virt-setting must be wrong.')
end

-- Now you have set the desired Scene in variable TargetScene

if (TargetScene ~= '') then
print('Target Scene: ' .. TargetScene)
commandArray['Scene:' .. TargetScene] = 'On'
end

This will set the Scene every minute, so you might want to add a test if the Scene is already selected. If so, do not run the command.
However, this is still better then setting the scene at every change of some device as it is with a device-driven script.

Note: I just typed the script as a suggestion, I didn't test or debug it.

Marco de Bruijn
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: Converting blocky to LUA

Post by Melotron »

mjdb wrote: TargetScene = '' -- Will contain the Scene to set
if otherdevices['Henna'] == 'Off' then
-- do nothing
elseif otherdevices['Virt_TV'] == 'On' then
-- do nothing
elseif otherdevices['Virt_Sleep'] == 'On' then
-- do nothing
elseif otherdevices['Virt_Day'] == 'On' then
TargetScene = 'Day'
elseif otherdevices['Virt_Dusk_light'] == 'On' then
TargetScene = 'Dusk light'
elseif otherdevices['Virt_Dusk'] == 'On' then
TargetScene = 'Dusk'
elseif otherdevices['Virt_Dark'] == 'On' then
TargetScene = 'Dark'
elseif
print('Strange to be here. Some Virt-setting must be wrong.')
end

-- Now you have set the desired Scene in variable TargetScene

if (TargetScene ~= '') then
print('Target Scene: ' .. TargetScene)
commandArray['Scene:' .. TargetScene] = 'On'
end
Thanks for this script, that's a really nice way of setting up an big general enviromen.
But sadly so are my knowledge on lua programming to smal atm. I'am getting the following error when its in Domoticz internal script list. :

EventSystem: in LUA Hemma Vanlight: [string "if otherdevices['Hemma'] == 'Off' then ..."]:19: 'then' expected near 'end'

I'am going to try a few more things on this script and see if I can get it to work. But this fits really good with getting an big general settings script and a few smaler scripts for what happens when there are movements, kodi and other similar things.

Regards Magnus Svensson
mjdb
Posts: 71
Joined: Thursday 12 January 2017 13:38
Target OS: Raspberry Pi / ODroid
Domoticz version: all beta
Location: NL - Pijnacker
Contact:

Re: Converting blocky to LUA

Post by mjdb »

sorry...

the last three lines should be

else
print('Strange to be here. Some Virt-setting must be wrong.')
end
- - - - - - - - - - -
2 x Domoticz on Raspberry Pi; 2 x RFXtrx433; Aeotec Z-Stick
KlikAanKlikUit ICS-2000 as Relay
Aeotec MultiSensors 6; Danfoss Z Thermostats 014G0013; Kaku Switches, Z-wave Switches
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest