简体   繁体   English

Openhab将MQTT主题上的数据与字符串进行比较

[英]Openhab comparing data on MQTT topic to a string

I am trying to compare the value on an MQTT Topic. 我正在尝试比较MQTT主题上的值。 I have successfully be able to capture the value and output the value to the console via the logInfo() call. 我已经能够成功捕获值并将其通过logInfo()调用输出到控制台。

What I am trying to do is compare what is on the MQTT Topic to a value and then execute additional openhab commands based on the value on the topic. 我想要做的是将MQTT主题上的内容与一个值进行比较,然后根据该主题上的值执行其他openhab命令。

I have been able to get the value on the MQTT topic and convert it to a string using the .toString operation. 我已经能够获取MQTT主题上的值,并使用.toString操作将其转换为字符串。

The result of my code is that I am getting conversion errors on my console when the comparison is executed. 代码的结果是,执行比较时,控制台上出现转换错误。

在此处输入图片说明

You can see that the value is captured and output to the console "1023" 您可以看到该值已捕获并输出到控制台“ 1023”

My code is 我的代码是

import org.eclipse.xtext.xbase.lib.*
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.math.BigDecimal.*
import java.lang.Integer
import java.lang.Double
import java.lang.String
import java.lang.Number

var String feedingsensor_reading

rule "start processing feeding sensor"
when 
    Item feedingsensor changed 
then
     logInfo("Step", "***********") 
     logInfo("Step", "** Start **") 
     logInfo("Stap", "***********") 
     logInfo("Step", "** Step 1 **") 

    feedingsensor_reading = feedingsensor.state.toString

    logInfo("Step", feedingsensor_reading) 

    if (feedingsensor_reading == "0"){

       logInfo("DATA", "******* Do Nothing *****") 

    }else
    {
        ... other code to be executed

I had the item definition defined as a String and not Number 我将项目定义定义为字符串而不是数字

the correct definition is 正确的定义是

Number feedingsensor {mqtt="<[home:{topic}:state:default]"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM