JSON https puller.

Moderator: leecollings

Post Reply
octy
Posts: 9
Joined: Monday 01 February 2021 17:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

JSON https puller.

Post by octy »

Hello! (first post)

Have been a lot on the forum here and received a lot of help(Thanks :D ), but did not register me until now.

Struggling a bit with how I can solve importing future electricity prices from a JSON online to some sort of custom sensor.
Should have had some input on how to proceed to open "today's file" at 12:05 am and import today's prices from a json one a day:
https://TodaysPrices/?zone=NO4&date=2021-02-01 (url not working)

This is the dataset that comes in response from the address: As you van se its hourly kWh prices for one day updated 1 day in advance.
{"2021-02-01T00:00:00+01:00":{"NOK_per_kWh":0.4411,"valid_from":"2021-02-01T00:00:00+01:00","valid_to":"2021-02-01T01:00:00+01:00"},"2021-02-01T01:00:00+01:00":{"NOK_per_kWh":0.4248,"valid_from":"2021-02-01T01:00:00+01:00","valid_to":"2021-02-01T02:00:00+01:00"},"2021-02-01T02:00:00+01:00":{"NOK_per_kWh":0.4244,"valid_from":"2021-02-01T02:00:00+01:00","valid_to":"2021-02-01T03:00:00+01:00"},"2021-02-01T03:00:00+01:00":{"NOK_per_kWh":0.4149,"valid_from":"2021-02-01T03:00:00+01:00","valid_to":"2021-02-01T04:00:00+01:00"},"2021-02-01T04:00:00+01:00":{"NOK_per_kWh":0.4225,"valid_from":"2021-02-01T04:00:00+01:00","valid_to":"2021-02-01T05:00:00+01:00"},"2021-02-01T05:00:00+01:00":{"NOK_per_kWh":0.5008,"valid_from":"2021-02-01T05:00:00+01:00","valid_to":"2021-02-01T06:00:00+01:00"},"2021-02-01T06:00:00+01:00":{"NOK_per_kWh":0.6160,"valid_from":"2021-02-01T06:00:00+01:00","valid_to":"2021-02-01T07:00:00+01:00"},"2021-02-01T07:00:00+01:00":{"NOK_per_kWh":0.8937,"valid_from":"2021-02-01T07:00:00+01:00","valid_to":"2021-02-01T08:00:00+01:00"},"2021-02-01T08:00:00+01:00":{"NOK_per_kWh":2.0726,"valid_from":"2021-02-01T08:00:00+01:00","valid_to":"2021-02-01T09:00:00+01:00"},"2021-02-01T09:00:00+01:00":{"NOK_per_kWh":1.7186,"valid_from":"2021-02-01T09:00:00+01:00","valid_to":"2021-02-01T10:00:00+01:00"},"2021-02-01T10:00:00+01:00":{"NOK_per_kWh":1.3014,"valid_from":"2021-02-01T10:00:00+01:00","valid_to":"2021-02-01T11:00:00+01:00"},"2021-02-01T11:00:00+01:00":{"NOK_per_kWh":0.6964,"valid_from":"2021-02-01T11:00:00+01:00","valid_to":"2021-02-01T12:00:00+01:00"},"2021-02-01T12:00:00+01:00":{"NOK_per_kWh":0.6846,"valid_from":"2021-02-01T12:00:00+01:00","valid_to":"2021-02-01T13:00:00+01:00"},"2021-02-01T13:00:00+01:00":{"NOK_per_kWh":0.6878,"valid_from":"2021-02-01T13:00:00+01:00","valid_to":"2021-02-01T14:00:00+01:00"},"2021-02-01T14:00:00+01:00":{"NOK_per_kWh":0.8132,"valid_from":"2021-02-01T14:00:00+01:00","valid_to":"2021-02-01T15:00:00+01:00"},"2021-02-01T15:00:00+01:00":{"NOK_per_kWh":0.8923,"valid_from":"2021-02-01T15:00:00+01:00","valid_to":"2021-02-01T16:00:00+01:00"},"2021-02-01T16:00:00+01:00":{"NOK_per_kWh":1.4515,"valid_from":"2021-02-01T16:00:00+01:00","valid_to":"2021-02-01T17:00:00+01:00"},"2021-02-01T17:00:00+01:00":{"NOK_per_kWh":2.0726,"valid_from":"2021-02-01T17:00:00+01:00","valid_to":"2021-02-01T18:00:00+01:00"},"2021-02-01T18:00:00+01:00":{"NOK_per_kWh":1.2339,"valid_from":"2021-02-01T18:00:00+01:00","valid_to":"2021-02-01T19:00:00+01:00"},"2021-02-01T19:00:00+01:00":{"NOK_per_kWh":0.7305,"valid_from":"2021-02-01T19:00:00+01:00","valid_to":"2021-02-01T20:00:00+01:00"},"2021-02-01T20:00:00+01:00":{"NOK_per_kWh":0.6457,"valid_from":"2021-02-01T20:00:00+01:00","valid_to":"2021-02-01T21:00:00+01:00"},"2021-02-01T21:00:00+01:00":{"NOK_per_kWh":0.5842,"valid_from":"2021-02-01T21:00:00+01:00","valid_to":"2021-02-01T22:00:00+01:00"},"2021-02-01T22:00:00+01:00":{"NOK_per_kWh":0.5727,"valid_from":"2021-02-01T22:00:00+01:00","valid_to":"2021-02-01T23:00:00+01:00"},"2021-02-01T23:00:00+01:00":{"NOK_per_kWh":0.5121,"valid_from":"2021-02-01T23:00:00+01:00","valid_to":"2021-02-02T00:00:00+01:00"}}
If anyone has an idea how to get a code`noob to do this, I appreciate it :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON https puller.

Post by waaren »

octy wrote: Monday 01 February 2021 18:17 Struggling a bit with how I can solve importing future electricity prices from a JSON online to some sort of custom sensor.

Should have had some input on how to proceed to open "today's file" at 12:05 am and import today's prices from a json one a day:
https://TodaysPrices/?zone=NO4&date=2021-02-01 (url not working)
If anyone has an idea how to get a code`noob to do this, I appreciate it :)
In dzVents you could start with something like below example.

__________________________________________________________________________________________________________________________
When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
___________________________________________________________________________________________________________________________

Code: Select all

local scriptVar = 'todaysPrices'

return
{
    on =
    {
        timer =
        {
            'every 5 minutes' -- just an example to trigger the request
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        if item.isTimer then
            dz.openURL(
            {
                url = 'https://todaysprices/?zone=NO4&date=' .. dz.time.rawDate
                callback = scriptVar,
            })
        elseif item.isHTTPResponse and item.json then

            dz.utils.dumpTable(item.json)
            -- local someValue = item.json.someValue  -- just an example and depending on the structure of the data

            -- update some device in Domoticz
            -- domoticz.devices('myTextDevice').updateText(someValue)

        else
            dz.log('There was a problem handling the request. Statuscode: ' .. (item.statusCode or '-') , dz.LOG_ERROR)
            dz.log(item, dz.LOG_DEBUG)
        end

    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
octy
Posts: 9
Joined: Monday 01 February 2021 17:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON https puller.

Post by octy »

Gave up some time, but now I`m stuck again in dzVents. (move to dzVents forum maybe?)

Code: Select all

return {
	on = {
		timer = {
			'every 1 minutes' -- just an example to trigger the request
		},
		httpResponses = {
			'trigger' -- must match with the callback passed to the openURL command
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
		    local today = os.date('%Y-%m-%d')
			domoticz.openURL({
				url = 'https://xxxxx/?zone=NO4&date=' .. today ,
				method = 'GET',
				callback = 'trigger', -- see httpResponses above.
			})
		end

		if (item.isHTTPResponse) then

			if (item.ok) then
				if (item.isJSON) then
                    local hournow = os.date('%Y-%m-%dT%H:00:00+01:00')  -- Returns 2021-11-30T02:00:00+01:00 that seems right
					
					local someValue = item.json.hournow['NOK_per_kWh'];
					
					-- update some device in Domoticz
					domoticz.devices('Elprice now').updateText(someValue)
					
				end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end

		end

	end
}
returns
Error: dzVents: Error: (3.1.7) template: ...moticz/scripts/dzVents/generated_scripts/Get_kWh_Lua.lua:32: attempt to index a nil value (field 'hournow')

I'm not sure how to get the hourly value i want ( local someValue = item.json.hournow['NOK_per_kWh']; )

Any pointers, anyone?
Last edited by octy on Thursday 31 March 2022 13:30, edited 1 time in total.
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: JSON https puller.

Post by waltervl »

Can you post an example Json? Without it is a little bit difficult to help as the structure of the Json is important.
Edit. Sorry, did not read the first post correctly...
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: JSON https puller.

Post by waltervl »

Please try:

local someValue = item.json.hournow.NOK_per_kWh;
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
octy
Posts: 9
Joined: Monday 01 February 2021 17:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON https puller.

Post by octy »

Thanks, I get same thing

2021-11-30 11:44:00.623 Status: dzVents: Debug: - OpenURL = {["_trigger"]="trigger", ["URL"]="https://xxxx/?zone=NO4&date=2021-11-30", ["method"]="GET"}
2021-11-30 11:44:03.068 Status: dzVents: Debug: - HTTPResponse: trigger
2021-11-30 11:44:03.132 Status: dzVents: Info: Handling httpResponse-events for: "trigger"
2021-11-30 11:44:03.132 Status: dzVents: Info: template: ------ Start internal script: kWh_dzEvents: HTTPResponse: "trigger"
2021-11-30 11:44:03.148 Status: dzVents: Info: template: ------ Finished kWh_dzEvents
2021-11-30 11:44:03.148 Status: dzVents: !Info: template: Debug: Writing module summary to /home/pi/domoticz/scripts/dzVents/module.log
2021-11-30 11:44:03.147 Error: dzVents: Error: (3.1.7) template: An error occurred when calling event handler kWh_dzEvents
2021-11-30 11:44:03.148 Error: dzVents: Error: (3.1.7) template: ...oticz/scripts/dzVents/generated_scripts/kWh_dzEvents.lua:33: attempt to index a nil value (field 'hournow')
Last edited by octy on Thursday 31 March 2022 13:29, edited 1 time in total.
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: JSON https puller.

Post by boum »

I think you are looking for this syntax:

Code: Select all

local someValue = item.json[hournow].NOK_per_kWh;
hournow is a string variable. To dereference the variable you need to access using the array square bracket [] operator.
json.hournow is equivalent to json["hournow"] and no such key exist in the JSON.
octy
Posts: 9
Joined: Monday 01 February 2021 17:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON https puller.

Post by octy »

You are my hero :D
Worked like a charm, thanks for the help guys :!:
Janssuhhh
Posts: 3
Joined: Wednesday 19 June 2019 14:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON https puller.

Post by Janssuhhh »

octy wrote: Tuesday 30 November 2021 12:06 You are my hero :D
Worked like a charm, thanks for the help guys :!:
Hey,

I am trying the same thing (for some other electricity company), where I also want to load the values for the next 24 hours into a custom sensor.
Did you manage to do this? Or are you only adding the value of the current hour?

Could you share your working solution :-) ?

Cheers!
octy
Posts: 9
Joined: Monday 01 February 2021 17:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON https puller.

Post by octy »

I did get it every hour, but the source shut down unfortunately.
Wold like to collect it all every day, but cant get it to work yet
funny0frank
Posts: 5
Joined: Friday 20 October 2017 8:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: JSON https puller.

Post by funny0frank »

Hi!

I'm the creator of https://norway-power.ffail.win, the source didn't shut down, you now get a 401 error message telling you that you need to send a email to get a free API-key. Just open https://norway-power.ffail.win/?zone=NO ... 2014-12-12 in a browser to see the email address.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest