Page 1 of 1

Selectord.lastLevel never takes the value 0

Posted: Monday 05 April 2021 21:11
by boggiz
Hi all,
I am trying to program a watering selector so as not to disrupt a watering in progress: it is mandatory to go through STOP before changing the watering mode.
When level 0 is hidden my script works correctly but the icon remains blue even in the STOP position which is not normal.
On the other hand, when I activate level 0 the script does not work because devSel.lastLevel never takes the value 0. On the other hand, the icon becomes grayed out which I like more.
Image

Code: Select all

	execute = function(dz, item)
	    local devSel = dz.devices('Arrosage')
        local varEtat = dz.variables('Etat Arrosage Auto')

            print('==> Selecteur NEW = ' .. devSel.level)
            print('==> Selecteur LAST = ' .. devSel.lastLevel)
            
        -- Selecteur sur Auto ou Auto fixe
        if devSel.level > 30 then 
            varEtat.set ('Manuel') 
        elseif  devSel.level > 10 then 
                varEtat.set ('En cours')
            else
                varEtat.set ('Terminé')
                print('Color ' ..devSel.getColor)
                devSel.icon= Light
                devSel.setHotWater(true) -- non defini

            end
    
        if  devSel.level > 10 and devSel.lastLevel > 10  then   -- <> de STOP on interdit la maj
                devSel.switchSelector(devSel.lastLevel).silent()
        end
    end
Do you know how to either change the color of the STOP icon or force devSel.lastLevel = 0?

Re: Selectord.lastLevel never takes the value 0

Posted: Tuesday 06 April 2021 10:05
by waaren
boggiz wrote: Monday 05 April 2021 21:11 Do you know how to either change the color of the STOP icon or force devSel.lastLevel = 0?
The lastLevel is not set to 0 when switching the selector to Off.
You will have to store the level yourself. You can do that using a uservariable or (like in below example) using dzVents persistent storage.

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Arrosage'
        },
    },
    data =
    {
        lastLevel =
        {
            initial = -1,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'selectorStop',
    },

    execute = function(dz, item)

        local devSel = dz.devices('Arrosage')
        local varEtat = dz.variables('Etat Arrosage Auto')

        dz.log('==> Selecteur NEW = ' .. devSel.level,dz.LOG_DEBUG)
        dz.log('==> Selecteur LAST = ' .. devSel.lastLevel,dz.LOG_DEBUG)

        -- Selecteur sur Auto ou Auto fixe
        if devSel.level > 30 then
            varEtat.set ('Manuel')
        elseif devSel.level > 10 then
            varEtat.set ('En cours')
        else
            varEtat.set ('Terminé')
            dz.log('Color ' .. devSel.getColor,dz.LOG_DEBUG)
            devSel.icon= Light
            devSel.setHotWater(true) -- non defini
        end

        if  devSel.level > 10 and dz.data.lastLevel > 10  then   -- <> de STOP on interdit la maj
            devSel.switchSelector(dz.data.lastLevel).silent()
        end
        dz.data.lastLevel = devSel.level
    end
}

Re: Selectord.lastLevel never takes the value 0

Posted: Tuesday 06 April 2021 10:28
by boggiz
Thank you Waaren for your response.
So I'll go through a persistent variable to store lastLevel.
Is this a bug or was it intended?

Re: Selectord.lastLevel never takes the value 0

Posted: Tuesday 06 April 2021 10:48
by waaren
boggiz wrote: Tuesday 06 April 2021 10:28 Is this a bug or was it intended?
It is intended.

Re: Selectord.lastLevel never takes the value 0

Posted: Tuesday 06 April 2021 10:58
by boggiz
OK. Thank you
... To the next what will inevitably happen !
Boggiz