Community discussions

MikroTik App
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Temperature in Celsius degrees

Wed Nov 23, 2022 12:04 pm

Hi, I have a script that warns me in case the system temperature rises above the defined threshold.
{
:local tempSystem [/system health get [find name=temperature] value]
:local DeviceName [/system identity get name]
:local tempMax "45";

:if ($tempSystem >= $tempMax) do={

    :log error "HIGH Temperature Alert: $tempSystem C";

    # START Send Telegram Module
    :local MessageText "\E2\9A\A0 <b>Router $DeviceName:</b>%0AHIGH Temperature Alert: $tempSystem C";
    :local SendTelegramMessage [:parse [/system script get MyTGBotSendMessage source]];
    $SendTelegramMessage MessageText=$MessageText;
    # END Send Telegram Module

    /tool e-mail send to="xxxxxxx@gmail.com" subject="\E2\9A\A0 [$DeviceName] HIGH Temperature Alert" body="The temperature in the router has reached a value of $tempSystem C"

} else={
:log info "The temperature is within the normal range: $tempSystem C"
}
}
The problem is that I can only put a "C" without the "º" symbol before it because RouterOS does not recognize this symbol in the results.

What I want: 45 ºC
What I have achieved: 45 C

The results of the script should send a message via Telegram and gives error when I put the code "%B0" or "\B0" that according to this table corresponds to it: https://www.barcodefaq.com/ascii-chart-char-set/.

It also sends an email and I use this HTML code without success: "&#176;" or "&deg;".

I don't get results in the syslog either.

Any advice?

BR.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Temperature in Celsius degrees  [SOLVED]

Wed Nov 23, 2022 1:11 pm

Any advice?
Just ask ;)

For other users:
E2 9A A0 is the Bytes Code for the entry code UTF-8 value U+26A0, the ⚠
C2 B0 is the Bytes Code for the entry code UTF-8 value U+00B0 = °

(Please check and understand also the other changes...)
/system 
:local tempSystem [:tonum ([health get [find where name=temperature]]->"value")]
:local DeviceName [identity get name]
:local tempMax    45
:if ($tempSystem >= $tempMax) do={
    :log error "HIGH Temperature Alert: $tempSystem\B0\43"
    # START Send Telegram Module
    :local MessageText "\E2\9A\A0 <b>Router $DeviceName:</b>%0AHIGH Temperature Alert: $tempSystem\C2\B0\43"
    :local SendTelegramMessage [:parse [/system script get MyTGBotSendMessage source]]
    $SendTelegramMessage MessageText=$MessageText
    # END Send Telegram Module
    /tool e-mail send to="<MAIL-ADDRESS>" subject="\E2\9A\A0 [$DeviceName] HIGH Temperature Alert" \
    body="The temperature in the router has reached a value of $tempSystem\C2\B0\43"
} else={
    :log info "The temperature is within the normal range: $tempSystem\B0\43"
}

I have not tested if MessageText is correctly translated to the correct URL when fetching within MyTGBotSendMessage and what the Telegram API accepts.
But the correct way to write that string is not with \xx.
If the URL is not converted to the correct URL inside by MyTGBotSendMessage (ignoring if the router name has spaces or special characters),
it should be written on one of that modes:

STRICT URL code

"%E2%9A%A0+%3Cb%3ERouter+$DeviceName%3A%3C%2Fb%3E%0AHIGH+Temperature+Alert%3A+$tempSystem%C2%B0%43"

LOOSE URL code

"%E2%9A%A0 <b>Router $DeviceName:</b>%0AHIGH Temperature Alert: $tempSystem%C2%B0%43"


You can check the other codes for ASCII 8-bit CP1252 to UTF-8 here
viewtopic.php?f=9&t=177551#p967513

terminal code

:put [$ASCIItoCP1252toUTF8 ("\B0")]
%C2%B0
Last edited by rextended on Wed Nov 23, 2022 2:54 pm, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Temperature in Celsius degrees

Wed Nov 23, 2022 2:03 pm

.
Addendum:
Telegram do not use ASCII or 1252, but UTF-8,
Is why ⚠ and other UTF-8 "only" symbols are supported.

(probably also your mail program if ⚠ appear correctly)

If you want, on telegram and mail only, you can use directly the dedicated symbol DEGREE CELSIUS (CENTIGRADE) (UTF-8 U+2103)

The Bytes code is \E2\84\83
URL encoded: %E2%84%83


RouterOS not support at all ASCII 8 bit or UTF-8,
only standard ASCII 7 bit characters from ! 0x21 to ~ 0x7E
also Tabulation 0x09 (\t), New line 0x0A (\n), Return 0x0D (\r), <SPACE> 0x20, <DEL> 0x7F
 
User avatar
diamuxin
Member
Member
Topic Author
Posts: 317
Joined: Thu Sep 09, 2021 5:46 pm
Location: Alhambra's City

Re: Temperature in Celsius degrees

Wed Nov 23, 2022 2:41 pm

Thanks Rex!

Tested this way and everything is perfect:
Syslog: $tempSystem\B0\43 -> 45ºC
Email: $tempSystem\C2\B0\43 -> 45ºC (directly the dedicated symbol not work)
Telegram: $tempSystem\C2\B0\43 -> 45ºC. (directly the dedicated symbol not work)
In Telegram the line break is sufficient with "%0A".

I guess you can put directly C instead of \43, right?
/system 
:local tempSystem [:tonum ([health get [find where name=temperature]]->"value")]
:local DeviceName [identity get name]
:local tempMax    25
:if ($tempSystem >= $tempMax) do={
    :log error "HIGH Temperature Alert: $tempSystem\B0\43"
    # START Send Telegram Module
    :local MessageText "\E2\9A\A0 <b>Router $DeviceName:</b>%0AHIGH Temperature Alert: $tempSystem\C2\B0\43"
    :local SendTelegramMessage [:parse [/system script get MyTGBotSendMessage source]]
    $SendTelegramMessage MessageText=$MessageText
    # END Send Telegram Module
    /tool e-mail send to="<MAIL-ADDRESS>" subject="\E2\9A\A0 [$DeviceName] HIGH Temperature Alert" \
    body="The temperature in the router has reached a value of $tempSystem\C2\B0\43"
} else={
    :log info "The temperature is within the normal range: $tempSystem\B0\43"
}

If you are interested in the Telegram module MyTGBotSendMessage call (it is another script) this is the content:

:local BotToken "XXXXXXXXXX:XXXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXX";
:local ChatID "XXXXXXXXXX";
:local parseMode "HTML";
:local SendText $MessageText;

/tool fetch url="https://api.telegram.org/bot$BotToken/sendMessage\?chat_id=$ChatID&parse_mode=$parseMode&text=$SendText" keep-result=no;

:log info "==> Send Telegram message";
BR.

Who is online

Users browsing this forum: Bing [Bot], ko00000000001 and 18 guests