Button battery state not reading as number in rules

I’m trying to make a rule that will send an E-Mail according to the battery state.
When I try to compare the battery state to the value I want I get the following error:

[ntime.internal.engine.RuleEngineImpl] - Rule 'sent test email': Unknown variable or command '>'; line 6, column 9, length 8

If I try to set the battery state as as Number I get the following error:

[ntime.internal.engine.RuleEngineImpl] - Rule 'sent test email': Could not cast mqtt_topic_e4df1f67_battery (Type=NumberItem, State=100, Label=Battery, Category=null) to java.lang.Number; line 6, column 9, length 13

Here is my code:

rule "sent test email"
when
    Item mqtt_topic_e4df1f67_battery received update
then
    var bat = mqtt_topic_e4df1f67_battery as Number
    if (bat > 90) {
    val mailActions = getActions("mail","mail:smtp:81520fb9")
    mailActions.sendHtmlMail("shaulliv@gmail.com", "Batt status", "batt status > 90%")}
end

And In Visual studio code I get the value of mqtt_topic_e4df1f67_battery as value 100 (which is correct):
image

Any help Would be appreciated.

You should try to access the state of the item and not the item itself.

mqtt_topic_e4df1f67_battery is an item and this items has a state. The state is accessed by mqtt_topic_e4df1f67_battery.state

If you change your code to:

rule "sent test email"
when
    Item mqtt_topic_e4df1f67_battery received update
then
    var bat = mqtt_topic_e4df1f67_battery.state
    if (bat > 90) {
    val mailActions = getActions("mail","mail:smtp:81520fb9")
    mailActions.sendHtmlMail("shaulliv@gmail.com", "Batt status", "batt status > 90%")}
end
1 Like

Just tried it and it worked.
Thank you.

1 Like