Page 1 of 2

Notification script

Posted: Tuesday 22 September 2020 21:42
by Larsoss
Hey,

I have another question about scripting. I have a number of dummy switches that I have sent a notification. now it is so that a script is running that switches my lamp. which causes me to get a notification quite often.

now I thought it might be possible to make 1 dzvents script in which I put all my idx of my switches of which I want a notification and if it changes status on / off then a notification via the script comes via telegram.

and maybe even adjust the notification.

so at idx 15 is my living room lamp and when it comes on the notification comes "lamp on" and when my dummy "ping television" comes on the notification "the tv is on" comes on.

who can help me in the right direction?

Re: Notification script

Posted: Tuesday 22 September 2020 22:13
by Tjalling94
You can also check the current state of your light.

Code: Select all

if domoticz.devices('lamp').state == 'Off' then
   domoticz.devices('lamp').switchOn();
end
That's enough to prevent duplicate notifications

Re: Notification script

Posted: Tuesday 22 September 2020 22:33
by Larsoss
Ok. But still want to make a script with the notifications :)

Re: Notification script

Posted: Tuesday 22 September 2020 22:50
by kimot
And why script for this "switch custom message", when you can write notification directly in switch notification tab?


2020-09-22-224730_1920x1080_scrot.png
2020-09-22-224730_1920x1080_scrot.png (154.28 KiB) Viewed 2748 times

Re: Notification script

Posted: Tuesday 22 September 2020 22:57
by HvdW
Did you have a look at the 'System-alive-check' script which you can find here to see howto set it up in your own script?

Re: Notification script

Posted: Tuesday 22 September 2020 23:26
by Larsoss
kimot wrote: Tuesday 22 September 2020 22:50 And why script for this "switch custom message", when you can write notification directly in switch notification tab?



2020-09-22-224730_1920x1080_scrot.png
because i get to many notifications, even when there is nothing change's... it still sending me the same notification.

Re: Notification script

Posted: Wednesday 23 September 2020 0:04
by waaren
Larsoss wrote: Tuesday 22 September 2020 23:26 because i get to many notifications, even when there is nothing change's... it still sending me the same notification.
Could look like below

Code: Select all

--[[

        script to send adjustable notifications
        Enter text in description field like below 2 lines
        Text within {} can be changed. device_name and device_id will be substituted by script

        active:{device device_name has been switched on}
        idle:{device device_id has been switched off}

]]--

return 
{
    on = 
    {
        devices = 
        {
            551, 552, 553, -- All devices you want to check
        },
    },

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

    data = 
    {
        devices = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)

        local function substitute(str)
            replacingText =
            {
                ['device_id']  = item.id,
                ['device_name']  = item.name,
            }

            for key, substitution in pairs(replacingText) do
               str = str:gsub(key, substitution) 
            end
           return str 
        end 

        local function procesDescription(state)
            local _, _, msg = string.find(item.description,state .. ":(%b{})") -- get text between {}
            if msg then
                msg = msg:match("[^{].*[^}]") -- remove {}
                msg = substitute(msg)
                dz.log('procesDescription: ' .. msg, dz.LOG_DEBUG)
            else
                dz.log('procesDescription: Text not found in description', dz.LOG_DEBUG)
            end
            return msg
        end

        local function procesNotification( state )
            if dz.data.devices[item.id] ~= state then
                dz.data.devices[item.id] = state
                return procesDescription(state)
            else
                dz.log('procesNotification: State did not change', dz.LOG_DEBUG)
            end
        end

        --main
        local msg = procesNotification(item.active and 'active' or 'idle') 
        if msg then
            dz.log(msg, dz.LOG_DEBUG)  
            dz.notify('notifyer',msg, dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
        end
    end
}

Re: Notification script

Posted: Wednesday 23 September 2020 0:09
by Larsoss
Almost Waaren, still no meassage to telegram.. but you get the point of what i was searching for.

Re: Notification script

Posted: Wednesday 23 September 2020 0:44
by waaren
Larsoss wrote: Wednesday 23 September 2020 0:09 Almost Waaren, still no meassage to telegram.. but you get the point of what i was searching for.
I tested the script before posting and it works for me. Did you add the required text in the description field for the devices at hand?
Do you see any messages in the log?

Re: Notification script

Posted: Wednesday 23 September 2020 0:47
by Larsoss
waaren wrote: Wednesday 23 September 2020 0:44
Larsoss wrote: Wednesday 23 September 2020 0:09 Almost Waaren, still no meassage to telegram.. but you get the point of what i was searching for.
I tested the script before posting and it works for me. Did you add the required text in the description field for the devices at hand?
Do you see any messages in the log?
no error in log

Code: Select all

2020-09-23 00:47:00.567 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-09-23 00:47:18.536 (MQTT Discovery) onCommand 004/Spots Overkapping: Command: 'Off', Level: 0, Color:
2020-09-23 00:47:18.537 (MQTT Discovery) MqttClient::Publish cmnd/spots_overkapping/POWER (OFF)
2020-09-23 00:47:18.590 (MQTT Discovery) 004/Spots Overkapping: Topic: 'tele/spots_overkapping/STATE 'Setting nValue: 1->0, sValue: ''->''
2020-09-23 00:47:18.526 Status: User: Admin initiated a switch command (147/Spots Overkapping/Off)
2020-09-23 00:47:18.732 Status: dzVents: Info: Handling events for: "Spots Overkapping", value: "Off"
2020-09-23 00:47:18.733 Status: dzVents: Info: notifyer: ------ Start internal script: Notificatie Script: Device: "Spots Overkapping (MQTT Discovery)", Index: 147
2020-09-23 00:47:18.733 Status: dzVents: Info: notifyer: ------ Finished Notificatie Script

Code: Select all

--[[

        script to send adjustable notifications
        Enter text in description field like below 2 lines
        Text within {} can be changed. device_name and device_id will be substituted by script

        active:{device device_name has been switched on}
        idle:{device device_id has been switched off}

]]--

return 
{
    on = 
    {
        devices = 
        {
            41, 42, 123, 124, 146, 147, 150, 151, 157 -- All devices you want to check
        },
    },

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

    data = 
    {
        devices = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)

        local function substitute(str)
            replacingText =
            {
                ['device_id']  = item.id,
                ['device_name']  = item.name,
            }

            for key, substitution in pairs(replacingText) do
               str = str:gsub(key, substitution) 
            end
           return str 
        end 

        local function procesDescription(state)
            local _, _, msg = string.find(item.description,state .. ":(%b{dummy is veranderd naar})") -- get text between {}
            if msg then
                msg = msg:match("[^].*[^]") -- remove {}
                msg = substitute(msg)
            end
            return msg
        end

        local function procesNotification( state )
            if dz.data.devices[item.id] ~= state then
                dz.data.devices[item.id] = state
                return procesDescription(state)
            end
        end

        --main
        local msg = procesNotification(item.active and 'active' or 'idle') 
        if msg then
            dz.log(msg)  
            dz.notify('notifyer', msg, dz.PRIORITY_HIGH, nil, nil, dz.NSS_TELEGRAM)
        end
    end
}

Re: Notification script

Posted: Wednesday 23 September 2020 1:02
by waaren
Larsoss wrote: Wednesday 23 September 2020 0:47 no error in log
I added some extra log statements in my original posted code. Please use this updated one and check the log. It might help to find why it is not working for you.

Re: Notification script

Posted: Wednesday 23 September 2020 11:44
by Larsoss
I really don't understand sorry .. and I also understand that you don't want to deliver everything in ready-made solutions .. but I really don't see what I'm forgetting .. I've tried to put text in everything, but stay now you get the message Text not found in description 'in the log.

Re: Notification script

Posted: Wednesday 23 September 2020 12:03
by waaren
Larsoss wrote: Wednesday 23 September 2020 11:44 I really don't understand sorry .. and I also understand that you don't want to deliver everything in ready-made solutions .. but I really don't see what I'm forgetting .. I've tried to put text in everything, but stay now you get the message Text not found in description 'in the log.
Did you enter the exact text in the description field of the device like below? (Click on the edit button of the device in the switches tab to get to this screen)
description.png
description.png (51.53 KiB) Viewed 2627 times

Re: Notification script  [Solved]

Posted: Wednesday 23 September 2020 12:07
by Larsoss
waaren wrote: Wednesday 23 September 2020 12:03
Larsoss wrote: Wednesday 23 September 2020 11:44 I really don't understand sorry .. and I also understand that you don't want to deliver everything in ready-made solutions .. but I really don't see what I'm forgetting .. I've tried to put text in everything, but stay now you get the message Text not found in description 'in the log.
Did you enter the exact text in the description field of the device like below? (Click on the edit button of the device in the switches tab to get to this screen)

description.png
No i did not, now i have done it. and the telegram arive's.. thank you very much.

Re: Notification script

Posted: Tuesday 13 October 2020 13:06
by Larsoss
Hey Waaren,

I have a new dummy with the seasons, it contains 4 selector switches that are triggered to the time of the year.

Now it is Autumn, for example. This works with the% value of 10,20,30,40.

Can I also process it in this script? So if a new season starts (winter for example) that there will also be a message about it?

Re: Notification script

Posted: Tuesday 13 October 2020 13:33
by waaren
Larsoss wrote: Tuesday 13 October 2020 13:06 Can I also process it in this script? So if a new season starts (winter for example) that there will also be a message about it?
Script needs some modification (see below) but yes.

Code: Select all

--[[
        script to send notifications of selector type switches
]]--

return 
{
    on = 
    {
        devices = 
        {
            668,  -- All devices you want to check
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'selector notifyer',
    },

    data = 
    {
        devices = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        
        local function procesNotification( label )
            if dz.data.devices[item.id] ~= label then
                dz.data.devices[item.id] = label
                return label
            else
                dz.log('procesNotification: Label did not change', dz.LOG_DEBUG)
            end
        end

        --main
        local msg = procesNotification(item.levelName) 
        if msg then
            dz.log(msg, dz.LOG_DEBUG)  
            dz.notify('notifyer','Selector ' .. item.name .. ' switched to ' .. msg, dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
        end
    end
}

Re: Notification script

Posted: Tuesday 13 October 2020 13:59
by waaren
Larsoss wrote: Tuesday 13 October 2020 13:06 Can I also process it in this script? So if a new season starts (winter for example) that there will also be a message about it?
But why not combine these functionalities into one script. (after restoring level 0 "Off")

Code: Select all

--[[
        script to set selector switch to season and
        send notifications of levelNames of selector type switches
]]--

local time = '08:30'

local spring = '21/03'
local summer = '21/06'
local autumn = '21/09'
local winter = '21/12'

local seasonDevice = 668

return 
{
    on = 
    {
        timer = 
        {
            'at ' .. time .. ' on ' .. spring,
            'at ' .. time .. ' on ' .. summer,
            'at ' .. time .. ' on ' .. autumn,
            'at ' .. time .. ' on ' .. winter,
        },
        
        devices = 
        {
            seasonDevice,  -- You can add other selector type devices you want to check
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Season selector notifyer',
    },

    data = 
    {
        devices = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        local season = dz.devices(seasonDevice)
        
        local function procesNotification( label )
            if dz.data.devices[item.id] ~= label then
                dz.data.devices[item.id] = label
                return label
            else
                dz.log('procesNotification: Label did not change', dz.LOG_DEBUG)
            end
        end

        --main
        if item.isTimer then 
            if item.trigger:find(winter) then season.switchSelector('Winter')
            elseif item.trigger:find(spring) then season.switchSelector('Spring') 
            elseif item.trigger:find(summer) then season.switchSelector('Summer')
            elseif item.trigger:find(autumn) then season.switchSelector('Autumn')
            else
                season.switchSelector('Very strange season')
            end
        else
            local msg = procesNotification(item.levelName) 
            if msg then
                dz.log(msg, dz.LOG_DEBUG)  
                dz.notify('notifyer','Selector ' .. item.name .. ' switched to ' .. msg, dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
            end
        end
    end
}

Re: Notification script

Posted: Tuesday 13 October 2020 15:24
by Larsoss
waaren wrote: Tuesday 13 October 2020 13:59
Larsoss wrote: Tuesday 13 October 2020 13:06 Can I also process it in this script? So if a new season starts (winter for example) that there will also be a message about it?
But why not combine these functionalities into one script. (after restoring level 0 "Off")

Code: Select all

--[[
        script to set selector switch to season and
        send notifications of levelNames of selector type switches
]]--

local time = '08:30'

local spring = '21/03'
local summer = '21/06'
local autumn = '21/09'
local winter = '21/12'

local seasonDevice = 668

return 
{
    on = 
    {
        timer = 
        {
            'at ' .. time .. ' on ' .. spring,
            'at ' .. time .. ' on ' .. summer,
            'at ' .. time .. ' on ' .. autumn,
            'at ' .. time .. ' on ' .. winter,
        },
        
        devices = 
        {
            seasonDevice,  -- You can add other selector type devices you want to check
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Season selector notifyer',
    },

    data = 
    {
        devices = 
        {
            initial = {},
        },
    },

    execute = function(dz, item)
        local season = dz.devices(seasonDevice)
        
        local function procesNotification( label )
            if dz.data.devices[item.id] ~= label then
                dz.data.devices[item.id] = label
                return label
            else
                dz.log('procesNotification: Label did not change', dz.LOG_DEBUG)
            end
        end

        --main
        if item.isTimer then 
            if item.trigger:find(winter) then season.switchSelector('Winter')
            elseif item.trigger:find(spring) then season.switchSelector('Spring') 
            elseif item.trigger:find(summer) then season.switchSelector('Summer')
            elseif item.trigger:find(autumn) then season.switchSelector('Autumn')
            else
                season.switchSelector('Very strange season')
            end
        else
            local msg = procesNotification(item.levelName) 
            if msg then
                dz.log(msg, dz.LOG_DEBUG)  
                dz.notify('notifyer','Selector ' .. item.name .. ' switched to ' .. msg, dz.PRIORITY_NORMAL,nil,nil,dz.NSS_TELEGRAM)
            end
        end
    end
}
Yep, this works right away. Thank you. And I haven't recovered anything.

The dummy is as above
Image

Re: Notification script

Posted: Friday 23 October 2020 15:46
by Jan Jansen
While converting a blockly script (burglar alarm) I came across this thread. In the event of a burglary, it is relevant to know which door or window is opened. If that can be done via a notification, fantastic! This concerns specific devices under specific circumstances.

Code: Select all

return {
	on = {
		devices = {
			'Voordeur',                                         -- door contact
			'Woonkamer (voorzijde)',                            -- door contact
			'Slaapkamer (voorzijde)',                           -- door contact
			'Keukendeur',                                       -- door contact
			'Inbraak keukendeur',                               -- dummy switch (on delay)
			'Schuurdeur',                                       -- door contact
			'Inbraak schuurdeur',                               -- dummy switch (on delay)
			'Beveiliging'                                       -- dummy selector levels 'Uit', 'Thuis' and 'Afwezig'
		}
	},
	
	logging =
    {
        level = domoticz.LOG_DEBUG, 
        marker = 'inbraak',
    },
	
	execute = function(dz, item)
	    local vd = dz.devices('Voordeur')
	    local wk = dz.devices('Woonkamer (voorzijde)')
	    local slpkmrvz = dz.devices('Slaapkamer (voorzijde)')
	    local kd = dz.devices('Keukendeur')
	    local inbkd = dz.devices('Inbraak keukendeur')
	    local sd = dz.devices('Schuurdeur')
	    local inbsd = dz.devices('Inbraak Schuurdeur')
	    local bev = dz.devices('Beveiliging')
	    local asinb = dz.devices('Alarmsignaal inbraak')
	    local iva = dz.devices('Inschakelvertraging alarm')     -- dummy switch on delay
	    
		if (vd.state == 'On' or wk.state == 'On' or slpkmrvz.state == 'On' or kd.state == 'On' or sd.state == 'On') and bev.state == 'Thuis' then
		    asinb.switchOn()
		    dz.notify('Inbraak (thuis)!!','   ??  geactiveerd',dz.PRIORITY_HIGH)
		end
		if (vd.state == 'On' or wk.state == 'On' or slpkmrvz.state == 'On' or inbkd.state == 'On' or inbsd.state == 'On') and iva.state == 'On' then
		    asinb.switchOn()
		    dz.notify('Inbraak (afwezig)!!','   ??   geactiveerd',dz.PRIORITY_HIGH)
		end
		if bev.state == 'Uit' and asinb == 'On' then
		    asinb.switchOff()
		    dz.notify('Reset alarm','Alarminstallatie gereset',dz.PRIORITY_HIGH)
		end    
	end
}
In case of bev.state == 'Thuis' it only concerns the devices 'Voordeur', Woonkamer (voorzijde), Slaapkamer (voorzijde), Keukendeur and Schuurdeur
In case iva.state == 'On' it only concerns the devices 'Voordeur', Woonkamer (voorzijde), Slaapkamer (voorzijde), Inbraak keukendeur and Inbraak schuurdeur.

Thanks in advance!

Regards Jan

Re: Notification script

Posted: Friday 23 October 2020 16:24
by waaren
Jan Jansen wrote: Friday 23 October 2020 15:46 While converting a blockly script (burglar alarm) I came across this thread. In the event of a burglary, it is relevant to know which door or window is opened.
Can you try after changing

Code: Select all

'   ??   geactiveerd'
to

Code: Select all

item.name .. ' geactiveerd'