Page 1 of 1

Check variable value (if changed)

Posted: Tuesday 28 January 2020 13:11
by TheCondor
Hi, i want to do some actions when the content of a variable is changed.
Currently i have this script (the script is more articulated but i put the essential part) that do some actions according the value of two variable (Tag_Andrea - Tag_Ilaria) which is controlled by BLE presence script. These variable could have two string content: Out when someone is away from the house, Home when someone is back:

Code: Select all

	on = {
	       variables = {
		                    'Tag_Andrea',
		                    'Tag_Ilaria'
	                    },
        },

execute = function(domoticz, Tag)

if domoticz.variables('Tag_Andrea').value == 'Out' and domoticz.variables('Tag_Ilaria').value == 'Out' then

-------------- TURN ON THE ALARM AND DO MUCH MORE

elseif domoticz.variables('Tag_Andrea').value == 'Home' or domoticz.variables('Tag_Ilaria').value == 'Home' then

-------------- TURN OFF THE ALARM AND DO MUCH MORE
Now the problem is that often BLE plugin retest the value and write again the variables with the same content, but updating the 'last update' value.
How can i edit this script to be triggered only on variable content changed?
Thanks since now for your help!

Re: Check variable value (if changed)

Posted: Tuesday 28 January 2020 14:48
by waaren
TheCondor wrote: Tuesday 28 January 2020 13:11 Hi, i want to do some actions when the content of a variable is changed.
How can i edit this script to be triggered only on variable content changed?
domoticz does not keep the value before the update anywhere so you wil have to do that in dzVents. That is why the script need to start but can stop immediate when the updated value is equal to the value before the update.

Code: Select all

local tagAndrea = 'Tag_Andrea'
local tagIlaria = 'Tag_Ilaria'

return 
{
    on = 
    {
        variables = 
        {
            tagAndrea,
            tagIlaria,
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'vars with condition',
    },

    data = 
        { 
            tagAndrea , 
            tagIlaria 
        },
     
    
    execute = function(dz, item)
        if item.value == dz.data[item.name] then return end
        dz.data[item.name] = item.value

        local tagAndrea = dz.variables(tagAndrea).value
        local tagIlaria = dz.variables(tagIlaria).value

        if tagAndrea == 'Out' and tagIlaria == 'Out' then
            -------------- TURN ON THE ALARM AND DO MUCH MORE
            dz.log('Andrea and Ilaria out',dz.LOG_DEBUG)
        elseif tagAndrea == 'Home' or tagIlaria == 'Home' then
            dz.log('Andrea and / or Ilaria home ',dz.LOG_DEBUG)
            -- ----------- TURN OFF THE ALARM AND DO MUCH MORE
        else
            dz.log('Does anyone know where Andrea and / or Ilaria is ? ',dz.LOG_DEBUG)
        end
    end
}

Re: Check variable value (if changed)  [Solved]

Posted: Thursday 30 January 2020 20:50
by TheCondor
Perfect!!! Thank you so much!