Sync script not working

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
MasMat
Posts: 20
Joined: Saturday 09 March 2019 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Helsinki
Contact:

Sync script not working

Post by MasMat »

Hi guys,
I'm asking for help, as this is my first DzVents script. Any tips are welcome, because I'm not a great coder either.

The idea is simple: sync a pair of outdoor lights when Home/Away is set at home. I'm not getting errors, but it's not working. The script is set ON.
Sorry about the names being in finnish gibberish ("Pihavalot" is main yard lights, "PuuCee Polkuvalo" is the one that's separate, KotonaPoissa = HomeAway). The spelling matches Domoticz names. Domoticz is v4.10717

Code: Select all

return {
   on = {
      devices = {
         'Pihavalot'
      }
   },
   execute = function(domoticz, piha)
    local polkuvalo = domoticz.devices('PuuCee Polkuvalo')
	if (	
	    (device.name == 'KotonaPoissa' and device.state == 'On') and
	    (device.name == 'Pihavalot' and device.state == 'On')) then
        polkuvalo.switchOn().checkFirst()
    end
    
    if (
	    (device.name == 'KotonaPoissa' and device.state == 'On') and
	    (device.name == 'Pihavalot' and device.state == 'Off')) then
        polkuvalo.switchOff().checkFirst()
    end
   end

}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Sync script not working

Post by waaren »

MasMat wrote: Monday 04 November 2019 8:48 The idea is simple: sync a pair of outdoor lights when Home/Away is set at home. I'm not getting errors, but it's not working. The script is set ON.
Sorry about the names being in finnish gibberish ("Pihavalot" is main yard lights, "PuuCee Polkuvalo" is the one that's separate, KotonaPoissa = HomeAway). The spelling matches Domoticz names. Domoticz is v4.10717
If I understand correctly, this should do it

Code: Select all

return 
{
    on = 
    {
        devices = 
        { 
            'Pihavalot' 
        } 
    },

    logging = 
    { 
        level = domoticz.LOG_DEBUG,  -- set to Error when script works as expected
        marker = "atHome" 
    },

   execute = function(dz, item)                              -- dz is the domoticz object with all devices, uservars, etc. item is the object that triggered the script
        local atHome = dz.devices('KotonaPoissa').state == 'On'  -- set atHome to true or false depending on the state of device KotonaPoissa
        local polkuvalo = dz.devices('PuuCee Polkuvalo') -- slave device

        if atHome and item.state == 'On' then
            polkuvalo.switchOn().checkFirst()
        elseif atHome and item.state == 'Off' then
            polkuvalo.switchOff().checkFirst()
        else
            dz.log('No action needed because atHome is false',dz.LOG_DEBUG)
        end
    end

}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest