Page 1 of 1

X10 extended dim, another solution, works with Homekit, pure lua, no shell calls

Posted: Wednesday 14 October 2020 9:21
by cuetus
I'm completely new to Domoticz, just installed last weekend, learning this system all the time. So this could be stupid way of scripting x10 dimmers, but I couldn't figure out better way.

This works well enough from Domoticz UI and also from Apple Homekit. But it requires 2 devices, one actual X10 device set up as on/off and another one specially named virtual dimmer for each x10 dimmer you have. This is because the device event triggered for set level command to x10 device set as dimmer from Homekit was completely weird and also because x10 dimmers have their dimming slider disabled in domoticz ui.

Device naming, let's say you have following at address A1
"My device name" set up as actual X10 device, on/off type.
"My device name ::a1" set up as virtual dimmer

It uses native mochad support for the usual on/off, and opens tcp socket to mochad for the xdim. So no external bash script calls or anything like that. Just open your events, copy paste this as new script, set it to trigger on device changes, create your special devices with ::XX naming and you're good to go. And you need to install lua-socket if you don't have it yet.

Code: Select all

commandArray = {}

local mochadcommand
local tab_deviceName
local tab_deviceValue
local dimValue
local host, port = "127.0.0.1", 1099
local socket = require("socket") --- sudo apt-get install lua-socket
local tcp = assert(socket.tcp())

function split(inputstr, sep)
        local t={}
        for str in string.gmatch(inputstr:gsub(sep,"\0"), "([^\0]+)") do
                table.insert(t, str)
        end
        return t
end


function trim(s)
        return s:match "^%s*(.-)%s*$"
end

function sendToMochad(mochadCommand)

        tcp:connect(host, port);
        tcp:send(mochadCommand.."\n");
        tcp:close()

end

for deviceName,deviceValue in pairs(devicechanged) do
        
        tab_deviceName = split(deviceName,"::") -- device name format for our special dimmer devices "Whatever Device ::A1" 

        if tab_deviceName[2] ~= nil then -- our specially named dimmer device

                if deviceValue == "Off" or deviceValue == "On" then
                        commandArray[trim(tab_deviceName[1])] = deviceValue -- domoticz native mochad support can handle these just give it the first part of the name                             

                else  

                        if deviceValue:sub(1,9) == "Set Level" then -- now we're dimming
                                tab_deviceValue = split(deviceValue.."","%s")
                                dimValue = math.floor(tab_deviceValue[3]*0.63)
                                mochadCommand = "pl "..tab_deviceName[2].." xdim "..dimValue
                                print("Trigger dimmer "..tab_deviceName[1]..": "..mochadCommand)
                                sendToMochad(mochadCommand)
                        end
                end
        end
end

return commandArray

Questions for you experienced domoticz people:
1) Is there a way to get x10 address of a device from within the script? (I couldn't find, so I added it to the device name :lol: )
2) Any way enable X10 dimmer slider? (Needed to play with virtual devices to get the dimming command in domoticz...)
3) Do you see any potential problems with this implementation?

Re: X10 extended dim, another solution, works with Homekit, pure lua, no shell calls

Posted: Wednesday 14 October 2020 9:31
by cuetus
So this is what it looks like, and the slider works as it is supposed to.

Image
Image

Re: X10 extended dim, another solution, works with Homekit, pure lua, no shell calls

Posted: Thursday 15 October 2020 22:28
by cuetus
cuetus wrote: Wednesday 14 October 2020 9:21
Questions for you experienced domoticz people:
1) Is there a way to get x10 address of a device from within the script? (I couldn't find, so I added it to the device name :lol: )
2) Any way enable X10 dimmer slider? (Needed to play with virtual devices to get the dimming command in domoticz...)
3) Do you see any potential problems with this implementation?
Answering my question #1, yes, deviceId is ascii code for the house code, unit, is.. unit.
domoticz.log('House '..string.char(device.deviceId)..' Unit: '..device.unit)

#2) so far I've seen that X10 device set as dimmer gets maxDimLevel 0: who, where and when is this set? reading through if then else jungle in the sources but if someone could point me in right direction this would go way faster :roll:

#3) it could be better...

Re: X10 extended dim, another solution, works with Homekit, pure lua, no shell calls

Posted: Friday 16 October 2020 12:41
by cuetus
Continuing to answer myself

2) RFXNames.cpp, single line change to GetLightStatus

Code: Select all

       case pTypeLighting1:
                switch (dSubType)
                {
                case sTypeX10:
                        bHaveGroupCmd = true;
                        maxDimLevel = 63; //added to get dim slider
                        switch (nValue)
                        {
But this is no longer Lua and that wasn't enough, stopping talking to myself here after this post :lol:

Code: Select all

 Status: User: Admin initiated a switch command (12/Työhuone (Valaistus)/Set Level) 
is written to log but it didn't trigger dzVent device trigger. If someone who is already familiar with domoticz wants to add absolute dimming to native mochad implementation this should not be difficult..x10 absolute dimming supports values 0 - 63. So just throw it to mochad socket rf a1 xdim x10level / pl a1 xdim x10level (first one is sending to rf, second to power line, I assume power line would be enough, never sent anything to rf from cm15a in my old system). Add another subtype for lighting or add to release notes that it works only on those x10 dimmers that support absolute dimming (din rail installable devices do at least).