Page 1 of 1

Weather alarm Telegram script

Posted: Saturday 06 January 2024 13:10
by BazemanKM
Made a script to send the text off weather alarm to Telegram, but it is sending every ten minutes the same message, how to make the script that only a new message is send?

Code: Select all

return {
	on = {
		devices = {'Weeralarm'}
	},
	
	execute = function(domoticz)
        local Weeralarm = domoticz.devices('Weeralarm')
        local teleTok   = 'teletokAPI'
        local chatId    = 'chatId'
        
        if Weeralarm.state ~= 'Er zijn momenteel geen waarschuwingen van kracht.' then
            os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendMessage" -d chat_id='..chatId..' -d text="'..Weeralarm.state..'"')
        
        end

	end
}


Re: Weather alarm Telegram script

Posted: Saturday 06 January 2024 13:25
by jvdz
Save the send Weeralarm.state value in a persistent data variable and compare each run whether it was changed.

Untested:

Code: Select all

return {
   on      = {
      devices = { 'Weeralarm' }
   },
   data    = {
      LastWeeralarm = { initial = '' },
   },
   execute = function(domoticz)
      local Weeralarm = domoticz.devices('Weeralarm')
      local teleTok   = 'teletokAPI'
      local chatId    = 'chatId'

      if Weeralarm.state ~= 'Er zijn momenteel geen waarschuwingen van kracht.' then
         if domoticz.data.LastWeeralarm ~= Weeralarm.state then
            os.execute('curl -s -X POST "https://api.telegram.org/bot' .. teleTok .. '/sendMessage" -d chat_id=' .. chatId .. ' -d text="' .. Weeralarm.state .. '"')
            domoticz.data.LastWeeralarm = Weeralarm.state
         end
      else
         domoticz.data.LastWeeralarm = ''
      end
   end
}


Re: Weather alarm Telegram script

Posted: Saturday 06 January 2024 13:54
by BazemanKM
Some errors, two time if after eachother. But thanks for the direction.

Re: Weather alarm Telegram script

Posted: Saturday 06 January 2024 14:03
by jvdz
Sorry, Guess I forgot a then on the first if. The 2 if's are required.

Re: Weather alarm Telegram script

Posted: Saturday 06 January 2024 14:14
by BazemanKM
With this i got the message once

Code: Select all

return {
   on = {
      devices = {'Weeralarm'}
   },
   data    = {
      LastWeeralarm = { initial = '' },
   },
   execute = function(domoticz)
        local Weeralarm = domoticz.devices('Weeralarm')
        local teleTok   = 'API'
        local chatId    = 'ChatID'

        if domoticz.data.LastWeeralarm ~= Weeralarm.state then
            os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendMessage" -d chat_id='..chatId..' -d text="'..Weeralarm.state..'"')
            domoticz.data.LastWeeralarm = Weeralarm.state
        
      end

   end
}

Re: Weather alarm Telegram script

Posted: Saturday 06 January 2024 14:29
by jvdz
That works too, but will now send also a telegram when there's no warning anymore. ;)

Re: Weather alarm Telegram script

Posted: Saturday 06 January 2024 22:27
by HvdW
Here's one more:

Code: Select all

return {
    on = {
        timer = { 'every 10 minutes' },
        httpResponses = { 'WeatherLiveDataRetrieved' },
    },    
        logging = {
	    level = domoticz.LOG_ERROR, 
	    marker = 'Weerlive DEBUG data'
	
	},
    execute = function(domoticz, item)
        local teleTok   = 'API'
        local chatId    = 'ChatID'
        
        if (item.isTimer) then
            domoticz.openURL({
            url = "http://weerlive.nl/api/json-data-10min.php?key=f6s372b8&locatie=52.22234, 4.53300",
            method = 'GET',
            callback = 'WeatherLiveDataRetrieved'
            })
        elseif (item.isHTTPResponse) then
            if (item.ok) then -- statusCode == 2xx
                -- Display all data from weerlive with the domoticz.utils.dumpTable(item.json) statement
                -- either	domoticz.log(domoticz.utils.dumpTable(item.json),domoticz.LOG_DEBUG)
                -- or  	domoticz.utils.dumpTable(item.json)
                domoticz.log('weather ALARM text is    ' .. item.json.liveweer[1].alarmtxt,domoticz.LOG_DEBUG)
            end
            if (item.json.liveweer[1].alarm == '1') then -- alarm state 0 or 1
                os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendMessage" -d chat_id='..chatId..' -d text="'..item.json.liveweer[1].alarm..'"')
                -- or, using domoticz notification
                domoticz.notify('WHEATHER ALARM!', "you don't need to panic." .. item.json.liveweer[1].alarmtxt, domoticz.PRIORITY_HIGH)
            end
        end
    end
}