Page 1 of 1

Read JSON data and update level blind

Posted: Sunday 16 February 2020 19:56
by djdevil
Hi everyone, I'm using a python script to issue commands and a bluethoot roller shutter engine.

The script by running this command:

Code: Select all

curl -i http://192.168.1.29:5000/AM43BlindsAction/CheckStatus
gives me this output

Code: Select all

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 193
Server: Werkzeug/1.0.0 Python/3.7.3
Date: Sun, 16 Feb 2020 18:30:15 GMT

{
    "Tenda bagno": [
        {
            "battery": 78,
            "light": 0,
            "macaddr": "02:A0:07:6A:94:8D",
            "position": 99
        }
    ],
    "status": "OK"
}
is it possible to create a script that extrapolates the given "position" value and updates the idx every 5 minutes? thanks in advance for the help

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 13:14
by djdevil
there is no one you can help me thanks :oops:

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 13:55
by waaren
djdevil wrote: Monday 17 February 2020 13:14 there is no one you can help me thanks :oops:
Sure someone can. But what kind of idx should be updated every 5 minutes ? Is it a switch, or a text device a uservariable maybe ? Please do not expect forum member to read your mind :)

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 14:30
by djdevil
waaren wrote: Monday 17 February 2020 13:55
djdevil wrote: Monday 17 February 2020 13:14 there is no one you can help me thanks :oops:
Sure someone can. But what kind of idx should be updated every 5 minutes ? Is it a switch, or a text device a uservariable maybe ? Please do not expect forum member to read your mind :)
you are right, sorry! :oops:
the idx is Blinds Percentage Inverted

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 15:03
by waaren
djdevil wrote: Monday 17 February 2020 14:30 you are right, sorry! :oops:
the idx is Blinds Percentage Inverted
Could be something like

Code: Select all

local scriptVar = 'blindPosition'

return 
{
    on = 
    {
        timer = 
        {
            'every 5 minutes',
        },

        httpResponses = 
        {
            scriptVar
        },
    },

    logging =   
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.isTimer then
            dz.openURL({
                url = 'http://192.168.1.29:5000/AM43BlindsAction/CheckStatus',
                callback = scriptVar,
            })
            return
        end

        if item.ok then
            local blind = dz.devices('Tenda bagno') -- Change to name of domoticz device 
            local rt = item.json
            local position = rt['Tenda bagno'][1].position
            dz.log('Position of blind is ' .. position, dz.LOG_DEBUG
            blind.dimTo(position).silent()
        else
           dz.log('There was a problem handling the request', dz.LOG_ERROR)
           dz.log(item, dz.LOG_ERROR)
        end
    end
}

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 18:18
by djdevil
Not work this is my degub log:

Code: Select all

2020-02-17 18:18:00.681 Status: dzVents: Info: blindPosition: ------ Start internal script: Script #1:, trigger: every 1 minutes
2020-02-17 18:18:00.681 Status: dzVents: Debug: blindPosition: OpenURL: url = http://192.168.1.29:5000/AM43BlindsAction/CheckStatus
2020-02-17 18:18:00.681 Status: dzVents: Debug: blindPosition: OpenURL: method = GET
2020-02-17 18:18:00.681 Status: dzVents: Debug: blindPosition: OpenURL: post data = nil
2020-02-17 18:18:00.681 Status: dzVents: Debug: blindPosition: OpenURL: headers = nil
2020-02-17 18:18:00.681 Status: dzVents: Debug: blindPosition: OpenURL: callback = blindPosition
2020-02-17 18:18:00.681 Status: dzVents: Info: blindPosition: ------ Finished Script #1
2020-02-17 18:18:03.265 Status: dzVents: Info: blindPosition: ------ Start internal script: Script #1: HTTPResponse: "blindPosition"
2020-02-17 18:18:03.284 Status: dzVents: Debug: blindPosition: Processing device-adapter for Tenda Bagno: Switch device adapter
2020-02-17 18:18:03.284 Status: dzVents: Error (2.4.19): blindPosition: An error occured when calling event handler Script #1
2020-02-17 18:18:03.284 Status: dzVents: Error (2.4.19): blindPosition: ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:37: attempt to index local 'rt' (a nil value)
2020-02-17 18:18:03.284 Status: dzVents: Info: blindPosition: ------ Finished Script #1

Re: Read JSON data and update level blind  [Solved]

Posted: Monday 17 February 2020 19:53
by waaren
djdevil wrote: Monday 17 February 2020 18:18 Not work this is my debug log:
2020-02-17 18:18:03.284 Status: dzVents: Error (2.4.19): blindPosition: ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:37: attempt to index local 'rt' (a nil value)
Probably the response of your Bluetooth does not tell domoticz it returns a json. Can you try this one ?

Code: Select all

local scriptVar = 'blindPosition'

return 
{
    on = 
    {
        timer = 
        {
            'every 5 minutes',
        },

        httpResponses = 
        {
            scriptVar
        },
    },

    logging =   
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.isTimer then
            dz.openURL({
                url = 'http://192.168.1.29:5000/AM43BlindsAction/CheckStatus',
                callback = scriptVar,
            })
            return
        end

        if item.ok then
            local blind = dz.devices('Tenda bagno') -- Change to name of domoticz device 
            local rt = dz.utils.fromJSON(item.data)
            if rt == nil then 
                dz.log(item, dz.LOG_ERROR) 
                return 
            end
            local position = rt['Tenda bagno'][1].position
            dz.log('Position of blind is ' .. position, dz.LOG_DEBUG
            blind.dimTo(position).silent()
        else
           dz.log('There was a problem handling the request', dz.LOG_ERROR)
           dz.log(item, dz.LOG_ERROR)
        end
    end
}

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 20:09
by djdevil
:D it work

Code: Select all

 2020-02-17 20:07:02.000 Status: dzVents: Info: blindPosition: ------ Start internal script: Script #1: HTTPResponse: "blindPosition"
2020-02-17 20:07:02.019 Status: dzVents: Debug: blindPosition: Processing device-adapter for Tenda Bagno: Switch device adapter
2020-02-17 20:07:02.024 Status: dzVents: Debug: blindPosition: Position of blind is 95
2020-02-17 20:07:02.024 Status: dzVents: Debug: blindPosition: Constructed timed-command: Set Level 95
2020-02-17 20:07:02.024 Status: dzVents: Debug: blindPosition: Constructed timed-command: Set Level 95 NOTRIGGER
2020-02-17 20:07:02.024 Status: dzVents: Info: blindPosition: ------ Finished Script #1 


can the 95% position be reversed? actually it would be 5% on the switch

Re: Read JSON data and update level blind

Posted: Monday 17 February 2020 20:21
by djdevil
djdevil wrote: Monday 17 February 2020 20:09 :D it work

Code: Select all

 2020-02-17 20:07:02.000 Status: dzVents: Info: blindPosition: ------ Start internal script: Script #1: HTTPResponse: "blindPosition"
2020-02-17 20:07:02.019 Status: dzVents: Debug: blindPosition: Processing device-adapter for Tenda Bagno: Switch device adapter
2020-02-17 20:07:02.024 Status: dzVents: Debug: blindPosition: Position of blind is 95
2020-02-17 20:07:02.024 Status: dzVents: Debug: blindPosition: Constructed timed-command: Set Level 95
2020-02-17 20:07:02.024 Status: dzVents: Debug: blindPosition: Constructed timed-command: Set Level 95 NOTRIGGER
2020-02-17 20:07:02.024 Status: dzVents: Info: blindPosition: ------ Finished Script #1 


can the 95% position be reversed? actually it would be 5% on the switch
I solved it like this!

Code: Select all

blind.dimTo(101-position).silent()
thanks waaren for the help you have given to me and that you give to the domoticz community every day ;)