Page 1 of 1

[Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 20:27
by Zweef
Hi2UAll!

I'm just starting with lua and need some (= a lot) assistance with this. So, please be patient to me! :mrgreen:

Because the best way to learn is to have a goal. So, this is my first goal:
When a variable is changed, send a Telegram message with the content from that variable.

Variable name = HC2_Bewonersstatus (English: Residents status)
The contents will be updated by my Fibaro HC2 Gateway as has got 5 statuses:
Thuis (English: Home)
Slaap (English: Sleeping)
Bewaakt (English: Watched, intended to watch the house if it is uninhibited for some period of time, will be implemented later on)
Weg (English: Away)
Vakantie (English: On Vacation)

So, first I made a new event name:
Telegram (Send), Lua, UserVariable

Code: Select all

commandArray = {}
-- ------------------------------------------------------------------------------------------
-- ℹ Initialisation of user settings (change if necessary)
-- ------------------------------------------------------------------------------------------
local bot = 'xxxxxxxxx'; -- Telegram Bot ID
local token = 'yyyyyyyyyyyyyy'; -- Telegram Bot Token
local chatId = 'zzzzzzzzzz'; -- Telegram Chat ID
local message = 'This is a test!'; -- Test messages

-- ------------------------------------------------------------------------------------------
-- Code (do not change)
-- ------------------------------------------------------------------------------------------
print ('Telegram message will be send..');
os.execute('curl --data chat_id='..chatId..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..bot..':'..token..'/sendMessage" ')
print ('Telegram message is send!)';

return commandArray
Step 1: how can I detect the proper variable change, get the content and contruct a message (message = 'Bewonerstatus --> '..HC2_Bewonersstatus)?

Please help, it will be highly appreciated!

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 20:53
by jvdz
Just add an if that looks something like this:

Code: Select all

if uservariablechanged['variablename'] then
	-- your logic goes here
end
Jos

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 21:15
by Zweef
jvdz wrote:Just add an if that looks something like this:

Code: Select all

if uservariablechanged['variablename'] then
 -- your logic goes here
end
Jos
That simple, huh! Thanks Jos.

Just another another question, the commandArray = {} can this be inserted underneath the code-header instead of as first command?
(ie what is this command used for?)

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 21:28
by jvdz
The commandArray table needs to be returned at each run of an Event script as the internal logic expect it to contain the actions it needs to do after your script ends. In your case you don't add any entry to it as you shell an external process, but it always needs to be returned.
So it really doesn't matter where in your script it is defined, but common practice is to do this at the start of a script.

Jos

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 21:40
by Zweef
Clear.

What is the command for getting the contents from a user variable?

In the lua example it's stated:
-- uservariables['yourvariablename'] = 'Test Value'
but that is used to write a content. Do I need
-- userContent = uservariables['yourvariablename']
to get the content?

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 21:55
by jvdz
Think this is a wiki page that you are interested in and can read all about it: https://www.domoticz.com/wiki/Events#Lu ... _Interface

Jos

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 22:03
by Zweef
Thanks.

But it doesn't mention how to get the content from an user variable, as I read right.... :?

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 22:09
by jvdz
It does but let me help a little:
To retrieve the value of variable TestVariable: uservariables['TestVariable']
To set the variable to a new value: commandArray['Variable:TestVariable']='Some value'

Have fun yet with scripting :-)
Jos

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 23:01
by Zweef
jvdz wrote:It does but let me help a little:
To retrieve the value of variable TestVariable: uservariables['TestVariable']
To set the variable to a new value: commandArray['Variable:TestVariable']='Some value'

Have fun yet with scripting :-)
Jos
I do have fun, it's like all learning it again from scratch... :D

I really appreciate your time and effort to get me started again!

I just added a selector switch (virtual device) with this 5 options corresponding the 5 status (Home, Away, etc). I though is was handy for testing variable changes. So in the selector action I added (to change the variable):
http://127.0.0.1:8080/json.htm?type=com ... alue=Thuis

That's working! So, that's not the first 'YES!!' I had today. :mrgreen:
So you can say, if have fun!

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Sunday 12 March 2017 23:12
by Zweef
Hmm, somehow my code is not triggered (yet)..

Code: Select all

commandArray = {}
-- ------------------------------------------------------------------------------------------
-- ℹ Initialisation of user settings (change if necessary)
-- ------------------------------------------------------------------------------------------
local bot = 'xxxxxx'; -- Telegram Bot ID
local token = 'yyyyyyyyyyyyyyyyyyy'; -- Telegram Bot Token
local chatId = 'zzzzzzzzzzzz'; -- Telegram Chat ID
local userVar = 'HC_Bewonersstatus'; -- Uservariable to watch for triggering on change

-- ------------------------------------------------------------------------------------------
-- Code (do not change)
-- ------------------------------------------------------------------------------------------
    -- Watch uservariable for change
local userVarContent = '';
local message = '';
if uservariablechanged[userVar] then
    userVarContent = uservariables[userVar];
    message = 'Bewonersstatus --> '..tostring(userVarContent);
    print ('Telegram message will be send..');
    os.execute('curl --data chat_id='..chatId..' --data-urlencode "text='..message..'"  "https://api.telegram.org/bot'..bot..':'..token..'/sendMessage" ');
    print ('Telegram message is send!');
end

return commandArray
I will look into it tomorrow what is going wrong. Had enough fun for today.... ;)

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Monday 13 March 2017 19:44
by lemassykoi
If your script is in LUA, maybe you should remove all the ; you put everywhere ? ;)

also, you should use :

Code: Select all

commandArray[1]={['Variable:userVarContent'] = userVar}
to update your user variable

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Monday 13 March 2017 20:19
by jvdz
lemassykoi wrote:If your script is in LUA, maybe you should remove all the ; you put everywhere ? ;)
Agree it isn't needed but it shouldn't cause any issues either.
lemassykoi wrote: also, you should use :

Code: Select all

commandArray[1]={['Variable:userVarContent'] = userVar}
to update your user variable
I assume the JSON call is done from the external HC system, but either way, it is required to do the update through a JSON call as updating a variable through the event system will not trigger any script_variable_xyz.lua scripts.

Jos

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Tuesday 14 March 2017 19:46
by Zweef
The variable is updated by the Fibaro HC2 controller (through Domoticz API-command, JSON call).
jvdz wrote:
lemassykoi wrote:If your script is in LUA, maybe you should remove all the ; you put everywhere ? ;)
Agree it isn't needed but it shouldn't cause any issues either.
I do this to because in the LUA early days it was needed. Just want to write/learn code by being as accurate/complete as possible. I know it's not needed. Just being a purist. :mrgreen:
jvdz wrote:
lemassykoi wrote: also, you should use :

Code: Select all

commandArray[1]={['Variable:userVarContent'] = userVar}
to update your user variable
I assume the JSON call is done from the external HC system, but either way, it is required to do the update through a JSON call as updating a variable through the event system will not trigger any script_variable_xyz.lua scripts.

Jos
You complete lost me here... :shock:
Do you mean when a variable is changed by an other source then Domoticz, nothing will be triggerd?
Can you please explain?

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Tuesday 14 March 2017 20:08
by jvdz
Zweef wrote: You complete lost me here... :shock:
Do you mean when a variable is changed by an other source then Domoticz, nothing will be triggerd?
Can you please explain?
It is the other way around.
When you update a variable with:
commandArray['Variable:TestVariable']='Some value'
the event system will update the variable but will not trigger any script_variable_*.lua event scripts.
script_variable_*.lua event scripts are only triggered when the variable is updated through JSON. I think this was done to avoid an indefinite loop but honestly see no difference with variables and devices as the same is possible for devices.

See another topic with some more information on this similar question: viewtopic.php?f=6&t=14745&p=108042#p108042

Not sure if anything has changed on the latest version of domoticz, but assume somebody will chime in when this is the case.
Jos

Re: [Noob, so help needed] Sending Telegram message when variable has changed

Posted: Tuesday 14 March 2017 20:29
by Zweef
Okay!

Learning all the time, learning all the time.... 8-)

Thanks a lot, Jos!