How do I have item in .items file with meta data multiple choices on OH3?

Hello,

I spent too long with this googling and trying different things.
Now It’s better to ask the community.

I have a serial binding and a serial thing:

Bridge serial:serialBridge:ttyUSB1 [serialPort="/dev/ttyUSB1", baudRate=9600] {
    Thing serialDevice Viewsonic_1_PX747 "Viewsonic_1_PX747"
    {
        Channels:
            Type switch : power [onValue="ON(\r*pow=on#\r),OFF(\r*pow=off#\r)"]
            Type string : control [stateTransformation="REGEX(.*[A-Z]=(.*?)#)"]
    }
}

I use .items files.
How do I make the item switch send the command [onValue=“ON(\rpow=on#\r),OFF(\rpow=off#\r)”] according to the .thing file?

How do I make an item shown in openhab 3 with metadata so I can send multiple choices according to below?

In items file:

Switch Viewsonic_1_PX747_Switch "ProjectorSwitch" (gViewsonic_1_PX747) ["Switch"] { channel="serial:serialDevice:Viewsonic_1_PX747:power" }
String Viewsonic_1_PX747_Control "Projector Control" (gViewsonic_1_PX747) ["Switch"] { channel="serial:serialDevice:Viewsonic_1_PX747:control", stateDescription =" " [ options = "\r*pow=?#\r" =" ViewsonicPowerStatus", "\r*ltim=?#\r" = "ViewsonicLampHours" ] }

Basically:
I want to start my projector with my switch. It should send for ON: \rpow=on#\r and for OFF: \rpow=off#\r)

I also want to query the projector how many hours its been on and what the power status is (on or off).
So sending a command \r*ltim=?#\r asks the projector and it responds back.

In OH2.5 I had this in my sitemap:

Selection item=viewsonic1projectorstring label="Proj" mappings=[
												"\r*pow=?#\r"="ViewsonicPowerStatus",
												"\r*ltim=?#\r"="ViewsonicLampHours"]

With these items:

String viewsonic1projectorstring                "Proj [%s]" (gProj)    { serial="/dev/ttyUSB1@9600,REGEX(.*[A-Z]=(.*?)#)" }
Switch viewsonic1projector        "Relay Q1"          (Entrance)      { serial="/dev/ttyUSB1@9600,ON(\r*pow=on#\r),OFF(\r*pow=off#\r)" }

Thanks.

Change

To

[onValue="\r*pow=on#\r", offValue="\r*pow=off#\r"]

in your Thing Channel definition.

1 Like

mr @hafniumzinc to save the day :slight_smile:

I checked and saw that I had some other errors as well. All are corrected now so “power on and power off” works.

Now to get lamp hours and power state I think maybe it’s wiser to have 2 items (one each).

.things

Bridge serial:serialBridge:sensors [serialPort="/dev/ttyUSB1", baudRate=9600] {
    Thing serialDevice relay [patternMatch =".*"]
    {
        Channels:
            Type switch : power [onValue ="\r*pow=on#\r", offValue ="\r*pow=off#\r"]
            Type string : lampstatus [stateTransformation="REGEX(.*[A-Z]=(.*?)#)"]
            Type string : powerstatus [stateTransformation="REGEX(.*[A-Z]=(.*?)#)"]
    }
}

.items:

Group gViewsonic_1_PX747 "gViewsonic_1_PX747"  (gCinema) ["Projector"]
Switch Viewsonic_1_PX747_Switch "Projector Power Switch" (gViewsonic_1_PX747) ["Switch"] { channel="serial:serialDevice:sensors:relay:power" }
String Viewsonic_1_PX747_LampStatus "Projector lamp status" (gViewsonic_1_PX747) ["Measurement"] { channel="serial:serialDevice:sensors:relay:lampstatus" }
String Viewsonic_1_PX747_PowerStatus  "Projector power status" (gViewsonic_1_PX747) ["Power"] { channel="serial:serialDevice:sensors:relay:powerstatus" }

Maybe I need to create a virtual switch with a rule that asks the projector each command. Or is it possible to do this from the item in .items file?

It took some time but I finally solved it…

For future reference:
I created two item switches

Group gViewsonic_1_PX747 "gViewsonic_1_PX747"  (gCinema) ["Projector"]
Switch Viewsonic_1_PX747_Switch "Projector Power Switch" (gViewsonic_1_PX747) ["Switch"] { channel="serial:serialDevice:sensors:relay:power" }
String Viewsonic_1_PX747_LampStatus "Projector Lamp status" (gViewsonic_1_PX747) { channel="serial:serialDevice:sensors:relay:lampstatus" }
Switch Viewsonic_1_PX747_Lamp "Projector Lamp Status Switch" (gViewsonic_1_PX747) ["Switch"] 
String Viewsonic_1_PX747_PowerStatus "Projector Power status" (gViewsonic_1_PX747) { channel="serial:serialDevice:sensors:relay:powerstatus" }
Switch Viewsonic_1_PX747_Power "Projector power Status Switch" (gViewsonic_1_PX747) ["Switch"]

The serial .thing:

Bridge serial:serialBridge:sensors [serialPort="/dev/ttyUSB1", baudRate=9600] {
    Thing serialDevice relay [patternMatch =".*"]
    {
        Channels:
            Type switch : power [onValue ="\r*pow=on#\r", offValue ="\r*pow=off#\r"]
            Type string : lampstatus [stateTransformation ="REGEX:.*?LTIM=(.*?)#"]
            Type string : powerstatus [stateTransformation ="REGEX:.*?POW=(.*?)#"]
    }
}

And a rule that send the commands I wanted. I know there is a difference in itemname.sendCommand(“string”) and sendCommand(itemname, “string”).
But only the latter worked…
Also made the rule to fire every night at oclock: 23.59.59 so that lamphours are logged in databases so I can graph usage.

rule:

rule "Projector power Status"
when
    Item Viewsonic_1_PX747_Power received command
then
    sendCommand(Viewsonic_1_PX747_PowerStatus, "\r*pow=?#\r")
    Viewsonic_1_PX747_Power.postUpdate(OFF)
end

rule "Projector Lamp Status"
when
	Time cron "0,59 0,59 0,23 ? * * *" or
    Item Viewsonic_1_PX747_Lamp received command ON
then
    sendCommand(Viewsonic_1_PX747_LampStatus, "\r*ltim=?#\r")
    Viewsonic_1_PX747_Lamp.postUpdate(OFF)
end

This only took me around 8 hours today to figure out… :rofl:

1 Like