Shelly and dzVents cannot fetch some json data  [Solved]

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

Moderator: leecollings

Post Reply
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Shelly and dzVents cannot fetch some json data

Post by HvdW »

Hi,
I'm trying to extract some data from a shelly 2.5 device
Capture.PNG
Capture.PNG (15.04 KiB) Viewed 753 times
The script executing leaves me with some questions.

Code: Select all

-- Shelly (get watt)
-- Howto at Luftdaten https://www.domoticz.com/forum/viewtopic.php?f=72&t=23406&hilit=luftdaten
-- Call Shelly with http://192.168.x.x/status

return {
	active = true,
	on = {
		timer = { 'every minute' },            -- every minute for testing, 10 minutes when active
		httpResponses = { 'ShellyRetrieved' }  -- matches callback string below
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://192.168.2.20/status',
				method = 'GET',
				callback = 'ShellyRetrieved'
			})

		elseif (item.isHTTPResponse) then
			if (item.ok and item.isJSON) then 
                --domoticz.devices('Shell').updateCustomSensor(item.json.meters[0].power)              -- Shelly-test.lua:22: attempt to index a nil value (field '?')
				domoticz.devices('Shell').updateCustomSensor(item.json.meters[1].power)                -- zero response instead of the value
				domoticz.notify('Power     ' .. item.json.meters[1].power, domoticz.PRIORITY_HIGH)     -- zero response
				--domoticz.notify('Is_valid  ' .. item.json.meters[1].is_valid, domoticz.PRIORITY_LOW) --howto display TRUE or FALSE?
				domoticz.log(item.json.meters[1].is_valid, domoticz.LOG_ERROR)                         -- displays correct as LOG_ERROR
				domoticz.notify('Timestamp ' .. item.json.meters[1].timestamp, domoticz.PRIORITY_LOW)  -- success
			else
				-- oops
				domoticz.log('Error fetching Shelly data', domoticz.LOG_ERROR)
				domoticz.log(item.data, domoticz.LOG_ERROR)
			end
		end
	end
}
I checked for 2 devices, one with power consumption of 4.26 Watt and the other with power consumption 206.95 Watt
The first log:

Code: Select all

 2020-10-17 19:05:00.496 Status: dzVents: Info: ------ Start internal script: Shelly-test:, trigger: "every minute"
2020-10-17 19:05:00.497 Status: dzVents: Info: ------ Finished Shelly-test
2020-10-17 19:05:00.498 Status: EventSystem: Script event triggered: /home/rpi1/domoticz/dzVents/runtime/dzVents.lua
2020-10-17 19:05:01.420 Notification sent (browser) => Success
2020-10-17 19:05:01.421 Notification sent (browser) => Success
2020-10-17 19:05:01.110 Status: dzVents: Info: Handling httpResponse-events for: "ShellyRetrieved"
2020-10-17 19:05:01.111 Status: dzVents: Info: ------ Start internal script: Shelly-test: HTTPResponse: "ShellyRetrieved"
2020-10-17 19:05:01.204 Status: dzVents: Info: ------ Finished Shelly-test
2020-10-17 19:05:01.205 Status: EventSystem: Script event triggered: /home/rpi1/domoticz/dzVents/runtime/dzVents.lua
2020-10-17 19:05:01.404 Status: Notification: Power 4.26
2020-10-17 19:05:01.405 Status: Notification: Timestamp 1602961500 
The second log shows power consumption 0 instad of 206.95

Code: Select all

 2020-10-17 19:10:00.644 Status: dzVents: Info: ------ Start internal script: Shelly-test:, trigger: "every 5 minutes"
2020-10-17 19:10:00.645 Status: dzVents: Info: ------ Finished Shelly-test
2020-10-17 19:10:00.646 Status: EventSystem: Script event triggered: /home/rpi1/domoticz/dzVents/runtime/dzVents.lua
2020-10-17 19:10:01.633 Notification sent (browser) => Success
2020-10-17 19:10:01.634 Notification sent (browser) => Success
2020-10-17 19:10:01.298 Status: dzVents: Info: Handling httpResponse-events for: "ShellyRetrieved"
2020-10-17 19:10:01.298 Status: dzVents: Info: ------ Start internal script: Shelly-test: HTTPResponse: "ShellyRetrieved"
2020-10-17 19:10:01.483 Status: dzVents: Info: ------ Finished Shelly-test
2020-10-17 19:10:01.486 Status: EventSystem: Script event triggered: /home/rpi1/domoticz/dzVents/runtime/dzVents.lua
2020-10-17 19:10:01.607 Status: Notification: Power 0.0
2020-10-17 19:10:01.607 Status: Notification: Timestamp 1602961801 
This 206.95 cannot be fetched whilst the Voltage 231.66 is displayed without a problem.

Then there is this other question on how to fetch data from unit [0] (part of script)

Code: Select all

domoticz.devices('Shell').updateCustomSensor(item.json.meters[0].power)
      -- Shelly-test.lua:22: attempt to index a nil value (field '?')
Capture2.PNG
Capture2.PNG (7.46 KiB) Viewed 748 times
Last edited by HvdW on Saturday 17 October 2020 22:36, edited 1 time in total.
Bugs bug me.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Cannot fetch some json data

Post by waaren »

HvdW wrote: Saturday 17 October 2020 19:15 I'm trying to extract some data from a shelly 2.5 device
...
Then there is this other question on how to fetch data from unit [0] (part of script)
I you insert below line you will get the complete table as it is translated by dzVents from the JSON return. That might help in identify the issue(s)

Code: Select all

domoticz.utils.dumpTable(item.json)
directly after the line

Code: Select all

if (item.ok and item.isJSON) then
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Cannot fetch some json data  [Solved]

Post by HvdW »

Ha!

Code: Select all

  2020-10-17 21:32:07.861 Status: Incoming connection from: 192.168.2.15
2020-10-17 21:35:00.737 Status: dzVents: Info: ------ Start internal script: Shelly-test:, trigger: "every 5 minutes"
2020-10-17 21:35:00.738 Status: dzVents: Info: ------ Finished Shelly-test
2020-10-17 21:35:00.739 Status: EventSystem: Script event triggered: /home/rpi1/domoticz/dzVents/runtime/dzVents.lua
2020-10-17 21:35:01.356 Notification sent (browser) => Success
2020-10-17 21:35:01.357 Notification sent (browser) => Success
2020-10-17 21:35:01.179 Status: dzVents: Info: Handling httpResponse-events for: "ShellyRetrieved"
2020-10-17 21:35:01.180 Status: dzVents: Info: ------ Start internal script: Shelly-test: HTTPResponse: "ShellyRetrieved"
2020-10-17 21:35:01.192 Status: dzVents: > time: 21:35
2020-10-17 21:35:01.192 Status: dzVents: > ram_total: 49504
2020-10-17 21:35:01.192 Status: dzVents: > actions_stats:
2020-10-17 21:35:01.192 Status: dzVents: > skipped: 0
2020-10-17 21:35:01.193 Status: dzVents: > wifi_sta:
2020-10-17 21:35:01.193 Status: dzVents: > ip: 192.168.2.21
2020-10-17 21:35:01.193 Status: dzVents: > rssi: -78
2020-10-17 21:35:01.193 Status: dzVents: > ssid: ZIGGO_3AE24
2020-10-17 21:35:01.193 Status: dzVents: > connected: true
2020-10-17 21:35:01.194 Status: dzVents: > meters:
2020-10-17 21:35:01.194 Status: dzVents: > 1:
2020-10-17 21:35:01.194 Status: dzVents: > power: 0.0
2020-10-17 21:35:01.194 Status: dzVents: > total: 0
2020-10-17 21:35:01.194 Status: dzVents: > is_valid: true
2020-10-17 21:35:01.194 Status: dzVents: > counters:
2020-10-17 21:35:01.194 Status: dzVents: > 1: 0.0
2020-10-17 21:35:01.195 Status: dzVents: > 2: 0.0
2020-10-17 21:35:01.195 Status: dzVents: > 3: 0.0
2020-10-17 21:35:01.195 Status: dzVents: > timestamp: 1602970501
2020-10-17 21:35:01.195 Status: dzVents: > overpower: 0.0
2020-10-17 21:35:01.195 Status: dzVents: > 2:
2020-10-17 21:35:01.195 Status: dzVents: > power: 145.54
2020-10-17 21:35:01.195 Status: dzVents: > total: 359
2020-10-17 21:35:01.196 Status: dzVents: > is_valid: true
2020-10-17 21:35:01.196 Status: dzVents: > counters:
2020-10-17 21:35:01.196 Status: dzVents: > 1: 145.492
2020-10-17 21:35:01.196 Status: dzVents: > 2: 145.339
2020-10-17 21:35:01.196 Status: dzVents: > 3: 68.754
2020-10-17 21:35:01.196 Status: dzVents: > timestamp: 1602970501
2020-10-17 21:35:01.196 Status: dzVents: > overpower: 0.0
2020-10-17 21:35:01.197 Status: dzVents: > overtemperature: false
2020-10-17 21:35:01.197 Status: dzVents: > uptime: 2226
2020-10-17 21:35:01.197 Status: dzVents: > voltage: 237.23
2020-10-17 21:35:01.197 Status: dzVents: > inputs:
2020-10-17 21:35:01.197 Status: dzVents: > 1:
2020-10-17 21:35:01.197 Status: dzVents: > event_cnt: 0
2020-10-17 21:35:01.198 Status: dzVents: > event:
2020-10-17 21:35:01.198 Status: dzVents: > input: 0
2020-10-17 21:35:01.198 Status: dzVents: > 2:
2020-10-17 21:35:01.198 Status: dzVents: > event_cnt: 0
2020-10-17 21:35:01.198 Status: dzVents: > event:
2020-10-17 21:35:01.198 Status: dzVents: > input: 0
2020-10-17 21:35:01.198 Status: dzVents: > fs_size: 233681
2020-10-17 21:35:01.199 Status: dzVents: > fs_free: 147839
2020-10-17 21:35:01.199 Status: dzVents: > mac: D8F15B131D0F
2020-10-17 21:35:01.199 Status: dzVents: > unixtime: 1602970501
2020-10-17 21:35:01.199 Status: dzVents: > temperature: 50.96
2020-10-17 21:35:01.199 Status: dzVents: > ram_free: 37124
2020-10-17 21:35:01.199 Status: dzVents: > update:
2020-10-17 21:35:01.199 Status: dzVents: > old_version: 20200827-065456/v1.8.3@4a8bc427
2020-10-17 21:35:01.200 Status: dzVents: > new_version: 20200827-065456/v1.8.3@4a8bc427
2020-10-17 21:35:01.200 Status: dzVents: > has_update: false
2020-10-17 21:35:01.200 Status: dzVents: > status: idle
2020-10-17 21:35:01.200 Status: dzVents: > serial: 48
2020-10-17 21:35:01.200 Status: dzVents: > tmp:
2020-10-17 21:35:01.200 Status: dzVents: > is_valid: true
2020-10-17 21:35:01.200 Status: dzVents: > tF: 123.72
2020-10-17 21:35:01.200 Status: dzVents: > tC: 50.96
2020-10-17 21:35:01.201 Status: dzVents: > cfg_changed_cnt: 0
2020-10-17 21:35:01.201 Status: dzVents: > relays:
2020-10-17 21:35:01.201 Status: dzVents: > 1:
2020-10-17 21:35:01.201 Status: dzVents: > timer_started: 0
2020-10-17 21:35:01.201 Status: dzVents: > timer_remaining: 0
2020-10-17 21:35:01.201 Status: dzVents: > overpower: false
2020-10-17 21:35:01.201 Status: dzVents: > timer_duration: 0
2020-10-17 21:35:01.201 Status: dzVents: > source: input
2020-10-17 21:35:01.202 Status: dzVents: > overtemperature: false
2020-10-17 21:35:01.202 Status: dzVents: > is_valid: true
2020-10-17 21:35:01.202 Status: dzVents: > has_timer: false
2020-10-17 21:35:01.202 Status: dzVents: > ison: false
2020-10-17 21:35:01.202 Status: dzVents: > 2:
2020-10-17 21:35:01.202 Status: dzVents: > timer_started: 0
2020-10-17 21:35:01.202 Status: dzVents: > timer_remaining: 0
2020-10-17 21:35:01.203 Status: dzVents: > overpower: false
2020-10-17 21:35:01.203 Status: dzVents: > timer_duration: 0
2020-10-17 21:35:01.203 Status: dzVents: > source: http
2020-10-17 21:35:01.203 Status: dzVents: > overtemperature: false
2020-10-17 21:35:01.203 Status: dzVents: > is_valid: true
2020-10-17 21:35:01.203 Status: dzVents: > has_timer: false
2020-10-17 21:35:01.203 Status: dzVents: > ison: true
2020-10-17 21:35:01.203 Status: dzVents: > mqtt:
2020-10-17 21:35:01.204 Status: dzVents: > connected: false
2020-10-17 21:35:01.204 Status: dzVents: > has_update: false
2020-10-17 21:35:01.204 Status: dzVents: > cloud:
2020-10-17 21:35:01.204 Status: dzVents: > enabled: false
2020-10-17 21:35:01.204 Status: dzVents: > connected: false
2020-10-17 21:35:01.282 Status: dzVents: Info: ------ Finished Shelly-test
2020-10-17 21:35:01.283 Status: EventSystem: Script event triggered: /home/rpi1/domoticz/dzVents/runtime/dzVents.lua
2020-10-17 21:35:01.339 Status: Notification: Power 0.0
2020-10-17 21:35:01.340 Status: Notification: Timestamp 1602970501  
It simply skips Power because
Capture.PNG
Capture.PNG (7.16 KiB) Viewed 738 times
Meters: 0: Power: is translated to domoticz.devices('Shell').updateCustomSensor(item.json.meters[1].power)
and
Meters: 1: Power is translated to domoticz.devices('Shell').updateCustomSensor(item.json.meters[2].power)
Which of course explains why domoticz.devices('Shell').updateCustomSensor(item.json.meters[0].power) delivers
Shelly-test.lua:22: attempt to index a nil value (field '?')

The reason why this 4.26 Watt device responded is because that one has both lights connected, so
domoticz.devices('Shell').updateCustomSensor(item.json.meters[1].power)
does indeed return a value.

Thanks @waaren for the

Code: Select all

domoticz.utils.dumpTable(item.json)
suggestion.
Bugs bug me.
HvdW
Posts: 664
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Cannot fetch some json data

Post by HvdW »

To conclude this item.

What I am trying to achieve is getting Watt data from charging the electric bike batteries.
LiIon batteries tend to have a longer lifespan when not being fully charged.
By the time the battery is (90% maybe 95%) charged the waatage drops.
The Shelly switch sends the data to Domoticz and by the time the current drops the charging will be cut.
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest