Page 1 of 1

Checking a sensor every few seconds

Posted: Monday 14 November 2022 22:21
by paul402
I want to check my fridge door using rxfcom and a door switch. The door switches normally work fine but if the fridge door is closed quickly then the closed signal is missed.
So I looked at this thread for inspiration. viewtopic.php?t=36400
Thanks for that.

My mods look like this.

Code: Select all

local scriptVar = 'every10Seconds'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_ERROR,
        marker = scriptVar,
    },

    execute = function( dz, item )

        if dz.devices('Fridge Door_D3').state == 'Off' then return end -- If fridge door check device state is Off the script will not do anything,

        if item.isTimer then
            for delay = 10, 50, 10 do
                dz.emitEvent(scriptVar).afterSec(delay)
            end
        end

        local fridgedoor = dz.devices('Fridge Door_D3')
        

        local function setvar(var, to)
            if var.value ~= to and dz.startTime.secondsAgo > 60 then var.set(to) end
        end

        if fridgedoor.lastUpdate.secondsAgo > 60 then
            domoticz.log('Fridge Door switch >x seconds')
            msg="Fridge Door Open " .. fridgedoor.lastUpdate.minutesAgo .. " mins/seconds"  
                        domoticz.openURL('https://api.telegram.org/botxxxxxxxxxxxxxxxxxxxxxxxx/sendMessage?chat_id=xxxxxxx&text=' .. msg)   
        else
            domoticz.log('Fridge Door closed')
        end
    end
}

and I'm getting these error messages.
2022-11-14 22:16:00.650 Error: dzVents: Error: (3.1.8) every10Seconds: An error occurred when calling event handler Script #1try-fridge-door
2022-11-14 22:16:00.650 Error: dzVents: Error: (3.1.8) every10Seconds: ...s/dzVents/generated_scripts/Script #1try-fridge-door.lua:42: attempt to index a nil value (global 'domoticz')

It's late, I'm tired, yes and I've had a few glasses of wine too. Please can anyone tell me what I'm missing here.

Re: Checking a sensor every few seconds

Posted: Monday 14 November 2022 22:33
by plugge
Replace 'domoticz' in your script (after execute) to 'dz' (without quotes)

Additionally, you should add 'local' in front of 'msg'
I've changed it for you:

Code: Select all

local scriptVar = 'every10Seconds'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_ERROR,
        marker = scriptVar,
    },

    execute = function( dz, item )

        if dz.devices('Fridge Door_D3').state == 'Off' then return end -- If fridge door check device state is Off the script will not do anything,

        if item.isTimer then
            for delay = 10, 50, 10 do
                dz.emitEvent(scriptVar).afterSec(delay)
            end
        end

        local fridgedoor = dz.devices('Fridge Door_D3')
        

        local function setvar(var, to)
            if var.value ~= to and dz.startTime.secondsAgo > 60 then var.set(to) end
        end

        if fridgedoor.lastUpdate.secondsAgo > 60 then
            dz.log('Fridge Door switch >x seconds')
            local msg = "Fridge Door Open " .. fridgedoor.lastUpdate.minutesAgo .. " mins/seconds"  
            dz.openURL('https://api.telegram.org/botxxxxxxxxxxxxxxxxxxxxxxxx/sendMessage?chat_id=xxxxxxx&text=' .. msg)   
        else
            dz.log('Fridge Door closed')
        end
    end
}

Re: Checking a sensor every few seconds

Posted: Tuesday 15 November 2022 10:04
by paul402
Wow! Thanks so much for your help. It works perfectly, of course.