Switch Sonoff if another device is switched on/off  [Solved]

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

Moderator: leecollings

Post Reply
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Switch Sonoff if another device is switched on/off

Post 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
}
Bugs bug me.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch Sonoff if another device is switched on/off

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Switch Sonoff if another device is switched on/off

Post 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.
Bugs bug me.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch Sonoff if another device is switched on/off

Post 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 :oops:
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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Switch Sonoff if another device is switched on/off  [Solved]

Post by HvdW »

Indeed, thanks a lot plus I learned something about dzvents again.
Bugs bug me.
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Switch Sonoff if another device is switched on/off

Post 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
}
Bugs bug me.
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Switch Sonoff if another device is switched on/off

Post 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?
Bugs bug me.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switch Sonoff if another device is switched on/off

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest