Page 1 of 1

Shelly and dzVents cannot fetch some json data

Posted: Saturday 17 October 2020 19:15
by HvdW
Hi,
I'm trying to extract some data from a shelly 2.5 device
Capture.PNG
Capture.PNG (15.04 KiB) Viewed 756 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 751 times

Re: Cannot fetch some json data

Posted: Saturday 17 October 2020 19:48
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

Re: Cannot fetch some json data  [Solved]

Posted: Saturday 17 October 2020 21:45
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 741 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.

Re: Cannot fetch some json data

Posted: Saturday 17 October 2020 22:00
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.