Page 1 of 1

Stuck with a script

Posted: Tuesday 02 April 2024 18:44
by HvdW
Hi,
I had a script running for years without any problem.
Then there was this lan problem and the shelly device didn't react.
I tried to solve it by adapting the script and that made things even worse.

Here's the situation.
I have some old Ikea plugs wich are switched with rfxtfx (433 mHz)
When 'piano aan' is switched I want to switch selly21 device on as well.
When 'piano uit' is switched I want to switch selly21 device off as well.
Here's to script:

Code: Select all

-- switch-another

local pushOn2 = 'Piano aan'
local pushOff2 = 'Piano uit'
local Shelly21 = 'Groene lampjes'

return
{
    on =
    {
        devices =
        {
            pushOn2,
            pushOff2,
            Shelly21, 
        }, 
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change from LOG_DEBUG to domoticz.LOG_ERROR when all OK
        marker = 'piano lampjes',
    },

    execute = function(domoticz, item)
        -- domoticz.devices(pushOff2).dump()
        domoticz.log('0 pushOn2          : ' ..pushOn2 , domoticz.LOG_DEBUG)
        --domoticz.log('1 waarde van item is           : ' ..item, domoticz.LOG_DEBUG)
        domoticz.log('2 Groene lampjes   : ' ..domoticz.devices('Groene lampjes').state, domoticz.LOG_INFO)
        domoticz.log('2a Piano aan state   : '..domoticz.devices('Piano aan').state,domoticz.LOG_DEBUG)
        domoticz.log('2b Piano uit state   : '..domoticz.devices('Piano uit').state,domoticz.LOG_DEBUG)
        if item.name == 'pushOn2' and item.active then
            domoticz.devices(Shelly21).switchOn()
            domoticz.log('3 Groene lampjes pushOn2  : ' ..domoticz.devices('Groene lampjes').state, domoticz.LOG_INFO)
        end
        if item.name == 'pushOff2' and item.active  then  -- and item.active
            domoticz.devices(Shelly21).switchOff()
            domoticz.log('4 Groene lampjes pushOff2     : ' ..domoticz.devices('Groene lampjes').state, domoticz.LOG_INFO)
        end
    end
}
The trick lies in this part of the code: item.name == 'pushOn2' (I suppose)

Of course help is appreciated.

Re: Stuck with a script

Posted: Tuesday 02 April 2024 18:58
by Kedi
Why is in this part Shell21 present? I think you should remove it.

Code: Select all

        devices =
        {
            pushOn2,
            pushOff2,
            Shelly21, 
        }, 
While you at it this could also be changed

Code: Select all

        domoticz.log('2 Groene lampjes   : ' ..domoticz.devices('Groene lampjes').state, domoticz.LOG_INFO)
        domoticz.log('2a Piano aan state   : '..domoticz.devices('Piano aan').state,domoticz.LOG_DEBUG)
        domoticz.log('2b Piano uit state   : '..domoticz.devices('Piano uit').state,domoticz.LOG_DEBUG)
in

Code: Select all

        domoticz.log('2 Groene lampjes   : ' ..domoticz.devices(Shelly21).state, domoticz.LOG_INFO)
        domoticz.log('2a Piano aan state   : '..domoticz.devices(pushOn2).state,domoticz.LOG_DEBUG)
        domoticz.log('2b Piano uit state   : '..domoticz.devices(pushOff2).state,domoticz.LOG_DEBUG)
It is more consistent code.

Re: Stuck with a script

Posted: Tuesday 02 April 2024 19:32
by HvdW
@Kedi: thanks, that is a good start. All Shelly21 removed and consistent naming now.

Re: Stuck with a script

Posted: Wednesday 03 April 2024 16:20
by Kedi
Is it working now as intended?