I try to display a battery voltage that I get form mqtt (panAdc). This works. I try to display this value on PanVolt. It won’t work. I have to divide the source value panAdc by 10. I try to do it with a rule:
rule "voltadc"
when
Item solAdc received update
then
// var DecimalType volt = solAdc.state as NumberItem
// volt = volt / 10
if (solAdc.state instanceof DecimalType) {
volt = solAdc.state as DecimalType / 10
postUpdate( solVolt, volt )
}
else
postUpdate( solVolt, "error" )
end
Resuming: I feed the panAdc item of type Number from a linked mqtt. Then I want to display this value divided by 10 on the String panVolt item. Currently I have a lot of code but that is because I’m learning and here Winter is starting …
Some background: On my PI I have mosquitto installed and linked to cloudMQTT. cloudMQTT is feed by an older ESP8266 project, that I don’t want to modify. The old ESP8266 home automation is working well since 3 years and I need it as a backup system. If openHAB fails, then old ESP8266 works. If old ESP8266 fails then very old GSM from Conrad Electronics still works. A lot of backup because I’m very off-grid.
I’m new here and I try to learn on a very slow internet, so please some help and examples. I’m using VS Code and OpenHAB2 on a Raspberry PI. I come from the C++ world, however I find it very hard to get into openHAB, however I like this concept because it is accesible for many.
Thank you Rich, now it is working. I defined all Items as numbers. The object style is much nicer.
Group gESP8266
String panStat "Status [%s %%]" <battery> (gESP8266) { mqtt="<[broker:/gosouth/quilaco/power/status:state:JSONPATH($.status)]" }
Number panAdc "ADC [%d]" <solarplant> (gESP8266) { mqtt="<[broker:/gosouth/quilaco/volt/status:state:JSONPATH($.status)]" }
Number panVolt "Volt [%.2f V]" <solarplant> (gESP8266) // from rule "voltadc"
my Rule:
import org.openhab.core.library.types.DecimalType
rule "voltadc"
when
Item panAdc received update
then
// var DecimalType volt
if( panAdc.state instanceof DecimalType ) {
volt = panAdc.state as DecimalType
volt = volt / 10
panVolt.postUpdate( volt ) // volt.toString
}
end
I still have a few questions:
Do I need to check: if (solAdc.state instanceof DecimalType) ??
I noticed tat the panVolt value is not always there, like if Item panAdc received update is not triggered. Is there a better way?
The import org.openhab.core.library.types.DecimalType is neccesary?
Tomorrow I will try to use the debug log messages…
1 it is a good idea actually. When oh first comes up or oh reloads the items the state of your items could be NULL (not to be confused with null) which is a special type of state.
You could make the intent of the if statement of you changed it to
if(solAdc.state != NULL)
which would be functionally equivalent.
As a side note, there are some edge cases where it is better to cast the state to Number instead of DecimalType.
See comment above about NULL.
Not in OH 2.x all of those classes are included by default and even if they were not they were moved. There is nothing at org.openhab anymore.