mycontroller-org / mycontroller-v1-legacy

The Open Source Controller
http://www.MyController.org
Apache License 2.0
148 stars 90 forks source link

[Improvement] Variables (notification) in script binding #411

Closed cimba007 closed 6 years ago

cimba007 commented 7 years ago

According to my post I propose a way to use variables in script bindings. https://forum.mycontroller.org/topic/207/telegram-notification/2

The variable should be the already known notification variables that can be used in E-Mail and Pushbullet notification.

Implementing this improvement would make operationgs calling scripts after beeing triggered by a rule much more useful and would help keeping operations as clean and multifunctional as needed!

Thanks in advance for your hard work!

cimba007 commented 7 years ago

Latest snapshot triggers some serious error:

https://pastebin.com/N4LX4X9a

Maybe same error as was here? https://forum.mycontroller.org/topic/142/corrupted-database

jkandasa commented 7 years ago

@cimba007 Thank you, I have updated the fix on this SNAPSHOT build. Kindly inform back.

cimba007 commented 7 years ago

Somhow I now have trouble getting rules working at all. Rule get fired up but not the corresponding operation. Nothing on the logfile .. I think will do a restore first. Maybe the error posted above might have caused some further issues.

.. get the operation trigged again like normal .. but no script execution and no error .. still investigating

cimba007 commented 7 years ago

@jkandasa Apparently the script is not called if the binding is like this:

{"message":"M ${notification.actualValue}"}
2017-10-25 21:19:22,074 DEBUG [Thread-7] [org.mycontroller.standalone.rule.McRuleListener:33] About to execute the rule:rule
2017-10-25 21:19:22,076 DEBUG [Thread-7] [org.mycontroller.standalone.rule.McRuleBase:202] Operations going to execute:[OperationExecuteScript(scriptFile=operations/telegram_bot.py, scriptBindings={message=M ${notification.actualValue}}, scriptBindingsTemp={})]
2017-10-25 21:19:30,135 WARN [Quartz_Scheduler_Worker-4] [org.mycontroller.standalone.rule.McRuleEngine:120] Scheduled Rule execution skipped. Engine not available for more than 4000 ms
2017-10-25 21:19:34,223 WARN [Quartz_Scheduler_Worker-5] [org.mycontroller.standalone.rule.McRuleEngine:120] Scheduled Rule execution skipped. Engine not available for more than 4000 ms
2017-10-25 21:19:39,008 WARN [Quartz_Scheduler_Worker-6] [org.mycontroller.standalone.rule.McRuleEngine:120] Scheduled Rule execution skipped. Engine not available for more than 4000 ms
2017-10-25 21:19:44,012 WARN [Quartz_Scheduler_Worker-7] [org.mycontroller.standalone.rule.McRuleEngine:120] Scheduled Rule execution skipped. Engine not available for more than 4000 ms
2017-10-25 21:19:49,040 WARN [Quartz_Scheduler_Worker-10] [org.mycontroller.standalone.rule.McRuleEngine:120] Scheduled Rule execution skipped. Engine not available for more than 4000 ms
2017-10-25 21:19:54,005 WARN [Quartz_Scheduler_Worker-2] [org.mycontroller.standalone.rule.McRuleEngine:120] Scheduled Rule execution skipped. Engine not available for more than 4000 ms

Edit: Ignore this .. might just be cooncidence after starting mycontroller. The real issue lies here:

M ${notification.actualValue}

So the value from the binding is not replaced as expected. Maybe the extra "" prevent this but as there is a json-sanity check I can't input the value without the ""

image

jkandasa commented 7 years ago

@cimba007 can you post your complete script?

cimba007 commented 7 years ago
with open("test.txt", "a") as myfile:
    myfile.write(message+"\n")
if 'notification' in locals() or 'notification' in globals():
    with open("test.txt", "a") as myfile:
        myfile.write(notification+"\n")
import commands
if 'picture' in locals() or 'picture' in globals():
   with open("test.txt", "a") as myfile:
      myfile.write("picture"+"\n")
   v = commands.getstatusoutput('/usr/bin/python /home/pi/mqtt/telegram_min.py -i "XXX" -m "' + message + ' " -p "' + picture + '" ' )
   with open("test.txt", "a") as myfile:
      myfile.write(v+"\n")
   commands = None
else:
   with open("test.txt", "a") as myfile:
      myfile.write("msg only"+"\n")
   v = commands.getstatusoutput('/usr/bin/python /home/pi/mqtt/telegram_min.py -i "XXX" -m "' + message + '" ')
   with open("test.txt", "a") as myfile:
      myfile.write(v+"\n")
   commands = None

Logfile

M ${notification.actualValue}M ${notification.actualValue}M ${notification.actualValue}
msg only
M ${notification.actualValue}
msg only
M ${notification.actualValue}
Alarm durch Bewegungsmelder Haustuer
Gateway offline (Mysensors)
jkandasa commented 6 years ago

@cimba007 sorry for the late response. I missed it somehow.

This script executed by operation right? If you execute manually, it won't work.

cimba007 commented 6 years ago

On the latest snapshot I can use this:

if 'notification' in locals() or 'notification' in globals():
   with open("test.txt", "a") as myfile:
      vv = unicode(notification,'utf-8')
      myfile.write(vv.encode('utf8')+"\n")

Which will write the follwing output to "myfile".

Alarm durch Bewegungsmelder Haustür
Rule definition: Alarm durch Bewegungsmelder Haustür
Condition: State [ if {[G]:mGW >> [N]:0:GW >> [S]:3:433 Bewegungsmelder Haustür >> [SV]:Tripped} == {On} ]
Present value: 1
Triggered at: Nov 05, 2017 16:19:58 MEZ
--- www.mycontroller.org

I would consider this issue closed! Thanks for your work!