property zwave encode  [Solved]

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

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

property zwave encode

Post by pvklink »

Hi ,

I try to improve my zwave config script. Changing my old function(zet_zwave_alarm) to a new function(applyzwavenodeconfig) that uses logical names instead off codes.

It does work perfect, but still some questions (one by one)
1. In my function i use the idx off the zwave device, i use 7 for the idx and 7 is also the id off the parameter in the zwave siren.
When you look at my zwave config (see attachment, you can see that my zwave id =15, how is that possible, that this even works!?

Code: Select all

local scriptVar = 'zwaveresponce'
-- url's afvangen met f12 network in zwave panel door beide settings alarm en doorbell af te vangen en te knippen en plakken
-- https://www.base64encode.org/  (encode/decode base = moet %3D zijn

return 
{
	on = {devices = {'sirene_cfg'}},
        
    httpResponses = {scriptVar},
        
    logging =   {level = domoticz.LOG_ERROR},

    execute = function(dz, item)
    _G.logMarker = _G.moduleLabel

    local function zet_zwave_alarm(alarm_type)
        local zwaveURL_start = 'http://192.168.20.35:82/json.htm?type=command&param=applyzwavenodeconfig&idx=7&valuelist='
        local zwaveURL_begin = '7_'
        local zwaveURL_end = '_'
        local alarm_types = { sirene = 'QWxhcm0gbXVzaWM%3D', doorbell = 'RG9vciBiZWxsIG11c2lj',}
        local zwaveURL = zwaveURL_start .. zwaveURL_begin .. alarm_types[alarm_type] .. zwaveURL_end        -- only nr 7 is changed, rest is for so far hard coded
        dz.openURL({url = zwaveURL, callback = scriptVar})
    end

    function applyzwavenodeconfig(dz, deviceidx, parameteridx, value)
        local server = 'http://192.168.20.35:82'
    	local encoded_value = dz.utils.urlEncode(dz.utils.toBase64(value))
	    local url = server .. '/json.htm?type=command&param=applyzwavenodeconfig&idx=' .. tostring(deviceidx) .. '&valuelist=' .. tostring(parameteridx) .. '_' .. encoded_value .. '_'

	    dz.log('Opening URL: '..url)
	    local handle = io.popen('curl "'..url..'"')
	    local result = handle:read("*a")
	    handle:close()
	    dz.log(result)
    end

    if (item.isDevice) then 
        if (item.state == 'sirene') then
            applyzwavenodeconfig(dz, 7, 7, "Alarm music")

            --zet_zwave_alarm('sirene')
        elseif (item.state == 'deurbel') then
            applyzwavenodeconfig(dz, 7, 7, "Door bell music")

            --    zet_zwave_alarm('doorbell')
        elseif (item.state == 'uit') then
            dz.devices('binnenalarm').switchOff()   --.repeatAfterSec(0, 2)
        else
            dz.log('There was a problem handling the state, state: ' .. item.state, dz.LOG_ERROR)
        end
    elseif item.isHTTPResponse and item.ok then -- do i need item.ok ???
            if not(item.isJSON) then item.json = dz.utils.fromJSON(item.data) end
            rt = item.json
            if rt.status == 'OK' then
                -- hier switch aanzetten en log met data bekijken  dit hele script moet een global functie worden
            else
                dz.log('There was a problem handling the request; response is ' .. item.data, dz.LOG_ERROR)
                
            end
    else
            dz.log('There was a problem the device type... ', dz.LOG_ERROR)
    end
    
end
}
zwave.jpg
zwave.jpg (162.82 KiB) Viewed 1025 times
And this is my domoticz database , zwave part that explains more...

zwave_dom.jpg
zwave_dom.jpg (119.81 KiB) Viewed 1030 times
Attachments
sirene.jpg
sirene.jpg (379.06 KiB) Viewed 1032 times
Last edited by pvklink on Monday 03 February 2020 11:04, edited 3 times in total.
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: property zwave encode  [Solved]

Post by waaren »

pvklink wrote: Monday 03 February 2020 10:03 I try to improve my zwave config script. Changing my old function(zet_zwave_alarm) to a new function(applyzwavenodeconfig) that uses logical names instead off codes.
Still get somes errors
toBase64 is a function in domoticz.utils

In this script I use domoticz.utils.toBase64 ( domoticz abbreviated to dz and dz.utils abbreviated to _u )
I also corrected the server var. You mixed up some scripts I guess.

Any specific reason why you use curl and not the native dz.openURL method like you do in function zet_zwave_alarm ?

Code: Select all

local scriptVar = 'zwaveresponse'
-- url's afvangen met f12 network in zwave panel door beide settings alarm en doorbell af te vangen en te knippen en plakken
-- https://www.base64encode.org/  (encode/decode base = moet %3D zijn

return 
{
    on = {devices = {'sirene_cfg'},
        
    httpResponses = {scriptVar}},
        
    logging =   {level = domoticz.LOG_ERROR},

    execute = function(dz, item)
    _G.logMarker = _G.moduleLabel
    local _u = dz.utils

    local function zet_zwave_alarm(alarm_type)
        local zwaveURL_start = 'http://192.168.20.35:82/json.htm?type=command&param=applyzwavenodeconfig&idx=7&valuelist='
        local zwaveURL_begin = '7_'
        local zwaveURL_end = '_'
        local alarm_types = { sirene = 'QWxhcm0gbXVzaWM%3D', doorbell = 'RG9vciBiZWxsIG11c2lj',}
        local zwaveURL = zwaveURL_start .. zwaveURL_begin .. alarm_types[alarm_type] .. zwaveURL_end        -- only nr 7 is changed, rest is for so far hard coded
        dz.openURL({url = zwaveURL, callback = scriptVar})
    end

    function applyzwavenodeconfig(dz, deviceidx, parameteridx, value)
        local server = 'http://192.168.20.35:82' 
        local encoded_value = _u.urlEncode(_u.toBase64(value))
        local url = server .. '/json.htm?type=command&param=applyzwavenodeconfig&idx=' .. tostring(deviceidx) .. '&valuelist=' .. tostring(parameteridx) .. '_' .. encoded_value .. '_'

        dz.log('Opening URL: '..url)
        local handle = io.popen('curl "'..url..'"')
        local result = handle:read("*a")
        handle:close()
        dz.log(result)
    end

    if (item.isDevice) then 
        if (item.state == 'sirene') then
            applyzwavenodeconfig(dz, 7, 7, "Alarm music")

            --zet_zwave_alarm('sirene')
        elseif (item.state == 'deurbel') then
            applyzwavenodeconfig(dz, 7, 7, "Door bell music")

            --    zet_zwave_alarm('doorbell')
        elseif (item.state == 'uit') then
            dz.devices('binnenalarm').switchOff()   --.repeatAfterSec(0, 2)
        else
            dz.log('There was a problem handling the state, state: ' .. item.state, dz.LOG_ERROR)
        end
    elseif item.isHTTPResponse and item.ok then -- do i need item.ok ??? / Yes ! You want to make sure you received a valid response as quickly as possible in the script
            if not(item.isJSON) then item.json = _u.fromJSON(item.data) end
            rt = item.json
            if rt.status == 'OK' then
                -- hier switch aanzetten en log met data bekijken  dit hele script moet een global functie worden
            else
                dz.log('There was a problem handling the request; response is ' .. item.data, dz.LOG_ERROR)
                
            end
    else
            dz.log('There was a problem the device type... ', dz.LOG_ERROR)
    end
    
end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: property zwave encode

Post by pvklink »

Ok, got it working!

and no reason for curl, or native... i am to much a hobbyist. I was focused on the use of a logical name.
So i follow your advise to make it better, see script.

Strange thing is the id of the zwave, i changed my first post (did not saw your reaction in time..)
To make this work i must use the domoticz id of the zwave device and not the id that is shown in zwave itself.
Would be nice if domoticz also shows this ID so i dont have to look in the database instead...

Code: Select all

local scriptVar = 'zwaveresponse'
-- url's afvangen met f12 network in zwave panel door beide settings alarm en doorbell af te vangen en te knippen en plakken
-- https://www.base64encode.org/  (encode/decode base = moet %3D zijn

return 
{
    on = {devices = {'sirene_cfg'},
        
    httpResponses = {scriptVar}},
        
    logging =   {level = domoticz.LOG_ERROR},

    execute = function(dz, item)
    _G.logMarker = _G.moduleLabel
    local _u = dz.utils

    function zet_zwave_cfg(dz, deviceidx, parameteridx, value)
        local server = 'http://192.168.20.35:82' 
        local encoded_value = _u.urlEncode(_u.toBase64(value))
        local myurl = server .. '/json.htm?type=command&param=applyzwavenodeconfig&idx=' .. tostring(deviceidx) .. '&valuelist=' .. tostring(parameteridx) .. '_' .. encoded_value .. '_'
        dz.openURL({url = myurl, callback = scriptVar})
    end

    if (item.isDevice) then 
        if (item.state == 'sirene') then
            zet_zwave_cfg(dz, 7, 7, "Alarm music")

        elseif (item.state == 'deurbel') then
            zet_zwave_cfg(dz, 7, 7, "Door bell music")

        elseif (item.state == 'uit') then
            dz.devices('binnenalarm').switchOff()   --.repeatAfterSec(0, 2)
        else
            dz.log('There was a problem handling the state, state: ' .. item.state, dz.LOG_ERROR)
        end
    elseif item.isHTTPResponse and item.ok then -- do i need item.ok ??? / Yes ! You want to make sure you received a valid response as quickly as possible in the script
            if not(item.isJSON) then item.json = _u.fromJSON(item.data) end
            rt = item.json
            if rt.status == 'OK' then
                -- hier switch aanzetten en log met data bekijken  dit hele script moet een global functie worden
            else
                dz.log('There was a problem handling the request; response is ' .. item.data, dz.LOG_ERROR)
                
            end
    else
            dz.log('There was a problem the device type... ', dz.LOG_ERROR)
    end
    
end
}
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: property zwave encode

Post by pvklink »

can i delete the id's of the zwave devices in the database that are not shown in domoticz zwave screen?
Feels not ok that i have two entries for my pir and two for my radiator...
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest