Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Question

Posted (edited)

This is a Quickapp script for HC3. I am trying to get this to work in a VD on HC2. Is it possible, or do I have to do it together with a scene to make it work?
Can anyone help?

 

function QuickApp:onInit()
    self:updateProperty("unit", "kr/kWh")
    self.refresh       = 60 -- in seconds
    local function loop()
        self:webrequest();
        setTimeout(loop,self.refresh * 1000)
    end
    if self.username ~= "changeme" then loop() end
end

function QuickApp:webrequest()
    local tid = os.date("%R")
    local minutt = os.date("%M")
    
    --OPPDATERER STRØMPRIS OG SNITT HVER TIME
    if (minutt == "00")
        then
        self:OppdaterPris();
        self:OppdaterSnitt();
    end
end

function QuickApp:OppdaterPris()
    --Opprette lokale variable
    local GammelStrompris = fibaro.get(126,"value")
    local NyStrompris

     --Definerer http
    local http = net.HTTPClient();

    --Definerer strengen Payload til å være kommando til tibber
    local payload = '{"query": "{viewer {homes {currentSubscription{priceInfo{current{total}}}}}}", "variables": null, "operationName": null}';

    --API.POST til Tibber--
    http : request('https://api.tibber.com/v1-beta/gql',  {
        options = 
            {
            method = "POST",
            headers = 
                {
                ['Authorization'] = '.........', --Min Tibber ID her.
                ['Content-Type'] = 'application/json'
                },
            data = payload  -- Query.
            },
        success = function(response) 

        --fibaro.debug (response.data)      --Verifiser at en har fått response.data. Dette er data som en har fått fra Tibber
        local data = json.decode(response.data);
        --fjernet local under???
        NyStrompris = json.encode(data.data.viewer.homes[1].currentSubscription.priceInfo.current.total);
        self:updateProperty("value", tonumber(NyStrompris))
        --print("OK")
    end,
         error = function(err) fibaro.debug ("Error:" .. err)
    end
    
});
end

function QuickApp:OppdaterSnitt()
    --Definerer http
    local http = net.HTTPClient();

    --Definerer strengen average til å være kommando til tibber
    local average = '{"query": "{viewer {homes {currentSubscription{priceRating{daily{entries{total}}}}}}}", "variables": null, "operationName": null}';

    --API.POST Til Tibber
    http : request
        ('https://api.tibber.com/v1-beta/gql',  
            {
            options = 
                {
                method = "POST",
                headers = 
                {
                ['Authorization'] = '...............', --Min Tibber ID her.
                ['Content-Type'] = 'application/json'
                },
            data = average  -- Query
            },
        success = function(response)
        --fibaro.debug(response.data)      --Verifiser at en har fått response.data. Dette er data som en har fått fra Tibber
        local data = json.decode(response.data);
        local Nypris = tostring(json.encode(data.data.viewer.homes[1].currentSubscription.priceRating.daily.entries[31].total));
        self:updateProperty("log", tostring(Nypris))
    end,
    }
)
end

Edited by agilje
Remove personal info

1 answer to this question

Recommended Posts

  • 0
Posted

It looks like Tibber, why not use the Tibber QuickApps available on the forum?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...