Page 1 of 1
Switch Sonoff if another device is switched on/off
Posted: Tuesday 21 July 2020 23:28
by HvdW
Hi,
I have the light on the piano that is switched using RFXcom
It has a push on button (Piano on) and a push off button (Piano off)
Now I want a Sonoff device (sonof) to be switched on and off as well.
I copied a script from Dannybloe and adapted it.
However it does not seem to do anything.
Can you help me fix it.
Code: Select all
return {
active = true,
on = {
'Piano on'
},
execute = function(domoticz, Pianoon)
if (Pianoon.state == 'On') then
domoticz.devices['sonof'].switchOn()
domoticz.notify('This rocks!',
'Turns out that it is getting warm here',
domoticz.PRIORITY_LOW)
end
if (Pianoon.state == 'Off') then
domoticz.devices['sonof'].switchOff()
end
end
}
Re: Switch Sonoff if another device is switched on/off
Posted: Wednesday 22 July 2020 0:12
by waaren
HvdW wrote: Tuesday 21 July 2020 23:28
I copied a script from Dannybloe and adapted it. However it does not seem to do anything.
Your script uses some pre dzVents 2.0 syntax
try this
Code: Select all
local pushOn = 'Piano on'
local pushOff = 'Piano off'
return
{
on =
{
pushOn,
pushOff,
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
marker = 'pianoLights',
},
execute = function(dz, item)
local pushOn = dz.devices(pushOn)
local pushOff = dz.devices(pushOff)
local sonof = dz.devices('sonof')
if item == pushOn and item.active then
sonof.switchOn()
elseif item == pushOff and item.active then
sonof.switchOff()
end
end
}
Re: Switch Sonoff if another device is switched on/off
Posted: Wednesday 22 July 2020 0:21
by HvdW
Ha!
I hoped you'd turn up @waaren!
The script doesn't work as intended.
The log just shows that the pianolight is turned on or off and the function isn't activated.
I scrambled something else together in 2 functions. (it's crap, I know, but it works)
Piano-on
Code: Select all
return {
active = true,
on = {
devices = {
'Piano on' ,
},
},
logging =
{
level = domoticz.LOG_DEBUG,
},
execute = function(domoticz, piano)
_G.logMarker = _G.moduleLabel
local light = domoticz.devices('sonof')
domoticz.log('state of ' .. piano.name .. ': ' .. piano.state .. ' => ' .. (piano.active and ' active' or 'not active') )
if piano.state == 'On' then
light.switchOn()
else
light.switchOff()
end
end
}
Piano-off
Code: Select all
return {
active = true,
on = {
devices = {
'Piano off' ,
},
},
logging =
{
level = domoticz.LOG_DEBUG,
},
execute = function(domoticz, piano)
_G.logMarker = _G.moduleLabel
local light = domoticz.devices('sonof')
domoticz.log('state of ' .. piano.name .. ': ' .. piano.state .. ' => ' .. (piano.active and ' active' or 'not active') )
if piano.state == 'On' then
light.switchOff()
else
light.switchOn()
end
end
}
and the log
Code: Select all
2020-07-22 00:21:57.124 Status: User: Admin initiated a switch command (460/Piano on/On)
2020-07-22 00:21:57.263 Status: dzVents: Info: Handling events for: "Piano on", value: "On"
2020-07-22 00:21:57.263 Status: dzVents: Info: ------ Start internal script: piano-aan: Device: "Piano on (RFXCOM)", Index: 460
2020-07-22 00:21:57.264 Status: dzVents: Debug: Processing device-adapter for sonof: Switch device adapter
2020-07-22 00:21:57.264 Status: dzVents: Info: state of Piano on: On => active
2020-07-22 00:21:57.265 Status: dzVents: Debug: Constructed timed-command: On
2020-07-22 00:21:57.265 Status: dzVents: Info: ------ Finished piano-aan
2020-07-22 00:21:57.265 Status: EventSystem: Script event triggered: /home/hein/domoticz/dzVents/runtime/dzVents.lua
I'd rather use your code to do the job @waaren, it's cleaner.
Re: Switch Sonoff if another device is switched on/off
Posted: Wednesday 22 July 2020 0:52
by waaren
HvdW wrote: Wednesday 22 July 2020 0:21
The log just shows that the pianolight is turned on or off and the function isn't activated.
Forgot an essential part
This should do something more ..
l
Code: Select all
ocal pushOn = 'Piano on'
local pushOff = 'Piano off'
return
{
on =
{
devices =
{
pushOn,
pushOff,
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
marker = 'pianoLights',
},
execute = function(dz, item)
local pushOn = dz.devices(pushOn)
local pushOff = dz.devices(pushOff)
local sonof = dz.devices('sonof')
if item == pushOn and item.active then
sonof.switchOn()
elseif item == pushOff and item.active then
sonof.switchOff()
end
end
}
Re: Switch Sonoff if another device is switched on/off [Solved]
Posted: Wednesday 22 July 2020 12:15
by HvdW
Indeed, thanks a lot plus I learned something about dzvents again.
Re: Switch Sonoff if another device is switched on/off
Posted: Friday 24 July 2020 1:45
by HvdW
Hi,
Just want to share why I wanted to switch other devices with 433mHz.
In our house lights are switched with Domoticz.
My wife likes to switch with the remote control rather than using an app.
Now I am installing Shelly switches.
By switching the Shelly switches when a button is pushed on the 433mHZ remote control the user experience is identical, just action in the background is different.
And why switch from 433mHz to WiFi? (the world of ESP8266)
433mz is less reliable, that's all.
Here's the script
Code: Select all
local pushOn1 = 'Piano on'
local pushOff1 = 'Piano off'
local pushOn2 = 'Lotek en hal aan'
local pushOff2 = 'Lotek en hal uit'
local pushOn3 = 'Groen 1 aan'
local pushOff3 = 'Groen 1 uit'
return
{
on =
{
devices =
{
pushOn1,
pushOff1,
pushOn2,
pushOff2,
pushOn3,
pushOff3,
},
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
marker = 'pianoLights',
},
execute = function(dz, item)
local pushOn1 = dz.devices(pushOn1)
local pushOff1 = dz.devices(pushOff1)
local sonof = dz.devices('sonof')
if item == pushOn1 and item.active then
sonof.switchOn()
elseif item == pushOff1 and item.active then
sonof.switchOff()
end
local pushOn2 = dz.devices(pushOn2)
local pushOff2 = dz.devices(pushOff2)
local Shelly_19_0 = dz.devices('Shelly 19-0')
local Shelly_19_1 = dz.devices('Shelly 19-1')
if item == pushOn2 and item.active then
Shelly_19_0.switchOn()
Shelly_19_1.switchOn()
elseif item == pushOff2 and item.active then
Shelly_19_0.switchOff()
Shelly_19_1.switchOff()
end
local pushOn3 = dz.devices(pushOn3)
local pushOff3 = dz.devices(pushOff3)
local Shelly_20_0 = dz.devices('Shelly 20-0')
local Shelly_20_1 = dz.devices('Shelly 20-1')
if item == pushOn3 and item.active then
Shelly_20_0.switchOn()
Shelly_20_1.switchOn()
elseif item == pushOff2 and item.active then
Shelly_20_0.switchOff()
Shelly_20_1.switchOff()
end
end
}
Re: Switch Sonoff if another device is switched on/off
Posted: Tuesday 28 July 2020 0:41
by HvdW
Hi,
Solved yes but partial.
Now pushing the 433 MHz button switches Shelly as well.
However, Shellys are lagging up to 10 maybe 15 seconds.
I guess the switch signal is received at once but the dzVents script is waiting its turn to execute.
Is that correct and how can I speed up execution?
Re: Switch Sonoff if another device is switched on/off
Posted: Tuesday 28 July 2020 0:54
by waaren
HvdW wrote: Tuesday 28 July 2020 0:41
Now pushing the 433 MHz button switches Shelly as well. However, Shellys are lagging up to 10 maybe 15 seconds.
I guess the switch signal is received at once but the dzVents script is waiting its turn to execute.
Is that correct and how can I speed up execution?
It's true that event scripts can only be processed one at the time but I have many scripts active and never experience a delay > 1.5 seconds because of this. If a dzVents script is delayed by > 10 seconds, I assume something else is causing this. Maybe another script blocking the event system?
Best to make sure that you see script starts in your your log and analyze the logs to see where the delay occurs.