Transform Exec python output

Hi,

i have a water-heater that i can interact with from a python script.

i have setup a thing as below

Thing exec:command:SwitcherAll [command="sudo python /etc/openhab2/scripts/Switcher-V2-Python/switcher.py 2", interval=60, timeout=5,autorun=false]

The full output is

2019-10-27 12:07:13.109 [vent.ItemStateChangedEvent] - Command_Output changed from NULL to =========================================================
                + Switcher V2 Python                                    +
                + Reverse Engineering and Coding By:                    +
                + Aviad Golan (@AviadGolan) and Shai Rod (@NightRang3r) +
                =========================================================
         
[*] Sending Login Packet to Switcher...
[+] Received SessionID: 283a814f
[*] Getting Switcher state...
[+] Device Name: Switcher Boiler 09E1
[+] Device is ON
[+] Electric Current is: 11.6(A)
[+] Power consumption is: 2556(W)
[+] Device is configured to auto shutdown in: 2:00 hour(s)
[*] Auto shutdown device in: 1:52:59
[+] Done!


2019-10-27 12:08:26.215 [vent.ItemStateChangedEvent] - mqtt_sensor_motion17 changed from ON to OFF

i want to retrieve:

  1. Device is “STATUS
  2. Electric Current is: 11.6(A)
  3. Power consumption is: 2556(W)

i have create 3 things as below

Thing exec:command:SwitcherDeviceStatus		[command="sudo python /etc/openhab2/scripts/Switcher-V2-Python/switcher.py 2", interval=60, timeout=5,autorun=false, transform="REGEX(.*Device is (.*) [+] Electric.*)"]
Thing exec:command:SwitcherDeviceElectric	[command="sudo python /etc/openhab2/scripts/Switcher-V2-Python/switcher.py 2", interval=60, timeout=5,autorun=false, transform="REGEX(.*Electric Current is: (.*)(A).*)"]
Thing exec:command:SwitcherDevicePower 		[command="sudo python /etc/openhab2/scripts/Switcher-V2-Python/switcher.py 2", interval=60, timeout=5,autorun=false, transform="REGEX(.*Power consumption is: (.*)(W).*)"]

Unfortunately the ouputs are empty.

I am missing something in my transform (which is well installed)

Thanks in advance

Your idea will run the script (and maybe its communications) three times.

If you want to analyze a single script output into three Items, you might want to do it in a rule (running the script once)

Right point

How can I extract sub text from a rule?

Trigger the rule from an update to your ‘source’ text Item, then apply your transforms in the rule to generate postUpdates for your target Items.
I know little about getting the regex right, though.

Thanks For your help

i am working only with only one Thing Exec and trying to manipulate it from the rule

i am close but i am retrieving

for Electric Current is: 11.5(A)=>5
for Power consumption is: 2535(W)=> 5
for Device is ON =>ON

is my transform regex correct ? ```

   SwitcherPowerConsumption = transform("REGEX", ".*(\\d).*", SwitcherPowerConsumption.toString)

you can find below the rule

rule "Command_Output "
when
   Item   Command_Output changed
then
val SwitcherStatus = 	Command_Output.state.toString.split('---').get(5)
val SwitcherElectric = 	Command_Output.state.toString.split('---').get(6)
val SwitcherPowerConsumption = 	Command_Output.state.toString.split('---').get(7)

        logInfo("SwitcherStatus", "---"+ SwitcherStatus)

        SwitcherStatus = transform("REGEX", "Device is (.*)", SwitcherStatus.toString)
        Switcher.postUpdate(SwitcherStatus)
        logInfo("SwitcherStatus", "---"+ SwitcherStatus +"---")


        logInfo("SwitcherElectric","---"+  SwitcherElectric+"---")
        SwitcherElectric = transform("REGEX",".*(\\d).*", SwitcherElectric.toString)
        NumberSwitcherElectric.postUpdate(SwitcherElectric)
        logInfo("SwitcherElectric","---"+  SwitcherElectric+"---")


        logInfo("SwitcherPowerConsumption", "---"+ SwitcherPowerConsumption)
        SwitcherPowerConsumption = transform("REGEX", ".*(\\d).*", SwitcherPowerConsumption.toString)
        logInfo("SwitcherPowerConsumption    ", "---"+ SwitcherPowerConsumption   +"---")
        NumberSwitcherPowerConsumption.postUpdate(SwitcherPowerConsumption)


end

Logs

   2019-10-28 12:23:36.130 [vent.ItemStateChangedEvent] - Command_Output changed from --- Sending Login Packet to Switcher...

--- Received SessionID: 1097824f
--- Getting Switcher state...
--- Device Name: Switcher Boiler 09E1
--- Device is OFF
--- Electric Current is: 0.0(A)
--- Power consumption is: 0(W)
--- Device is configured to auto shutdown in: 2:00 hour(s)
--- Done! to 


--- Sending Login Packet to Switcher...
--- Received SessionID: df97824f
--- Getting Switcher state...
--- Device Name: Switcher Boiler 09E1
--- Device is ON
--- Electric Current is: 11.5(A)
--- Power consumption is: 2535(W)
--- Device is configured to auto shutdown in: 2:00 hour(s)
--- Auto shutdown device in: 1:59:25
--- Done!

==> /var/log/openhab2/openhab.log <==
2019-10-28 12:23:36.197 [INFO ] [marthome.model.script.SwitcherStatus] - --- Device is ON
2019-10-28 12:23:36.215 [INFO ] [marthome.model.script.SwitcherStatus] - ---ON---
2019-10-28 12:23:36.223 [INFO ] [rthome.model.script.SwitcherElectric] - --- Electric Current is: 11.5(A)
---==> /var/log/openhab2/events.log <==
2019-10-28 12:23:36.243 [vent.ItemStateChangedEvent] - NumberSwitcherElectric changed from 0 to 5
==> /var/log/openhab2/openhab.log <==
2019-10-28 12:23:36.245 [INFO ] [rthome.model.script.SwitcherElectric] - ---5---
2019-10-28 12:23:36.252 [INFO ] [odel.script.SwitcherPowerConsumption] - --- Power consumption is: 2535(W)
2019-10-28 12:23:36.262 [INFO ] [.script.SwitcherPowerConsumption    ] - ---5---
==> /var/log/openhab2/events.log <==
2019-10-28 12:23:36.275 [vent.ItemStateChangedEvent] - NumberSwitcherPowerConsumption changed from 0 to 5

try to replace the Current regex from".*(\\d).*" to ".*(\\d{1,2}(.)\\d).*"
and the Power to ".*(\\d{1,4}).*"

BTW, where can get the py script?
(edit) found it: https://github.com/NightRang3r/Switcher-V2-Python
I wish there was something similar to this in OH:https://www.youtube.com/watch?v=_ZENb42xPXk

There is a non-regex problem in your rule, declaring val and later trying to modify it. Use var

I have modified the rule to correctly get the values but still unable to parse the regex correctly to get the values out. Could anyone please suggest how to resolve it?

Rule:

rule "Switcher Output"
when
Item   Switcher_Command_Output changed
then
val SwitcherStatus = Switcher_Command_Output.state.toString.split('[+]').get(9)
val SwitcherCurrent = Switcher_Command_Output.state.toString.split('[+]').get(10)
val SwitcherPowerConsumption = Switcher_Command_Output.state.toString.split('[+]').get(11)

    logInfo("SwitcherStatus", "---"+ SwitcherStatus)
    var SwitcherStatusVar = transform("REGEX", "Device is (.*)", SwitcherStatus.toString)
    SwitchSwitcherStatus.postUpdate(SwitcherStatusVar)


    logInfo("SwitcherCurrent","---"+  SwitcherCurrent+"---")
    var SwitcherCurrentVar = transform("REGEX",".*(\\d(.)\\d).*", SwitcherCurrent.toString)
    NumberSwitcherCurrent.postUpdate(SwitcherCurrentVar)


    logInfo("SwitcherPowerConsumption", "---"+ SwitcherPowerConsumption)
    var SwitcherPowerConsumptionVar = transform("REGEX", ".*(\\d).*", SwitcherPowerConsumption.toString)
    NumberSwitcherPowerConsumption.postUpdate(SwitcherPowerConsumptionVar)
end

Log:

[+] Done! to =========================================================

	   	+ Switcher V2 Python                                    +

	 	+ Reverse Engineering and Coding By:                    +

	 	+ Aviad Golan (@AviadGolan) and Shai Rod (@NightRang3r) +

	 	=========================================================

	 

[*] Sending Login Packet to Switcher...

[+] Received SessionID: 00000000

[*] Getting Switcher state...

[+] Device Name: Switcher Touch

[+] Device is ON

[+] Electric Current is: 11.1(A)

[+] Power consumption is: 2446(W)

[+] Device is configured to auto shutdown in: 2:59 hour(s)

[*] Auto shutdown device in: 2:58:47

[+] Done!

2019-10-29 01:15:48.280 [vent.ItemStateChangedEvent] - System_CPU_Uptime changed from 11520.1 to 11522.1

==> /var/log/openhab2/openhab.log <==

2019-10-29 01:15:48.805 [INFO ] [marthome.model.script.SwitcherStatus] - ---] Device is ON

[

==> /var/log/openhab2/events.log <==

2019-10-29 01:15:48.817 [vent.ItemStateChangedEvent] - SwitchSwitcherStatus changed from NULL to 

==> /var/log/openhab2/openhab.log <==

2019-10-29 01:15:48.820 [INFO ] [arthome.model.script.SwitcherCurrent] - ---] Electric Current is: 11.1(A)

[---

==> /var/log/openhab2/events.log <==

2019-10-29 01:15:48.831 [vent.ItemStateChangedEvent] - NumberSwitcherCurrent changed from 0.0 to 1.1

==> /var/log/openhab2/openhab.log <==

2019-10-29 01:15:48.836 [INFO ] [odel.script.SwitcherPowerConsumption] - ---] Power consumption is: 2446(W)

[

==> /var/log/openhab2/events.log <==

2019-10-29 01:15:48.849 [vent.ItemStateChangedEvent] - NumberSwitcherPowerConsumption changed from 0 to 6

2019-10-29 01:21:04.328 [vent.ItemStateChangedEvent] - Switcher_Command_Output changed from =========================================================

   	+ Switcher V2 Python                                    +

 	+ Reverse Engineering and Coding By:                    +

 	+ Aviad Golan (@AviadGolan) and Shai Rod (@NightRang3r) +

 	=========================================================

[*] Sending Login Packet to Switcher…

[+] Received SessionID: 00000000

[*] Getting Switcher state…

[+] Device Name: Switcher Touch

[+] Device is ON

[+] Electric Current is: 11.0(A)

[+] Power consumption is: 2417(W)

[+] Device is configured to auto shutdown in: 2:59 hour(s)

[*] Auto shutdown device in: 2:54:34

[+] Done! to =========================================================

   	+ Switcher V2 Python                                    +

 	+ Reverse Engineering and Coding By:                    +

 	+ Aviad Golan (@AviadGolan) and Shai Rod (@NightRang3r) +

 	=========================================================

[*] Sending Login Packet to Switcher…

[+] Received SessionID: 00000000

[*] Getting Switcher state…

[+] Device Name: Switcher Touch

[+] Device is ON

[+] Electric Current is: 11.2(A)

[+] Power consumption is: 2463(W)

[+] Device is configured to auto shutdown in: 2:59 hour(s)

[*] Auto shutdown device in: 2:53:31

[+] Done!

==> /var/log/openhab2/openhab.log <==

2019-10-29 01:21:04.349 [INFO ] [marthome.model.script.SwitcherStatus] - —] Device is ON

[

2019-10-29 01:21:04.366 [INFO ] [arthome.model.script.SwitcherCurrent] - —] Electric Current is: 11.2(A)

[—

2019-10-29 01:21:04.380 [INFO ] [odel.script.SwitcherPowerConsumption] - —] Power consumption is: 2463(W)

[

==> /var/log/openhab2/events.log <==

2019-10-29 01:21:04.385 [vent.ItemStateChangedEvent] - NumberSwitcherCurrent changed from 1.0 to 1.2

2019-10-29 01:21:04.398 [vent.ItemStateChangedEvent] - NumberSwitcherPowerConsumption changed from 7 to 3

In OH, the regular expression must match the entire string.

".*Device is (.*)\n.*"

Hi Rich,
My experience in software is lame and I am learning as I go here.
Could you please show me how to format the rule correctly in order to parse the 3 strings correctly?

Thank you.

i succeed as below

i have updated the script to get the output with this prefix
"— "for the split

the python script updated can be downloaded from https://paste.ofcode.org/WuUY8i8ZTQrtugs2wV7nxL

rule "Command_Output "
when
   Item   Command_Output changed 
then 
	val SwitcherStatus = 	Command_Output.state.toString.split('--- ').get(5).trim
	val SwitcherElectric = 	Command_Output.state.toString.split('--- ').get(6).trim
	val SwitcherPowerConsumption = 	Command_Output.state.toString.split('--- ').get(7).trim

	logInfo("SwitcherStatus", "---"+ SwitcherStatus +"---")
	
	SwitcherStatus = transform("REGEX", "Device is (.*)", SwitcherStatus.toString)
	Switcher.postUpdate(SwitcherStatus)
	logInfo("SwitcherStatus", "---"+ SwitcherStatus +"---")

	logInfo("SwitcherElectric","---"+  SwitcherElectric+"---")
	SwitcherElectric = transform("REGEX","Electric Current is: (.*)A", SwitcherElectric.toString)
	logInfo("SwitcherElectric","---"+  SwitcherElectric+"---")
	NumberSwitcherElectric.postUpdate(SwitcherElectric)


	logInfo("SwitcherPowerConsumption", "---"+ SwitcherPowerConsumption+  "---")	
	SwitcherPowerConsumption = transform("REGEX", "Power consumption is: (.*)W", SwitcherPowerConsumption.toString)
	logInfo("SwitcherPowerConsumption    ", "---"+ SwitcherPowerConsumption   +"---")
	NumberSwitcherPowerConsumption.postUpdate(SwitcherPowerConsumption)


end

Update your regular expressions to match against the entire String returned by the script, as I demonstrated in my previous reply.

Thanks it works