Check variable value (if changed)  [Solved]

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

Moderator: leecollings

Post Reply
TheCondor
Posts: 78
Joined: Thursday 18 June 2015 10:32
Target OS: Linux
Domoticz version:
Contact:

Check variable value (if changed)

Post 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!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Check variable value (if changed)

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
TheCondor
Posts: 78
Joined: Thursday 18 June 2015 10:32
Target OS: Linux
Domoticz version:
Contact:

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

Post by TheCondor »

Perfect!!! Thank you so much!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest