Executing python script from openhabian

Hi guys, I have a python script in /var/lib/openhab/myScripts/ups_hat_b.py that I can run manually like >python3 ups_hat_b.py and it publishes values through MQTT. but can’t find the way to automate the script to run as a service or execute it as a rule. can anyone help please. thanks

You can use linux cron entries to run the script at specific times / repeatedly every e.g. n minutes.
To run it at 10:00 PM:

00 22 * * * /usr/bin/python3 /var/lib/openhab/myScripts/ups_hat_b.py

To run it every 10 minutes between 8 AM and 8 PM

*/10 8-20 * * * /usr/bin/python3 /var/lib/openhab/myScripts/ups_hat_b.py

You can use a rule that uses the OH cron timer and run the script by using executeCommandLine to run it at e.g. 10:00 PM.

rule "rule triggered by timer"
when Time cron "0 0 22 ? * * *"
then
   var ScriptResponse = executeCommandLine( Duration.ofSeconds(5), "/usr/bin/python3",  "/var/lib/openhab/myScripts/ups_hat_b.py" ) 
end

Hi Wolfgang. I copied the codes to Rules in OH user web interface and it is stuck in INITIALINZING with the following error Failed to execute rule ‘c8f73a88fa’ with status ‘INITIALIZING’. Any idea why?

Frist two examples need to be entered in linux shell in crontab command:

  • login e.g. using ssh/putty
  • enter: crontab -e
  • then enter/paste one of the first two examples
  • exit the editor that your shell uses in case it is vi use :wq to write and quit

The third example is DSL rule syntax like it is used in a .rules file.
You should be able to adapt that one using in the UI. Do you need help for that ?

here is the DSL code:
configuration: {}
triggers:

  • id: “1”
    configuration:
    cronExpression: 0 * * * * ? *
    type: timer.GenericCronTrigger
    conditions:
    actions:

  • inputs: {}
    id: “2”
    configuration:
    type: application/vnd.openhab.dsl.rule
    script: >-
    rule “rule triggered by timer”

    when Time cron "0 * * * * ? *"
    
    then
       var ScriptResponse = executeCommandLine( Duration.ofSeconds(5), "/usr/bin/python3",  "/var/lib/openhab/myScripts/ups_hat_b.py" ) 
    end
    

    type: script.ScriptAction

and I get the following error:

2022-08-08 13:50:00.941 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘47cf012c80’ failed: ___ rule ___ “rule triggered by timer”
when Time cron “0 * * * * ? *”
then
var ScriptResponse = executeCommandLine( Duration.ofSeconds(5), “/usr/bin/python3”, “/var/lib/openhab/myScripts/ups_hat_b.py” )
end

  1. The method or field rule is undefined; line 1, column 0, length 4
  2. The method or field when is undefined; line 2, column 31, length 4
  3. The method or field cron is undefined; line 2, column 41, length 4
  4. The method or field then is undefined; line 3, column 62, length 4
  5. The method or field end is undefined; line 5, column 200, length 3
  6. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 1, column 5, length 25
  7. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 2, column 36, length 4
  8. This expression is not allowed in this context, since it doesn’t cause any side effects.; line 2, column 46, length 15

I think it needs to look like:

configuration: {}
triggers:
  - id: "1"
    configuration:
      cronExpression: 0 * * * * ? *
    type: timer.GenericCronTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: 'var ScriptResponse = executeCommandLine( Duration.ofSeconds(5), "/usr/bin/python3",  "/var/lib/openhab/myScripts/ups_hat_b.py" )'
    type: script.ScriptAction

Wolfgang_S is right.

Please be aware that the “envelope”

rule "name"
when
     <some trigger>
then
    <code to execute>
end

is valid for text files. But if configuring a dsl rule via UI, you will set the rule name and trigger via UI, so it makes no sense to add this part of information in the code. Therefor, rule, when, then and end have to be omitted.

the rule in the OH web interface didn’t work but I place it in crontab -e and their it works just fine. I have no idea why I can’t make it run in OH rule… Thanks guys

1 Like

Hi Wolfgang, I found out what was wrong with the rule. The rule was actually running properly but it was returning and error of “permission denied for i2c”. I just added “sudo adduser openhab i2c” and now the rule is working fine. Thanks for all your help.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.