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 ?
As I’m a very biginner, I may missed somethings… be kind
Many thanks
PS : I’m using OH2
MDAR
(Stuart Hanlon, UK importer of Velbus hardware)
2
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
MDAR
(Stuart Hanlon, UK importer of Velbus hardware)
5
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.
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 and the value has not been written inside
This means my commandLine is maybe wrong
I did
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.
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 -
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
MDAR
(Stuart Hanlon, UK importer of Velbus hardware)
11
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.
I just tried with a timeout of 5000 and still not working
MDAR
(Stuart Hanlon, UK importer of Velbus hardware)
20
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