How to acces action data from another action

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

How to acces action data from another action

Post by Medy » Sat Sep 19, 2020 1:59 pm

So as far as ive learned from the Writing plugin documentation


I can save and acces plugin data like this

1. Set Result in Configure
2. Acces in __start__




But what if I want to save data inside a aciton(Eg.Actionbase)

ACTION-A

Code: Select all

def myactionA(Eg.ActionBase)
    def Configure(self , value):
                while panel.Affirmed():
                    panel.SetResult(value)

And then acces it in another Action ?

ACTION-B

Code: Select all

def myactionB(Eg.ActionBase)
    def __call__(self , value):
        ???? <-- self.plugin.value doesnt work

User avatar
Sem;colon
Plugin Developer
Posts: 732
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: How to acces action data from another action

Post by Sem;colon » Sat Sep 19, 2020 4:03 pm

Hi Medy,
I'm not 100% sure if it's impossible, but it surely doesn't make much sense. You can't make sure the two actions are always both configured in the user's configuration.
Why do you want to do that?
Usually you would store the shared data in self.plugin during the runtime and then access it from any action.
If you like my work, Image me a drink :wink:

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sat Sep 19, 2020 6:42 pm

Sem;colon wrote:
Sat Sep 19, 2020 4:03 pm
Hi Medy,
I'm not 100% sure if it's impossible, but it surely doesn't make much sense. You can't make sure the two actions are always both configured in the user's configuration.
Why do you want to do that?
Usually you would store the shared data in self.plugin during the runtime and then access it from any action.

well found the problem now ... I named some variables similiar and messed things up big time ... I though stuff didnt work ... but it was my mistake :D

All good now. Thank you for the help --- your comment helped me get on the right track. (again)

User avatar
Sem;colon
Plugin Developer
Posts: 732
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: How to acces action data from another action

Post by Sem;colon » Sun Sep 20, 2020 8:12 am

You're welcome! I'm happy I could help :D
If you like my work, Image me a drink :wink:

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sun Sep 20, 2020 9:03 am

Sem;colon wrote:
Sat Sep 19, 2020 4:03 pm
Usually you would store the shared data in self.plugin during the runtime and then access it from any action.


I also wrote kgschlosser this

I have 2 actions
LearnSignal
SendSignal

so when I learn a Signal in "LearnSignal"
then save and restart eventghost -->


I cant load the data from "LearnSignal"
in "SendSignal"

I could merge the learn and send into 1 and then have acces through Configure --> __call__ .. but I didnt choose to do it ... because I thought it would be more user friendly having separate learn and send commands

1. The user could start LearnSignal
2. data gets saved
3. Send Signal


So every "learn Signal", would need to save to a place where the other action can acces the latest data.
(After a restart as well) ... I dont know how to save data without a configpanel ... I have no controls setup



So in the SendSignal I cant use the shared data from Eg.plugin,
since ... I dont have acces to it during outside of a config panel ?

kgschlosser told me to use Eg.PersistantData ... So unless im missing smth
im goign to use that ...


BEcause otherwise I dont know how I would acces data I just saved in a command inside another command ..
Im learning/experimenting ...so take it with a grain of salt

User avatar
Sem;colon
Plugin Developer
Posts: 732
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: How to acces action data from another action

Post by Sem;colon » Sun Sep 20, 2020 9:48 am

Hi Medy,

The normal way would be to indeed combine the learn and send action into one.

However, I see your use case now and it does make sense to store this data in a place both actions can access.
You always have access to the EG.plugin namespace, everywhere inside your plugin.
- in actions you would call self.plugin.something
- in your main plugin code (like in __start__()) you would call self.something.
This will access the same data.
However, this data is not saved persistently and is only available during the runtime.
Basically what you need is a "database" for your learned codes, EG doesn't have something like this on default.

Sure, there is EG.PersistantData, but it's not advisable to use it from inside plugins for these reasons:
- can be easily altered and overwritten by other plugins or user configurations
- will never be cleaned up: meaning, even when your plugin is removed or uninstalled. The data you stored there remains in the user's config. This leads to orphaned data that can cause all kinds of issues and also making it hard to reproduce troubleshoot issues with your plugin for you later.

What is done in other plugins is DIY when it comes to storing data, usually file based.
For example you could create your own plugin specific .xml or .json files to store your data in.
You would just dump your variable in self.plugin to that file whenever you learned a code and parse the file into the variable at startup ;)
If you like my work, Image me a drink :wink:

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sun Sep 20, 2020 10:13 am

Sem;colon wrote:
Sun Sep 20, 2020 9:48 am
Hi Medy,

The normal way would be to indeed combine the learn and send action into one.

However, I see your use case now and it does make sense to store this data in a place both actions can access.
You always have access to the EG.plugin namespace, everywhere inside your plugin.
- in actions you would call self.plugin.something
- in your main plugin code (like in __start__()) you would call self.something.
This will access the same data.
However, this data is not saved persistently and is only available during the runtime.
Basically what you need is a "database" for your learned codes, EG doesn't have something like this on default.

Sure, there is EG.PersistantData, but it's not advisable to use it from inside plugins for these reasons:
- can be easily altered and overwritten by other plugins or user configurations
- will never be cleaned up: meaning, even when your plugin is removed or uninstalled. The data you stored there remains in the user's config. This leads to orphaned data that can cause all kinds of issues and also making it hard to reproduce troubleshoot issues with your plugin for you later.

What is done in other plugins is DIY when it comes to storing data, usually file based.
For example you could create your own plugin specific .xml or .json files to store your data in.
You would just dump your variable in self.plugin to that file whenever you learned a code and parse the file into the variable at startup ;)
Okay , I like the idea of using my own file in that case ... do you have a example on where I should save that file
im not sure if there could be issues related to the installiation path and having acces to saving data to that path....


I have my EG installed in default folder....

Didnt mes with creating files yet .... Im sure its not a problem ... but since EG always come into the mix
Would prefer to have a example in case there is stuff I need to look out for....
Maybe this could be added to the offical documentation also. Would be cool

Im familiar with .json ... since Im coming from javascript / node.js / react ....

User avatar
Sem;colon
Plugin Developer
Posts: 732
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: How to acces action data from another action

Post by Sem;colon » Sun Sep 20, 2020 11:16 am

Perfect! :D
I prefer json too. And it's pretty easy indeed.
First "import json" in your plugin.
When you want to dump/ parse your json object, you just need to open a file in Python for reading or writing and use json.load() and json.dump() to convert your object to or from json.
Check this out: https://stackabuse.com/reading-and-writ ... ted%20here.

As for the file location, there is a folder for EventGhost in your Windows user profile (under appdata / roaming) that you can use and create a folder in for your plugin. I think there is even a constant in EG pointing to it, need to dig it out though (I don't have a PC close to me atm)
If you like my work, Image me a drink :wink:

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sun Sep 20, 2020 11:31 am

Sem;colon wrote:
Sun Sep 20, 2020 11:16 am
Perfect! :D
I prefer json too. And it's pretty easy indeed.
First "import json" in your plugin.
When you want to dump/ parse your json object, you just need to open a file in Python for reading or writing and use json.load() and json.dump() to convert your object to or from json.
Check this out: https://stackabuse.com/reading-and-writ ... ted%20here.

As for the file location, there is a folder for EventGhost in your Windows user profile (under appdata / roaming) that you can use and create a folder in for your plugin. I think there is even a constant in EG pointing to it, need to dig it out though (I don't have a PC close to me atm)
This sounds good , if there is a constant that would be great and I would like to use it ...
Since well it should be unique for each user .... so I dont have figure out how to get the username from the file system / add extra code

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sun Sep 20, 2020 5:06 pm

Sem;colon wrote:
Sun Sep 20, 2020 11:16 am
I think there is even a constant in EG pointing to it, need to dig it out though (I don't have a PC close to me atm)
diggy diggy hole ♪
https://www.youtube.com/watch?v=34CZjsEI1yU

User avatar
Sem;colon
Plugin Developer
Posts: 732
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: How to acces action data from another action

Post by Sem;colon » Sun Sep 20, 2020 8:00 pm

Hehe, sorry for the late reply, was a busy day again ;)

Code: Select all

eg.configDir
That's the one

Btw. Nice song :D
If you like my work, Image me a drink :wink:

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sun Sep 20, 2020 8:06 pm

Thanks this will help ;)

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Sun Sep 27, 2020 1:08 pm

Sem;colon wrote:
Sun Sep 20, 2020 8:00 pm
Hehe, sorry for the late reply, was a busy day again ;)

Code: Select all

eg.configDir
That's the one

Btw. Nice song :D
EDIT : SOLVED ... it was annoying .... but I found out through google that the right encoding is "hex" ... so now I can encode and decode without error .... loading signal data from json works :D ... I can FINALLY .. finish the plugin in the course of this week now ...

Man this library doesnt tell you anything about how to use it :D ... really sucked having to rummage through google...
Though love

Solved with :
https://github.com/mjg59/python-broadlink/issues/17
https://www.domoticz.com/forum/viewtopi ... nk#p103517
https://github.com/mjg59/python-broadlink/issues/87

Neever Heard of domotics before ... interstingly they also have a the same forum as EG ...
Im very intrigued by it ... they also have a implementaion for broadlink... How have I not heard of this xD


Hey man applied saving to file today ..
but I still have one last problem.... which is saving the signal data to the json...


Code: Select all

ir_packet = selectedDevice[0].check_data()
apperently the data it is of <type 'str'>

but when I try to dump this string to a json like this :

Code: Select all

      
rf_fson = {'name' : 'test' , 'data': ir_packet}
dict = []
dict.append(ir_packet)
data = json.dumps(dict)
im getting this error

Code: Select all

  File "json\__init__.pyc", line 244, in dumps
  File "json\encoder.pyc", line 207, in encode
  File "json\encoder.pyc", line 270, in iterencode
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb2 in position 0: invalid start byte
I have no way of knowing how to correctly decode this thing without loosing information

On stackoverflow I only found to ignore the error .. but it seems information is lost ... since the data send no longer works...

with smth like this

Code: Select all

input_string = input_string.decode('ascii', 'ignore').encode('ascii') #note: this removes the character and encodes back to string.
Last edited by Medy on Sat Oct 10, 2020 12:38 pm, edited 1 time in total.

User avatar
Sem;colon
Plugin Developer
Posts: 732
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: How to acces action data from another action

Post by Sem;colon » Mon Sep 28, 2020 7:13 pm

Yeah, I agree encoding and decoding errors can be really a pain in the *** :/
I'm glad you found out what the right encoding is :D
If you like my work, Image me a drink :wink:

User avatar
Medy
Experienced User
Posts: 161
Joined: Sat Nov 15, 2014 8:33 pm
Location: Germany

Re: How to acces action data from another action

Post by Medy » Tue Sep 29, 2020 8:01 am

Sem;colon wrote:
Mon Sep 28, 2020 7:13 pm
Yeah, I agree encoding and decoding errors can be really a pain in the *** :/
I'm glad you found out what the right encoding is :D
Indeed . It was all new to me ... didnt even know what was going on there...
Panic mode was activated :D

Post Reply