Exec binding - Exit result update switch On/Off status - Netcat

Im trying to get a Channel from the Exec binding to change the status of an On/Off switch to update and reflect the current state of the device. As someone who is quite new to all of this, I have a couple of questions I cannot seem to find the answer to, after 3 hours reading the documentation and hunting the forums on this topic.

Question 1
My Exec binding Channel uses Netcat, which returns the result 0 for on and 1 for off. The below works and shows an ON button and and OFF button on my site. However, to the left of the buttons, it displays a 0 or a 1.

ITEMS
Number PLAYSTATION “Playstation 4” { channel=“exec:command:daa779bc:exit” }

SITEMAP
Switch item=PLAYSTATION mappings=[“0”=“On”,“1”=“Off”]

Is there are simple way, with the above to stop it displaying that 0 or 1 next to the buttons (as below)?

Image1

P.S. If I change the item to a switch or string… I either get an “Err” msg on the sitemap webpage next to the button or a “-” dash and the buttons dont update.

Question 2
The next thing I have have tried is to use a rule to update my switch on the main site, but I am struggling to find a working example that will read/convert my 0 or 1, to postUpdate to my PLAYSTATION switch as on or off (or 0 or 1).

Ive tried multiple variations within a rule to notice the change, but none seem to work e.g.

when
	Thing "exec:command:daa779bc:exit" 

also

when
	Item PlaystationExecResult 

In the above, Ive tried “received update”, “changed”, “changed from 1 to 0” etc etc etc

then
	PLAYSTATION.postUpdate(ON)

In the then section Ive tried a simple postUpdate (I changed my items file PLAYSTATION to a Switch and changed the siteitems PLAYSTATION a On and OFF mapping). Ive also tried If/Else.

Ive looked at pulling the exec:command:daa779bc:exit into a Val to convert it, but I cant find examples of how to change 0/1 to On/Off.

So my question is, if theres not a simple way to remove the 0/1 as in my first question, is there a rule I can use to update my switch? Something like:

Rule “PS-ONOFF”
when
Thing “exec:command:daa779bc:exit” received update
then
if(“exec:command:daa779bc:exit.state.substring”(0,1) == “0”)
{PLAYSTATION.postUpdate(ON)}
else
{PLAYSTATION.postUpdate(OFF)}
end

Thanks

Is there a reason you are not using the Network binding?

This looks like a bug. You don’t have the label configured to print the state so it should not be showing up on the right.

The “-” means the Item is uninitialized. The docs clearly state that a Number Item is the only Item type supported for the exit channel so that is likely why you cannot use Switch or String. The Error message when you use a Switch is because “ON” and “OFF” are the only valid Strings one can use to update a Switch.

Switch PLAYSTATION "Playstation 4"
Number PlaystationExecResult { channel="exec:command:daa779bc:exit" }
rule "Update Playstation state"
when
    Item PlayStationExitResult changed
then
    PLAYSTATION.postUpdate(if(PlayStationExitResult.state == 0) ON else OFF)
end
1 Like

Thanks for that… Works a treat!

I did do something similar somewhere to that, but I must have messed up somewhere! I think Ive got it all straight in my head now. You may want to edit your post for future people as theres a difference on the PlayStationExit(Exec)Result (My fault for using complex names in the first post!).

Really appreciated though! :slight_smile:

BTW… Yes Ive tried the network binding… and for whatever reason, it never sees the change of state. Perhaps its something to do with the fact the playstation is in low power mode and its more a case of connecting to the service behind the tcp port… as the port is still open(?) when the playstation is in standby, but there is no reply from the daemon on that port. So the Network Binding doesnt seem to test it in the way needed.

Can you share how you did the exec command?
I cannot seem to get that working, and with the network binding, it’s always on, even in standby mode.

Heres my full setup…

Make sure youve installed Netcat (the NC command line… youll need it later).

Items

Switch 	PLAYSTATION 	"Playstation 4"		<playstation4>
Number 	PSResult 	{ channel="exec:command:PSON:exit" }

Sitemap

Switch	item=PLAYSTATION	mappings=["ON"="On","OFF"="Off"]

Rules

// Update switch status to On/Off if device manually switched on

rule "Update Playstation state"
when
    Item PSResult changed
then
	if (PSResult.state == 0) PLAYSTATION.postUpdate("ON") else PLAYSTATION.postUpdate("OFF")
end

// PLAYSTATION - On/Off

rule "PlaystationOn"
	when 
		Item PLAYSTATION received command ON
	then
		executeCommandLine=("ps4-waker")
		Thread::sleep(2000)
	end

rule "PlaystationOff"
	when 
		Item PLAYSTATION received command OFF
	then
		executeCommandLine=("ps4-waker standby")
		Thread::sleep(2000)		
	end

In the Openhab PaperUI interface, go to Configuration > Things, click on the + symbol and add an Exec binding. In there, you simply need to add the netcat command with your IP address and the port 9295. So in the below command line, you would replace the 192.168.1.101 with the STATIC (it must be a static IP or you must have a fixed hostname set), with your Playstations IP address.

EDIT - My command, whilst it says “Command” as the name, is referenced by the name “PSON”… I cannot recall if you need to change the name from “Command” to “PSON” when creating the binding… and its just the case that in my screengrab, for some reason its showing “Command” as the name, rather than “PSON”.

nc -v -z -w 1 192.168.1.101 9295

I also set mine to run the check every 15 seconds…

Other than that… Im assuming youve run the PS4Waker from the command line already and got it working (this HAS to be done to make PS4Waker work… assuming you are using PS4Waker).

That, I believe is all you need!!

Thanks, im trying this for a PS3, but was having trouble with the netcat command.
In the meantime i figured it out on cmd line, i am now using:

nc -v -n -z -w 1 192.168.1.101 5223 2>&1 | grep refused | grep -v grep | wc -l

which returns 1 if its on, 0 when its off.

Yeah, so in the rules bit, you may need to swap the 0 to a 1, so if 1 shows, it will say its ON and otherwise change to OFF… Im sure you get the idea.

if (PSResult.state == 0) PLAYSTATION.postUpdate("ON") else PLAYSTATION.postUpdate("OFF")

Yes, thank you very much :smiley:

No probs!! Good luck getting it sorted!

For those who want to know, this didn’t work in an exec binding, it keeps nagging about Invalid port specification.
So what i did was almost the same as @ewrw

Create an new Thing -> Exec Binding -> Command.
Name:Check if PS3 is on
Thing id: PSON
Command: nc -v -n -z -w1 192.168.1.7 5223

Items:

Switch PLAYSTATION "Playstation 3" 
String PSResult { channel="exec:command:PSON:output" } 

Sitemap:

Switch item=PLAYSTATION label="Playstation 3"

Rules:

rule "Update Playstation state"
when
    Item PSResult changed
then
	if (PSResult.state.toString.contains ("refused")) PLAYSTATION.postUpdate("ON") else PLAYSTATION.postUpdate("OFF")
end

What this does is that the PS3 doesn’t have a Remote play port, so it will either give back a connection timed out when the PS3 is off, and a connection refused when the PS3 is on.

Guys…

This can be done “Switchale” to use with siri / alexa?

EDIT: The TAG “Switcheable” works!

Would it not be possible with Regex to get OFF if failed and else ON?

good night, i know it is horrible to relive a 2018 topic but i am a layman in this part, you would have some tutorial for a layman with all the setup or youtube video teaching how to do.

I think you will need to narrow this question a bit, to get useful advice.