Weather alarm Telegram script

Moderator: leecollings

Post Reply
BazemanKM
Posts: 35
Joined: Wednesday 22 July 2015 21:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Weather alarm Telegram script

Post 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
}

User avatar
jvdz
Posts: 2211
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Weather alarm Telegram script

Post 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
}

New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
BazemanKM
Posts: 35
Joined: Wednesday 22 July 2015 21:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weather alarm Telegram script

Post by BazemanKM »

Some errors, two time if after eachother. But thanks for the direction.
User avatar
jvdz
Posts: 2211
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Weather alarm Telegram script

Post by jvdz »

Sorry, Guess I forgot a then on the first if. The 2 if's are required.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
BazemanKM
Posts: 35
Joined: Wednesday 22 July 2015 21:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Weather alarm Telegram script

Post 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
}
Last edited by BazemanKM on Saturday 06 January 2024 17:14, edited 1 time in total.
User avatar
jvdz
Posts: 2211
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Weather alarm Telegram script

Post by jvdz »

That works too, but will now send also a telegram when there's no warning anymore. ;)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
HvdW
Posts: 540
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Weather alarm Telegram script

Post 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
}
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests