Trigger on different text values

Dashticz, alternative dashboard based on HTML, CSS, jQuery

Moderators: leecollings, htilburgs, robgeerts

Post Reply
Vomera
Posts: 184
Joined: Wednesday 06 September 2017 9:11
Target OS: Linux
Domoticz version:
Contact:

Trigger on different text values

Post by Vomera »

Hi All,

I saw Meteoalarm didnt work anymore. So i got a new script that i found on this https://www.domoticz.com/forum/viewtopic.php?t=37919 (thank you for that)
With Meteoalarm i always trigger on text value. For example meteoalarm gives me Wind(3) or Wind(2) (Every time the same text value's), i show a warning on my dashboard with yellow block with wind symbol. (cant show an example because its down already) All text value's coded into the custum.js

Now i want to do that for the KNMI warning messages, but what i see is that the text value's are not the same and changing all the time.

Image

Ofcourse if i use "Zware windstoten tot ongeveer 90 km/uur." it works :

Code: Select all

	else if (device['Data']=="Zware windstoten tot ongeveer 90 km/uur."){
	block.addClass='warningyellow' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'	

	}
Image



Now i was thinking to trigger just on some words, for example Zware windstoten. But the == statement is not usable here

Code: Select all

	else if (device['Data']=="Zware windstoten"){
	block.addClass='warningyellow' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'	

	}
I was thinking of using wildcards or maybe there is a statement with " else if device data contains" instead to collect all the data and put it in the custom.js. I will be a long list then.


Has anyone some advice?
Lokonli
Posts: 2290
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Trigger on different text values

Post by Lokonli »

You can use the JavaScript search method and a regular expression.

See https://www.google.com/url?sa=t&source= ... Um3DzpV2Ku

Verstuurd vanaf mijn AC2003 met Tapatalk

Vomera
Posts: 184
Joined: Wednesday 06 September 2017 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: Trigger on different text values

Post by Vomera »

Here is example that works:

Code: Select all

function getStatus_14146(block){
   var idx = block.idx;
   var device = block.device;
   var tekst = device['Data'];
	
   if(device['Data']=="No Data"){
	block.addClass='none'  
	block.icon = 'far fa-thumbs-up'
	}
	// geen actie
	
	
	else if (tekst.includes('geen waarschuwingen')){
    block.addClass='none' 
	block.icon = 'far fa-thumbs-up'
	block.title = 'Geen Melding'
	}

	
	// zware windstoten
	else if (tekst.includes('Kans op zware windstoten')){
	block.addClass='warningyellow' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'
	}

	
}	
Image

ps. need some editing of the position of the block
Vomera
Posts: 184
Joined: Wednesday 06 September 2017 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: Trigger on different text values

Post by Vomera »

Okay with loading the dashboard i need to use the code
function getStatus_14146(block)

But if there will be a new update, it doesnt change.

So i figure out that you also need to use the

function getChange_14146(block)

It means i have to copy for getStatus as also for the getChange. If i have 100 lines of code for the weather alarm, means i have 100 lines of the same code for getStatus and getChange.

Is there a way to combine it ?

For example, function getChange_14146(block), function getStatus_14146(block) {...}

Image

Code: Select all

// https://cdn.knmi.nl/knmi/xml/rss/rss_KNMIwaarschuwingen.xml
//  STATUS

	function getStatus_14146(block)  {
	var idx = block.idx;
	var device = block.device;
	var tekst = device['Data'];
	
   if(device['Data']=="No Data"){
	block.addClass='none'  
	block.icon = 'far fa-thumbs-up'
	}
	// geen actie
	
	
	else if (tekst.includes('geen waarschuwingen')){
    block.addClass='none' 
	block.icon = 'far fa-thumbs-up'
	block.title = 'Geen Melding'
	}

	
	// zware windstoten
	else if (tekst.includes('op zware windstoten')){
	block.addClass='warningyellow' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'
	}
	
	
	// op zeer zware windstoten

	else if (tekst.includes('op zeer zware windstoten')){
	block.addClass='warningorange' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'
	}

	
}	

// einde status
Lokonli
Posts: 2290
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Trigger on different text values

Post by Lokonli »

You could achieve it by creating one common function as follows:

Code: Select all

function updateKNMI(block) {
	var idx = block.idx;
	var device = block.device;
	var tekst = device['Data'];
	
   if(device['Data']=="No Data"){
	block.addClass='none'  
	block.icon = 'far fa-thumbs-up'
	}
	// geen actie
	
	
	else if (tekst.includes('geen waarschuwingen')){
    block.addClass='none' 
	block.icon = 'far fa-thumbs-up'
	block.title = 'Geen Melding'
	}

	
	// zware windstoten
	else if (tekst.includes('op zware windstoten')){
	block.addClass='warningyellow' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'
	}
	
	
	// op zeer zware windstoten

	else if (tekst.includes('op zeer zware windstoten')){
	block.addClass='warningorange' 
	block.icon = 'fas fa-wind'
	block.title = 'Wind'
	}

	
}	

function getChange_14146(block) {
  updateKNMI(block);
}

function getStatus_14146(block) {
  updateKNMI(block);
}
// einde status
But the getStatus function should be called always, and the getChange function on any change, so it's a surprising that it doesn't work like that. I'll do some tests.
Vomera
Posts: 184
Joined: Wednesday 06 September 2017 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: Trigger on different text values

Post by Vomera »

I test it too, thats an other way to implement it. i was already looking to combine 2 function names

edit: test it, and it works
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest