Page 1 of 1

Sync script not working

Posted: Monday 04 November 2019 8:48
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

}

Re: Sync script not working

Posted: Monday 04 November 2019 9:18
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

}