LUA: To many Telegram messages

Moderator: leecollings

Post Reply
Twoink
Posts: 8
Joined: Monday 16 July 2018 11:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

LUA: To many Telegram messages

Post 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
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: LUA: To many Telegram messages

Post 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
The most dangerous phrase in any language is:
"We always done this way"
Twoink
Posts: 8
Joined: Monday 16 July 2018 11:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA: To many Telegram messages

Post 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?
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: LUA: To many Telegram messages

Post by freijn »

Interested in answer as well. :-)
User avatar
jvdz
Posts: 2269
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: LUA: To many Telegram messages

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: LUA: To many Telegram messages

Post 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
}
The most dangerous phrase in any language is:
"We always done this way"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest