Page 1 of 1

LUA: To many Telegram messages

Posted: Monday 16 July 2018 12:24
by Twoink
Hi All,

I have the following script to switch on a Fan (433 KAKU switch) if the temperature reaches a certain level.
The script will also send a Telegram message, but.... it looks like "commandArray['Solar: Fan'] = "On" is executed 5 times, so i will receive the message also 5 times.

Does someone have a solution for this issue?

Thanks!

Regards Twoink :D

Code: Select all


function Telegram(message)
    local bot = 'XXXX'; -- Telegram Bot ID
    local token = 'XXXXX'; -- Telegram Bot Token
    local chatId = 'XXXXX'; -- Telegram Chat ID
	
	os.execute('curl --data chat_id='..chatId..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..bot..':'..token..'/sendMessage" ')
	return
end

-------------DEVICES----------------
local fan                   	= otherdevices['Solar: Fan'] 
local temp       		= otherdevices_temperature['Solar: Temperature']
		
-------------SETTINGS----------------
local Set_switch_off_temp   = 35 
local Set_switch_on_temp    = 43  
-----------------------------------------

-- START SCRIPT
commandArray = {}

if (temp >= Set_switch_on_temp and fan == "Off") then
    commandArray['Solar: Fan'] = "On"
    Telegram("Fan is switched ON")
elseif (omvormer_temp <= Set_switch_off_temp and fan == "On") then
    commandArray['Solar: Fan'] = "Off"
    Telegram("Fan is switched OFF")
end

return commandArray

Re: LUA: To many Telegram messages

Posted: Monday 16 July 2018 12:45
by emme
5 times means the script is fired 5 times

if you use pure LUA script (instead of dzVents) you should also ensure your FAN device is within the changed devices
When a script device is fired domoticz will pass the devicechanged table to the script, you should use it to ensure the device is included

your script runs on temp change... good!

Code: Select all

function Telegram(message)
    local bot = 'XXXX'; -- Telegram Bot ID
    local token = 'XXXXX'; -- Telegram Bot Token
    local chatId = 'XXXXX'; -- Telegram Chat ID
	
	os.execute('curl --data chat_id='..chatId..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..bot..':'..token..'/sendMessage" ')
	return
end

-------------DEVICES----------------
local fan                   	= otherdevices['Solar: Fan'] 
local tempName = 'Solar: Temperature'
local temp       		= otherdevices_temperature[tempName]
		
-------------SETTINGS----------------
local Set_switch_off_temp   = 35 
local Set_switch_on_temp    = 43  
-----------------------------------------

-- START SCRIPT
commandArray = {}
if (devicechanged[tempName] ~= nil) then 
   if (temp >= Set_switch_on_temp and fan == "Off") then
       commandArray['Solar: Fan'] = "On"
       Telegram("Fan is switched ON")
   elseif (omvormer_temp <= Set_switch_off_temp and fan == "On") then
       commandArray['Solar: Fan'] = "Off"
       Telegram("Fan is switched OFF")
   end
end

return commandArray

Re: LUA: To many Telegram messages

Posted: Thursday 23 August 2018 20:18
by Twoink
emme wrote: Monday 16 July 2018 12:45 5 times means the script is fired 5 times

if you use pure LUA script (instead of dzVents) you should also ensure your FAN device is within the changed devices
When a script device is fired domoticz will pass the devicechanged table to the script, you should use it to ensure the device is included

your script runs on temp change... good!
Thanks for your reply! It helped, but i don't understand your explanation. How do i ensure that the device is included in the table?

Re: LUA: To many Telegram messages

Posted: Thursday 23 August 2018 23:11
by freijn
Interested in answer as well. :-)

Re: LUA: To many Telegram messages

Posted: Thursday 23 August 2018 23:43
by jvdz
I am not sure why this script can work at all as the variable "omvormer_temp" is nowhere defined!
I also assume this script is a device script and not a time script, which I think you normally would use for this purpose.
Normally you want to run this script either one time per minute(script_time_????.lua) or only when one particular device changes. The latter is done by checking for the device with the statement: if devicechanged[tempName] then
This will ensure the rest of the logic is only performed when that temp actually changed.

Jos

Re: LUA: To many Telegram messages

Posted: Monday 27 August 2018 11:00
by emme
Ciao,

sorry for the delay in reply, just back from my vacation today :P :P

can you please try to convert your script in dzVents as follow?

Code: Select all

function Telegram(message)
    local bot = 'XXXX'; -- Telegram Bot ID
    local token = 'XXXXX'; -- Telegram Bot Token
    local chatId = 'XXXXX'; -- Telegram Chat ID
	
	os.execute('curl --data chat_id='..chatId..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..bot..':'..token..'/sendMessage" ')
	return
end

-------------DEVICES----------------
local fan                   	= otherdevices['Solar: Fan'] 
local tempName = 'Solar: Temperature'
local temp       		= otherdevices_temperature[tempName]
		
-------------SETTINGS----------------
local Set_switch_off_temp   = 35 
local Set_switch_on_temp    = 43  
-----------------------------------------


return {
	on = {
		devices = { tempName }
	},
	execute = function(domoticz, device)
        local temp = domoticz.devices(tempName).temperature 
        local fan  = domoticz.devices('Solar: Fan').state 
        if temp >= Set_switch_on_temp  then
            domoticz.devices('Solar: Fan').switchOn().checkFirst()
            Telegram("Fan is switched ON")
            
        elseif temp < Set_switch_off_temp  then
            domoticz.devices('Solar: Fan').switchOff().checkFirst()
            Telegram("Fan is switched OFF")
        end
	end
}