My first Rule, converting text to text

I have a String item which I would like to convert to a text virtual item. Mainly to have something human readable in basic UI

I’m starting off with something like :-

rule "mode changes"
when
Item Mode received update
then 
    if (Mode.state == 0) {
        var x = "Off"
        ModeReadable.postUpdate(x)
        }
end

but ModeReadable never updates. My first though is that .state or == don’t work on strings.
Any suggestions?

Hi @vworp,

I would try something like

logInfo("mode changes", "Mode.state = " + Mode.state)

to find out, what value is set. So you can see something in the log.

Then I would setup a second logInfo inside the if-clause to get a hint if I it gets into it.

Bye
HFM

rule "mode changes"
when
Item Mode received update
then 
logInfo("mode changes", "Mode.state = " + Mode.state)
    if (Mode.state == "0") {
        logInfo("mode changes", "Mode.state inside loop= " + Mode.state)
        var x = "Off"
        ModeReadable.postUpdate(x)
        }
end

Changing Mode.state == 0 to == “0” seems to work, I now get both logInfo passed through to the log and I’m also seeing “ModeReadable changed from NULL to Off”.
What i aren’t seeing is any result in my sitemap. Text item=ModeReadable is blank where I’m expecting it to be “Off”

Hi @vworp,

cool, you have progress! :slight_smile:

Did you define the item ModeReadable?
What type is ModeReadable?

Bye
HFM

Yep. Defined as String

You might also want to check out map transformation. It’s really useful for displaying string values in friendly text in Basic UI if your string item has a defined set of values. You just define a .map file in the /transform folder, and then reference it in the label for your string item. So there’s no need for the virtual item.

Thanks for that. Map transformations look to be a better fit in this case. Had to add Map Transformation in the addons through Paper UI, but it was all pretty straightforward after that.

Still curious as to why string updated item wouldn’t display on my sitemap

so am I
post your sitemap file?

Did you ask for it? i.e. a [%s] in your label

[%s] it was :slight_smile:

1 Like