Mobile home script modification not working

Moderator: leecollings

Post Reply
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Mobile home script modification not working

Post by Fredom »

Dear members,
The script below works perfectly.
But I want something extra and that is that when a mobile leaves home, a message goes to Telegram with the name and how many mobiles are still at home.
I've tried everything necessary but no good result yet.
I turned off the debug rules because these bits now work fine.
I have set the 6 lines in question to read because this gives error messages.
The 7 are the number of registered cell phones.
I hope this is possible in this script.

Code: Select all

local phones =
{
    
    'Mobiel Fred',  
    'Mobiel Hanny',
    'Mobiel Mariska',
    'Mobiel Barney',
    'Mobiel Nils',
    'Mobiel Test',
    'Mobiel Mandy',
    'Thuis', 
}


return
{
    active = true,

    on = {
            devices = phones
        },

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



     execute = function(dz, item)
        local   home = dz.devices('Thuis') 
        local atHome = home.active

        local function phonesActive()
            local activePhones = 0
            for _, phoneName in ipairs(phones) do
                if dz.devices(phoneName).active then
                    dz.log(phoneName .. ' active', dz.LOG_DEBUG)
                    activePhones = activePhones + 1
                end
            end
            if (dz.devices('Thuis').state == 'On') then            
                activePhones = activePhones - 1
            end
            return activePhones
        end
        
        if item.active then    
            if (dz.devices('Thuis').state == 'Off') then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoon binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    --dz.log(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
            else 
                if phonesActive() > 1 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    --dz.log(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().checkFirst().silent()
                end
            end
          
            --else
                --if phonesActive() < 7 then
                    --dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu nog' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt nog niet goed
                    --dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu nog' .. phonesActive() .. ' mobiele telefoons binnen.' dz.LOG_DEBUG)
                --end
            --end

            else
                if phonesActive() < 1 then
                --dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)
                dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                home.switchOff().checkFirst().silent()
            end
           
        end
        
        if (dz.devices('Thuis').state == 'Off') then
            for _, phoneName in ipairs(phones) do
                if dz.devices(phoneName).active then
                   home.switchOn().checkFirst().silent()
                end
            end
        end
    end
}
Thanks in advance
Fred
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waltervl
Posts: 6678
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: Mobile home script modification not working

Post by waltervl »

This is strange, the logging and notification works on entering house but the notification on leaving is not working. Is the logging on leaving really working?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Mobile home script modification not working

Post by Fredom »

waltervl wrote: Sunday 20 February 2022 20:49 This is strange, the logging and notification works on entering house but the notification on leaving is not working. Is the logging on leaving really working?
Thank you for your response.

This is the part that doesn't work.

The rest of the script works perfectly

Code: Select all

          --else
                --if phonesActive() < 7 then
                    --dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu nog' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt nog niet goed
                    --dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu nog' .. phonesActive() .. ' mobiele telefoons binnen.' dz.LOG_DEBUG)
                --end
            --end
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waltervl
Posts: 6678
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: Mobile home script modification not working

Post by waltervl »

And what is not working, do you get errors?
Is there no log when activated?
Do you use elseif instead if else?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Mobile home script modification not working

Post by Fredom »

waltervl wrote: Sunday 20 February 2022 23:45 And what is not working, do you get errors?
Is there no log when activated?
Do you use elseif instead if else?
Hi waltervl,
I solved the problem.
I forgot a comma in the log line and an end statement.
It works perfectly now.
Thanks for thinking along
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Mobile home script modification not working

Post by Fredom »

waltervl wrote: Sunday 20 February 2022 23:45 And what is not working, do you get errors?
Is there no log when activated?
Do you use elseif instead if else?
Hi Waltervl

Unfortunately, the script still doesn't quite work.
Can someone help me to get the last bit working.
The last part of the script where the dummy "Thuis" should be switched off,
and the message that the last mobile is leaving, doesn't work.
I also get messages in the log that I don't understand

Code: Select all

2022-03-06 20:43:57.021 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 20:44:17.080 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 20:46:47.607 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 20:46:57.673 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 21:08:11.345 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 21:09:41.699 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 21:15:42.897 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: false
2022-03-06 21:16:33.076 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 21:20:03.745 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
2022-03-06 21:20:33.845 Error: dzVents: Error: (3.1.8) phones: There is no device with that name or id: true
The intent of this script should be.
Mobile comes in "Thuis" switch on.
Last mobile leaves "Thuis" switch Off.

And here the script as it is now

Code: Select all

local phones =
{
    
    'Mobiel Fred',  
    'Mobiel Hanny',
    'Mobiel Mariska',
    'Mobiel Barney',
    'Mobiel Nils',
    'Mobiel Test',
    'Mobiel Mandy',
}


return
{
    active = true,

    on = {
            devices = phones
        },

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



     execute = function(dz, item)
        local   home = dz.devices('Thuis') 
        local atHome = home.active

        local function phonesActive()
            local activePhones = 0
            for _, phoneName in ipairs(phones) do
                if dz.devices(phoneName).active then
                    dz.log(phoneName .. ' active', dz.LOG_DEBUG)
                    activePhones = activePhones + 1
                end
            end
        
            return activePhones
        end
        
        if item.active then    
            if not  dz.devices(atHome) then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoon binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().silent()   --werkt goed
            else 
                if phonesActive() > 0 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().checkFirst().silent()   --werkt goed
                end
            end
          
            else
                if phonesActive() < 7 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)

            else
                if phonesActive() < 1 then   -- not workng
                    home.switchOff().checkFirst().silent()   --not working
                    dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)   --not working
                    dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --nt working
            end
        end
    end
end
}
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waltervl
Posts: 6678
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: Mobile home script modification not working

Post by waltervl »

It looks like somewhere in the script you mixed up a true/false check with a phone name check. Therefore the strange errors.

Put a log in the script that always shows the value of phonesActive(), that will make debugging easier.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Mobile home script modification not working

Post by boum »

Line 47:

Code: Select all

if not  dz.devices(atHome) then
should be:

Code: Select all

if not atHome then
Then your last test is never executed because your if/else/end blocks does not match. Bad indentation does not help.
It is currently:

Code: Select all

			if phonesActive() < 7 then
				dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
				dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)

			else
				if phonesActive() < 1 then   -- not workng
					home.switchOff().checkFirst().silent()   --not working
					dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)   --not working
					dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --nt working
				end
			end
When phonesActive() is less than 7, the else part is never executed, which is the case for < 1. You should invert the two tests or if you want both notifications move the second part out of the else block:

Code: Select all

			if phonesActive() < 7 then
				// ...
			end

			if phonesActive() < 1 then
				// ...
			end
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Mobile home script modification not working

Post by Fredom »

waltervl wrote: Sunday 06 March 2022 23:32 It looks like somewhere in the script you mixed up a true/false check with a phone name check. Therefore the strange errors.

Put a log in the script that always shows the value of phonesActive(), that will make debugging easier.

Hi waltervl,
If I knew how to do that, I certainly would. I can make minor adjustments to an existing script and hope it works, unfortunately nothing more. But I am learning more and more.
I no longer have errors in the log file thanks to boum's tip.
"if not atHome then"
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Mobile home script modification not working

Post by Fredom »

boum wrote: Monday 07 March 2022 10:02 Line 47:

Code: Select all

if not  dz.devices(atHome) then
should be:

Code: Select all

if not atHome then
Then your last test is never executed because your if/else/end blocks does not match. Bad indentation does not help.
It is currently:

Code: Select all

			if phonesActive() < 7 then
				dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
				dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)

			else
				if phonesActive() < 1 then   -- not workng
					home.switchOff().checkFirst().silent()   --not working
					dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)   --not working
					dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --nt working
				end
			end
When phonesActive() is less than 7, the else part is never executed, which is the case for < 1. You should invert the two tests or if you want both notifications move the second part out of the else block:

Code: Select all

			if phonesActive() < 7 then
				// ...
			end

			if phonesActive() < 1 then
				// ...
			end
Hi boum,
Thanks for your quick response.
I think I did what you suggested. The domoticz log file now has no errors.
But if a mobile comes in now, I get a message from Fred coming home. There is now 1 mobile phone inside.
and Fred leaves. There are now 1 cell phones inside. And when the mobile really leaves, there is no notification and "Home" does not switch off either

I'm not a programmer but I can make small adjustments in a script

Code: Select all

local phones =
{
    
    'Mobiel Fred',  
    'Mobiel Hanny',
    'Mobiel Mariska',
    'Mobiel Barney',
    'Mobiel Nils',
    'Mobiel Test',
    'Mobiel Mandy',
}


return
{
    active = true,

    on = {
            devices = phones
        },

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



     execute = function(dz, item)
        local   home = dz.devices('Thuis') 
        local atHome = home.active

        local function phonesActive()
            local activePhones = 0
            for _, phoneName in ipairs(phones) do
                if dz.devices(phoneName).active then
                    dz.log(phoneName .. ' active', dz.LOG_DEBUG)
                    activePhones = activePhones + 1
                end
            end
        
            return activePhones
        end
        
        if item.active then    
            if not  atHome then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoon binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().silent()   --werkt goed
            else 
                if phonesActive() > 0 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().checkFirst().silent()   --werkt goed
                end
            end
                if phonesActive() < 4 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu ' .. phonesActive() .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                end
            
                if phonesActive() < 1 then   -- not workng
                    home.switchOff().checkFirst().silent()   --not working
                    dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)   --not working
                    dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --nt working
                end
            end
        end
}
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Mobile home script modification not working

Post by boum »

Like before, your chaining of if/then/else is faulty. You moved some things around and now there is no "else" clause for the "item.active" that's why "nobody is leaving"

Code: Select all

local phones =
{
    
    'Mobiel Fred',  
    'Mobiel Hanny',
    'Mobiel Mariska',
    'Mobiel Barney',
    'Mobiel Nils',
    'Mobiel Test',
    'Mobiel Mandy',
}


return
{
    active = true,

    on = {
            devices = phones
        },

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



     execute = function(dz, item)
        local   home = dz.devices('Thuis') 
        local atHome = home.active

	-- don't need a function for that, it won't change during the invocation of the script
            local activePhones = 0
            for _, phoneName in ipairs(phones) do
                if dz.devices(phoneName).active then
                    dz.log(phoneName .. ' active', dz.LOG_DEBUG)
                    activePhones = activePhones + 1
                end
            end
        
        if item.active then
            -- this is when a phone enters
            if activePhones==1 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. activePhones .. ' mobiele telefoon binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().silent()   --werkt goed
            else 
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
            end
            home.switchOn().checkFirst().silent()   --werkt goed
        else
            -- this is when a phone leaves
            if activePhones > 0 then
                -- there is still somebody
                dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
            else
                -- everybody is gone
                home.switchOff().checkFirst().silent() 
                dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)   --not working
                dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --nt working
            end
        end
    end
}
Make sure your code is in the correct scope.
Also, the script is executed once when a device change, so no need to compute the number of active phones many times, it won't change until the script is run another time because this or another trigger is hit.
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Mobile home script modification not working

Post by Fredom »

boum wrote: Monday 07 March 2022 18:35 Like before, your chaining of if/then/else is faulty. You moved some things around and now there is no "else" clause for the "item.active" that's why "nobody is leaving"

Code: Select all

local phones =
{
    
    'Mobiel Fred',  
    'Mobiel Hanny',
    'Mobiel Mariska',
    'Mobiel Barney',
    'Mobiel Nils',
    'Mobiel Test',
    'Mobiel Mandy',
}


return
{
    active = true,

    on = {
            devices = phones
        },

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



     execute = function(dz, item)
        local   home = dz.devices('Thuis') 
        local atHome = home.active

	-- don't need a function for that, it won't change during the invocation of the script
            local activePhones = 0
            for _, phoneName in ipairs(phones) do
                if dz.devices(phoneName).active then
                    dz.log(phoneName .. ' active', dz.LOG_DEBUG)
                    activePhones = activePhones + 1
                end
            end
        
        if item.active then
            -- this is when a phone enters
            if activePhones==1 then
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. activePhones .. ' mobiele telefoon binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt thuis. Er is nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
                    home.switchOn().silent()   --werkt goed
            else 
                    dz.notify(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                    dz.log(item.name:gsub('Mobiel ', '') .. ' komt ook thuis. Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
            end
            home.switchOn().checkFirst().silent()   --werkt goed
        else
            -- this is when a phone leaves
            if activePhones > 0 then
                -- there is still somebody
                dz.notify(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --werkt goed
                dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt.Er zijn nu ' .. activePhones .. ' mobiele telefoons binnen.', dz.LOG_DEBUG)
            else
                -- everybody is gone
                home.switchOff().checkFirst().silent() 
                dz.log(item.name:gsub('Mobiel ', '') .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.LOG_DEBUG)   --not working
                dz.notify('Devices'.. item.name .. ' vertrekt. Er zijn nu geen mobiele telefoons meer binnen.', dz.PRIORITY_MEDIUM, dz.SOUND_PERSISTENT, nil, dz.NSS_TELEGRAM) --nt working
            end
        end
    end
}
Make sure your code is in the correct scope.
Also, the script is executed once when a device change, so no need to compute the number of active phones many times, it won't change until the script is run another time because this or another trigger is hit.

Dear boum,

It works perfectly now.
I hope to learn something from this.
Thanks a lot for your help
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest