Read JSON data and update level blind  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Read JSON data and update level blind

Post 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
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Read JSON data and update level blind

Post by djdevil »

there is no one you can help me thanks :oops:
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Read JSON data and update level blind

Post 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 :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Read JSON data and update level blind

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

Re: Read JSON data and update level blind

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Read JSON data and update level blind

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

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

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Read JSON data and update level blind

Post 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
User avatar
djdevil
Posts: 101
Joined: Friday 26 July 2019 18:19
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Contact:

Re: Read JSON data and update level blind

Post 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 ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest