Send the value from a slider to a script .py

Hello the OH community,
Situation : I have a slider which will control a power generator.
Issue : I need to send the output value from the Slider in the sitemap, to a file .py in order to reuse it as an argument. (Then with the power value in the .py I have a code for controlling my generator)
First of all, is it possible ? Then, how do I make it ? :sweat:
As I’m a very biginner, I may missed somethings… be kind :sweat_smile:
Many thanks

PS : I’m using OH2

Do you want to do this in a rule or with the exec binding?

I would approach it in a rule. (a quick search of the forum is never pointless when starting out)

Something like this

rule "item value to command line"
when
Item WhateverYouLike changed  // Item name for your Slider in this case
then
execCommand("AbsolutePathToYourScript and switches " + WhateverYouLike.state) // this will run your script, add whatever switches your script needs and append the State of the Item. You could structure complex command lines this way
end

Setup scripted automation and Jython, then you can read use your existing code in a script or module, and directly read the value of the slider or trigger a rule when it’s state changes.

Ok thanks, I’m trying this, but WhateverYouLike.state will give me a String so I should convert it into a float ?
like this

rule "item value to command line"
when
Item WhateverYouLike changed 
then
var value=(WhateverYouLike.state as DecimalType).floatValue
executeCommandLine("AbsolutePathToYourScript and switches" + value) 
end

I think that very much depends on what your script will accept.

I’m not a coder / programmer, so I can only offer a ‘best guess’.
Up to now, I’ve only ever passed the state of an item as a String to a script.

Give it a try and see what happens :smile:

I tried but not working. I cheked the logs and my Item well received the command with the good value then executed the commadLine but my file.py is still empty :cry: and the value has not been written inside
This means my commandLine is maybe wrong
I did

executeCommandLine("sudo python AbsolutePath/file.py" + value) 

It rather depends on what type of Item you have selected.
Most people use sitemap sliders with a Number or Dimmer type Item, which is going to have a numeric state.
But strictly it is a state object, and often you’d do something like
(WhateverYouLike.state as Number)
in an actual rule.

On the other hand,
executeCommandLine()
requires you to give a string parameter.
You’d probably want
executeCommandLine("sudo python AbsolutePath/file.py " + value.toString)
and also note I’ve added a space in there.

1 Like

Thanks !! I will later try to convert my rule to Jython to see what it comes ! :grin:

1 Like

Thanks, I did this and worked well

My real probleme now is how to write my value into a file.py through the executeCommandLine

executeCommandLine("sudo python AbsolutePath/file.py" + value) 

even whith this my python file is still empty

executeCommandLine() wants you to give it a string. I think the rule engine is smart enough to work it when you start with a string and the + something to it, and will automatically convert your value to string version.
But strictly, you should do that yourself.

Most important - I expect you need a space like
myscript.py 3.1417 ?
It’s not going to guess that for you, you have to ask.
So -

executeCommandLine("sudo python AbsolutePath/file.py " + value.toString)

Top tip - there are all sorts of permissions problems and so on that you can run into with exec. Often it will tell you what the problem is, but you have to listen.

var myResults = executeCommandLine("sudo python AbsolutePath/file.py " + value.toString)
logInfo("test", "My exec results " + myResults)
1 Like

Does the path to Python have to be absolute too?

I completely forgot to add the space in my example, thanks for stepping in and turning my feeble attempt into something useful.

Don’t ask me, I’ve got no idea!

1 Like

Just “the blind leading the bewildered” again then.

I only mention it because I saw it as a suggestion in another thread

executeCommandLine("/bin/echo something >> /tmp/temp.txt",5000)

Rather than

executeCommandLine("echo something >> /tmp/temp.txt",5000)

Ok thanks, I did it and here is what the log shows


So it seems erverything is ok but my ReadValue.py is still empty
I tried different command line but nothing is working

At least you’re progressing.

So give us an example of a working command line…

It’s probably something silly like spaces / capitals / file permissions / path

HoldOnAMo (a distant and reluctant relative to Geronimo)

Why are you trying to use Nano (an interactive text editor) to put the value permanently into the python script?

Surely the script just needs to see your value as a variable, supplied as part of the command line?

Just as @rossko57 has suggested, call Python to run your script, with your value being passed to your script from the command line.

2 Likes

Stuart @MDAR reminds me - we really, really ought to use a timeout with this, or it won’t wait for the script to work (or mess up)

var myResults = executeCommandLine("sudo python AbsolutePath/file.py " + value.toString, 5000)
logInfo("test", "My exec results " + myResults)
1 Like

nano was just a test :sweat_smile: I already tried call python to run my script from the command line and didn’t worked neither

I just tried with a timeout of 5000 and still not working

Arrr I see.

So I’ve just tried something super simple like this.

Try this bash script to echo a variable and append a text file, saved as /opt/echoTest.sh

echo Value received from command line is $1 \\n
new=`expr $1 \* 66`
echo After some maths it becomes $new \\n
echo $1 $new >> /opt/test.txt
exit 0

The command line of

sh /opt/echoTest.sh 2

Puts 2 132 into test.txt


@silv
So if you put this into your rule… What happens?

var myResults = executeCommandLine("sh /opt/echoTest.sh " + value.toString, 5000)
logInfo("test", "My exec results " + myResults)

A bash script to play with

 #!/bin/bash
echo Value received from command line is $1 \\n
date > /opt/test.txt
echo inital value is $1 >> /opt/test.txt
new=`expr $1 \* $1`
echo $1 multipled by $1 is $new >> /opt/test.txt
echo $1 multipled by $1 is $new 
exit 0