Telegram notifier script for xiaomi temperature sensor

Hi!

I need a rule for my Xiaomi temperature and humidity sensor. I need to send notifications to my tg bot when the temperature is greater than 23 degrees. I try to use this:

rule "Xiaomi temp sensor"
when
    Channel "mihome:sensor_ht:xxxxxxxxxxxx:temperature" changed from 22.32 to 23.12
then
    sendTelegram("bot1", "Temp is changed")
end

but it’s not working ((

EDITED: And how to edit the script so that will sends notifications once an hour or only once ?

Thats because you use the channel.
Use the temperature item. And do it more “correctly” like this:

rule "Xiaomi temp sensor"
when
    Item [xiaomi temp sensor item] changed
then
   if ([xiaomi temp sensor item] > 23 )
    sendTelegram("bot1", "Temp is changed")
end

And, you should pay attention to the logfile as well. I would assume your rule would be triggering errors.

Thank for your support!
I change the script:

rule "Xiaomi temp sensor"
when
    Item [mihome:sensor_ht:xxxxxxxxxxxxxxx:temperature] changed
then
   if ([mihome:sensor_ht:xxxxxxxxxxxxxxxx:temperature] > 23 )
    sendTelegram("bot1", "Temp is changed")
end

but at the log file:

020-04-30 20:06:02.418 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'telegram.rules' has errors, therefore ignoring it: [3,5]: no viable alternative at input '['
[3,32]: mismatched character 'c' expecting set null
[5,15]: mismatched input ':' expecting ']'
[5,16]: missing ')' at 'sensor_ht'
[5,25]: mismatched input ':' expecting 'end'
[5,30]: mismatched character 'c' expecting set null

Ahh, you need to remove the brackets [ ] I just inserted those so you could see where to put your item.

Like this:

rule "Xiaomi temp sensor"
when
    Item mihome:sensor_ht:xxxxxxxxxxxxxxx:temperature changed
then
   if (mihome:sensor_ht:xxxxxxxxxxxxxxxx:temperature > 23 )
    sendTelegram("bot1", "Temp is changed")
end
cat /etc/openhab2/rules/telegram.rules
rule "Xiaomi temp sensor"
when
    Item mihome:sensor_ht:xxxxxxxxxxx:temperature changed
then
   if (mihome:sensor_ht:xxxxxxxxxxxxxxxx:temperature > 23 )
    sendTelegram("bot1", "Temp is changed")
end

stiil have a error:

2020-04-30 20:41:04.612 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'telegram.rules' has errors, therefore ignoring it: [3,5]: no viable alternative at input ':'
[3,31]: mismatched character 'c' expecting set null
[5,14]: mismatched input ':' expecting ')'
[5,24]: mismatched input ':' expecting 'end'
[5,29]: mismatched character 'c' expecting set null

Could you show me your item please??

cat /etc/openhab2/items/xiaomi.items 
// Xiaomi Temperature and Humidity Sensor
Number Xi_HT_Temperature <temperature> { channel="mihome:sensor_ht:158d0001bc1dc5:temperature" }
Number Xi_HT_Humidity <humidity> { channel="mihome:sensor_ht:158d0001bc1dc5:humidity" }
Number Xi_HT_Battery <battery> { channel="mihome:sensor_ht:158d0001bc1dc5:batteryLevel" }
Switch Xi_HT_BatteryLow <energy> { channel="mihome:sensor_ht:158d0001bc1dc5:lowBattery" }

You previous post had a first linie which is NOT suppose to be in the rule file…
(This line: cat /etc/openhab2/rules/telegram.rules )

This is how you rule should look like:

rule "Xiaomi temp sensor"
when
    Item Xi_HT_Temperature changed
then
   if (Xi_HT_Temperature > 23 )
    sendTelegram("bot1", "Temp is changed")
end

And only this…
Please look closely how I inserted your item…

line cat /etc/openhab2/rules/telegram.rules is only linux command, it is not in the script.

changed on this:

rule "Xiaomi temp sensor"
when
    Item Xi_HT_Temperature changed
then
   if (Xi_HT_Temperature > 23 )
    sendTelegram("bot1", "Temp is changed")
end

now in log:
2020-05-01 00:15:09.769 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘telegram.rules’
2020-05-01 00:16:01.700 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Xiaomi temp sensor’: Unknown variable or command ‘>’; line 5, column 8, length 22
2020-05-01 00:18:23.032 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Xiaomi temp sensor’: Unknown variable or command ‘>’; line 5, column 8, length 22

You are looking to compare the Item’s state and I added a code block so that you could add more lines…

rule "Xiaomi temp sensor"
when
    Item Xi_HT_Temperature changed
then
    if (Xi_HT_Temperature.state > 23) {
        sendTelegram("bot1", "Temp is changed")
    }
end

The > < operators can be a bit funny in rules, you may need to try

if ((Xi_HT_Temperature.state as Number) > 23 )

Big thanks to all script now look like this:

rule "Xiaomi temp sensor"
when
    Item Xi_HT_Temperature changed
then
   if ((Xi_HT_Temperature.state as Number) > 23 )
    sendTelegram("bot1", "Temp is changed")
end

and in log only:

2020-05-01 00:36:20.698 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'telegram.rules'

now it should work, I think.

And please tell me how to edit the script so that will sends notifications once an hour or only once ?

According to its doc… Like this:

I dont use the telegram action myself though, so I have no idea how it works. You probably have to set it up correctly. Read the doc. as linked.

1 Like

You’d want the rule to trigger once an hour, instead of every time the temperature changes.
The magic word is cron trigger

But if the temperature changes to > 23, you might have to wait 59 minutes until the next time the rule runs. Can you say what you want in more detail?

For example -
Want a message as soon as temp > 23
But
Don’t want another message for one hour, and then I want a reminder if temp is still >23.
And
Want a reminder every hour as long as still > 23

Sorry for the inaccuracy. I want a reminder every half of hour as long as temperature is still > 23.

All the parts for that job will be in here -

1 Like

ok, thank you