Hello I need to play a sound alert when an alarm is present

I already set a button with necessary javascript code to play it manually. Next step, play the file with an onchange javascript event or similar, additionally the ewon boolean tag have to be equals to 1 to play it. Do you have any suggestion?

Hi @jjdesarrollosistemas

Yes this is do able but will require you to write some javascript to accomplish it.
Basically we simply need to set up a “watchdog” that will check if your tag value is 1 or a 0.

Steps:

  1. Add a new Javascript section to the view you would like the audio to play on.
    To do this, simply click any white area on the canvas, then under actions select the new icon and select Javascript Sections.

  1. Paste in the following script, you will need to update the fields specified otherwise it will not work as expected.

var mp3FileName = ""; // Update this to be your audio file name. NOTE: This file must be in the root usr folder
var value = getEwonTagVal('YourTagName'); // Update this to be the tag to monitor

/*
 * Check if there is a DOM element to play the audio, if none exists, add it and set it to play your audio. 
 * This will only run once.
 */
if($('#myAudio').length == 0){
     console.log("Audio element does not exist. Adding once");
     var audio = document.createElement('audio');
     audio.setAttribute('id', 'myAudio');
     audio.setAttribute('src', '../' + mp3FileName);
     $('body').append(audio);
}

/*
 * If your tag name equals 1, play the audio file you have loaded. 
 * If the value is a 0, stop any playing audio and reset the play
 * runtime to zero. 
 * 
 * This will execute every X seconds. (defined by interval)
 */
if(value === 1) {
     $('#myAudio')[0].play();
}else{
     $('#myAudio')[0].pause();
     $('#myAudio')[0].currentTime = 0;
}


That is it!

1 Like

Hi Jordan,

I’ll try it, when I get the eWON back.

Thanks for your support

1 Like

Sounds good.

Hi Jordon,

I have copied above code and trying to play the sound when my tag = 1. Unfortunately is not working. Below is the code for your reference. Can you please advise, what could be the wrong?

My mp3 file name is warning.mp3 and Tag is ALARM. I have put the .mp3 in USR folder.

var mp3FileName = "warning.mp3"; // Update this to be your audio file name. NOTE: This file must be in the root usr folder
var value = getEwonTagVal('ALARM'); // Update this to be the tag to monitor

 /*
 * Check if there is a DOM element to play the audio, if none exists, add it and set it to play your audio. 
 * This will only run once.
 */
if($('#myAudio').length == 0){
     console.log("Audio element does not exist. Adding once");
     var audio = document.createElement('audio');
     audio.setAttribute('id', 'myAudio');
     audio.setAttribute('src', '../' + mp3FileName);
     $('body').append(audio);
}

/*
 * If your tag name equals 1, play the audio file you have loaded. 
 * If the value is a 0, stop any playing audio and reset the play
 * runtime to zero. 
 * 
 * This will execute every X seconds. (defined by interval)
 */
if(value === 1) {
     $('#myAudio')[0].play(10);
}else{
     $('#myAudio')[0].pause(10);
     $('#myAudio')[0].currentTime = 0;
}

Hello @Mihir,

Unfortunately Jordan may not be around to help with this. A good place for you to start trouble shooting this would be to check the Browsers Javascript console for any error or messages. This should point your towards what is not working in the script.

Deryck

It is solved. I put the code in viewon script and executed by a button press.