Variable as trigger OR .checkFirst() in json?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Variable as trigger OR .checkFirst() in json?

Post by leby »

Hi, I cant get around how to solve my need. I want to update a dummy switch in domoticz from a shelly device and vice versa.

I have to find a way to avoid loop. I have two options that I have tried.

1: A variable that is changed from the shelly

Code: Select all

/json.htm?type=command&param=updateuservariable&vname=Shelly_kallaren&vtype=integer&vvalue=0
this works just fine.

Then to update the switch I tried this event but cant get it to update the switch (or I think it is the trigger that never happen)

Code: Select all

return {
on = {
 variables = {
 	'Shelly_kallaren'
 }
},
execute = function(domoticz, item, triggerInfo)
if uservariables['Shelly_kallaren'] == '1' then
   domoticz.devices(451).switchOn().checkFirst()

elseif uservariables['Shelly_kallaren'] == '0' then
device.Shelly_kallaren.switchOff().checkFirst()
    end
end
}
2: the second way is probably better, less complex and that is to update the dummy switch directly with json. The problem I have here is that I have not fund any equvivilent to the dzvent .checkFirst() in json and without it the looping starts.

I have used

Code: Select all

http://192.168.3.10:8080/json.htm?type=command&param=switchlight&idx=451&switchcmd=Off
and that works but I run in to loop. Is there anything like

Code: Select all

http://192.168.3.10:8080/json.htm?type=command&param=switchlight&idx=451&switchcmd=Off.checkFirst()
??

I know that I can use MQTT but I wolud like to use the shelly cloud as well but the memory in the shelly only allows one of them.

Please you all clever people out there, point me in the right direction!
/lennart
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by waaren »

leby wrote: Friday 24 May 2019 13:34 Hi, I cant get around how to solve my need. I want to update a dummy switch in domoticz from a shelly device and vice versa.
One way of solving this is to have shelly update a domoticz text sensor. That can trigger a dzVents script and have that interpret the text in the sensor and act accordingley.

some questions / remarks.

This url does trigger the attached script. Maybe you are on a very (too) old version ?
You set the var to integer but check against a string

http://pi-3:8080/json.htm?type=command& ... 0&vvalue=1

Code: Select all

local scriptVar = "test Variable trigger"
 
return {  
    on = { variables = { 'ff'} } ,

    logging = {
                    level = domoticz.LOG_INFO,
                    marker = scriptVar
    },
    
    execute = function(dz, item )
        if dz.variables('ff').value == 1 then 
            dz.log('============ variable ' .. item.name .. ' triggered to 1',dz.LOG_FORCE)
        else
            dz.log('============ variable ' .. item.name .. ' triggered to 0',dz.LOG_FORCE)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by leby »

@warren
Hmm, I run Version: 4.10548 on rpi but perhaps its just that I check it as a string? The variable get set so far it works..
/lennart
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by leby »

No, it did not help :-( I have changed the url to

Code: Select all

http://192.168.3.10:8080/json.htm?type=command&param=updateuservariable&vname=Shelly_kallaren&vtype=2&vvalue=Off
and the script to

Code: Select all

return {
	on = { variables = { 'Shelly_kallaren' }
	},
	execute = function(domoticz, item)
	if uservariables['Shelly_kallaren'] == 'On' then
	   domoticz.devices(451).switchOn().checkFirst()

	elseif uservariables['Shelly_kallaren'] == 'Off' then
	device.Shelly_kallaren.switchOff().checkFirst()
    end
end
}
The url update the variable as it should so still no problem there but the script does not trigger, I will try to run it on time but then i will get lag in the update.. More suggestions?
/lennart
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by waaren »

leby wrote: Friday 24 May 2019 15:32 More suggestions?
A couple...
  • Look in the domoticz logfile
  • Check the syntax of your script. You are using code that is not valid after dzVents 1.1. 2
  • Have a look at the wiki
  • Update to latest stable.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by leby »

Cant get my head around this. The script works so far that the variable change and the script runs and wright to the log but no switch change??

Code: Select all

return {
	on = {
		variables = { 
			'Shelly*'
		}
	},
	execute = function(dz, variable)
	    local Var_Tvatt = dz.variables(6)
	    local Tvatt = dz.devices(312)
	    
	if Var_Tvatt == 'On' then
	   Tvatt.switchOn()
	   domoticz.log('Tvatt on!')

	elseif  Var_Tvatt == 'Off' then
	        Tvatt.switchOff()
			domoticz.log('Tvatt off!')
    end
end
}
Anyone?
/lennart
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by waaren »

leby wrote: Friday 24 May 2019 18:06 Cant get my head around this. The script works so far that the variable change and the script runs and wright to the log but no switch change??

Anyone?
Try it with this ?

Code: Select all

return {
	on = {
		variables = { 
			'Shelly*'
		}
	},
	execute = function(dz, variable)
		local Var_Tvatt = variable.value
		local Tvatt = dz.devices(312)
	    
	if Var_Tvatt == 'On' then
		Tvatt.switchOn()
		domoticz.log('Tvatt on!')

	elseif  Var_Tvatt == 'Off' then
		Tvatt.switchOff()
		domoticz.log('Tvatt off!')
	else
		domoticz.log('Funny value in Var_Tvatt')
    end
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
leby
Posts: 98
Joined: Monday 28 July 2014 9:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Stockholm
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by leby »

@warren even if I don't see why it work, it does :-) Many thx!!
/lennart
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by waaren »

leby wrote: Friday 24 May 2019 21:45 @warren even if I don't see why it work, it does :-) Many thx!!
What I changed to get it working is that the script now uses the value of the variable (.value); in your last version you tried to compare the variable (which is a table in Lua) with a string.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kimot
Posts: 105
Joined: Saturday 25 November 2017 17:18
Target OS: Raspberry Pi / ODroid
Domoticz version: v3.8153
Location: Czech Rep.
Contact:

Re: Variable as trigger OR .checkFirst() in json?

Post by kimot »

I am using ESPeasy on Shelly or Sonoff ( it is ESP8266 based )
Solution is simply.

To switch Shelly from Domoticz dummy switch, I write to switch edit page "On action":

Code: Select all

http://192.168.1.102/control?cmd=GPIO,12,1
Off action:

Code: Select all

http://192.168.1.102/control?cmd=GPIO,12,0
To report switch state from Shelly to Domoticz simply write "Rules":

Code: Select all

SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=On
for switch On.
This command switched dummy switch to ON in Domoticz.
It is fact, that Domoticz immediately sends command for switching Shelly on, but it is switched, so it is no problem and gpio setting command not send nothing else from Shelly.
( You must check mark off "Send to Controller" box in relay ESPEASYs device tab, of course )

If you need some complex action when dummy switch switched, you can send custom command from dummy switch instead of GPIO.

This is example for Rules, which toggles Sonoff relay ( and LED ) with each Sonoff button press and report actual state to Domoticz.
From Domoticz you can switch on or off relay by clicking on dashboard and thus sending commands "lamp_on" or "lamp_off".
From other ESPs or other devices you can send "lamp_change" command to change relay state and this new state will be reported to Domoticz too.

No Domoticz variables and scripting needed.

Code: Select all

on lamp_on do
    gpio,12,1   //  relay on
    gpio,13,0  //    LED on
endon

on lamp_off do
    gpio,12,0
    gpio,13,1
endon

on lamp_change do
  if [rele#Switch]=1        // if relay is on
    event,lamp_off           // switch it off !
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=Off        // and send actual state to Domoticz
  else
    event,lamp_on
    SendToHTTP 192.168.1.253,8080,/json.htm?type=command&param=switchlight&idx=19&switchcmd=On
  endif
endon

on SW01#Switch do          // if button is pressed
  event,lamp_change        // call "lamp_change" event = toggle relay and report to Domoticz
endon
RPi2 Domoticz v 4.10717
10 x Sonoff Basic - ESPeasy
1 x Wemos D1 - ESPeasy
1 x Shelly Plus Plug S
1 x Sonoff S26 - ESPeasy
1 x Shelly 1
1 x MySensors HC-SR04
1 x MySenosrs wifi gateway
1 x RFLink
4x Cam IPC-T240H
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest