如何编写一个增加数字的openHAB规则?

时间:2015-03-12 12:33:39

标签: openhab

我想在openHAB2中编写一个规则,该规则会递增所有组项的计数器。 项目:

Group counters
Number cnt1 (counters)
Number cnt2 (counters)

我尝试了一条规则:

rule "Increase value .1 per minute"
when 
    Time cron "0 * * * * ?" or
    System started
then
    // Initialize. Necessary?
    counters?.members.forEach(counter|
        postUpdate(counter, 0.0)
    )
    counters?.members.forEach(counter|
        postUpdate(counter, 0.1 + counter.state)
    }
end

但这不起作用。例外: Error during the execution of startup rule 'Increase value .1 per minute': Could not invoke method: org.eclipse.xtext.xbase.lib.DoubleExtensions.operator_plus(double,byte) on instance: null

我尝试探索counter.state的类型,并使用logInfo(counter.state.class)正确记录...DecimalType

1 个答案:

答案 0 :(得分:2)

似乎隐式类型转换似乎不起作用。如果您将规则更改为:

rule "Increase value .1 per minute"
when 
    Time cron "0 * * * * ?" or
    System started
then
    // Initialize. Necessary?
    counters?.members.forEach(counter|
        if (counter.state == null) {
            postUpdate(counter, 0.0)
        }
    )
    counters?.members.forEach(counter|
        postUpdate(counter, 0.1 + (counter.state as DecimalType))
    }
end

它应该有用。希望这会有所帮助,

Thomas E.-E。