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

Moderator: leecollings

Post Reply
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post 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!
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post by jvdz »

Just add an if that looks something like this:

Code: Select all

if uservariablechanged['variablename'] then
	-- your logic goes here
end
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post 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?)
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post 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?
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post by Zweef »

Thanks.

But it doesn't mention how to get the content from an user variable, as I read right.... :?
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post 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!
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post 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.... ;)
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
lemassykoi
Posts: 15
Joined: Saturday 11 March 2017 23:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: France
Contact:

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

Post 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
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post 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?
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Zweef
Posts: 17
Joined: Sunday 02 November 2014 16:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

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

Post by Zweef »

Okay!

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

Thanks a lot, Jos!
Domoticz on Raspberry Pi 3 using mainly Z-Wave

Other gateways:
Fibaro HC2
Athom Homey
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest