Page 1 of 1

Reading setpoint from Shelly TRV

Posted: Tuesday 03 January 2023 12:05
by vspamn
I wrote a script to read the setpoint temp from a shelly valve. It works, I however keep getting the following error in my logs (which is annoying).

Code: Select all

Error: dzVents: Error: (3.1.8) ...scripts/Thermostaat studeerkamer (update connection).lua:39: attempt to index a nil value (field 'target_t')
The error is caused because the field "target_t" is indeed empty. Is there another way to obtain the value of shellyvalve.target_t.value?

Code: Select all

-- DzVents script for update of set temperature Shelly Valve

local IPADDRESS = 'xx'                                            -- IP address
local USER = 'xx'                                                       -- login credentials
local PASSWORD = 'xx'                              -- login credentials
local THERMOSTAAT = 'Thermostaat studeerkamer'                              -- Thermostaat domoticz device name

return {
	on = {
		timer = {
			'every minute'
		},
		httpResponses = {
			'trigger'
		}
	},
	execute = function(dz, item)


		if (item.isTimer) then
			dz.openURL({
				url = 'http://' .. USER .. ':' .. PASSWORD .. '@' .. IPADDRESS .. '/thermostat/0',
				method = 'GET',
				callback = 'trigger',
			})
		end


		if (item.isHTTPResponse) then
			if (item.ok) then

			   -- Parsing JSON data and interpreting data...
			   dz.log('processing data...', dz.LOG_INFO)
			   local shellyvalve = dz.utils.fromJSON(item.data)
			   local setpointshellyvalve = shellyvalve.target_t.value

			   -- Updating domoticz devices
			   dz.devices(THERMOSTAAT).updateSetPoint(setpointshellyvalve).silent()


		    else

		       dz.log('HTTP response error', dz.LOG_ERROR)

	        end
	        


		end

	end
}

Re: Reading setpoint from Shelly TRV

Posted: Tuesday 03 January 2023 23:10
by waltervl
You can check first if value ~= nil then updateSetPoint() to prevent this error.

Re: Reading setpoint from Shelly TRV

Posted: Tuesday 03 January 2023 23:33
by waltervl
Addition, if the value should not sometimes be nil but a real number can jou post the JSON that came out of '/thermostat/0'?

Re: Reading setpoint from Shelly TRV

Posted: Wednesday 04 January 2023 1:33
by HvdW
Screenshot_20230104-013033.png
Screenshot_20230104-013033.png (198.23 KiB) Viewed 795 times
Maybe dump table gives the information you need.

Re: Reading setpoint from Shelly TRV

Posted: Wednesday 04 January 2023 10:20
by vspamn
The JSON format that comes out of '/thermostat/0' is:

Code: Select all

{"pos":100.0,"target_t":{"enabled":true,"value":19.5,"value_op":8.0,"units":"C"},"tmp":{"value":19.5,"units":"C","is_valid":true},"schedule":false,"schedule_profile":1,"boost_minutes":0,"window_open":false}
The dumpTable from Domoticz shows:

Code: Select all

2023-01-04 10:17:01.463 Status: dzVents: > boost_minutes: 0
2023-01-04 10:17:01.463 Status: dzVents: > target_t:
2023-01-04 10:17:01.463 Status: dzVents: > enabled: true
2023-01-04 10:17:01.463 Status: dzVents: > units: C
2023-01-04 10:17:01.464 Status: dzVents: > value: 19.5
2023-01-04 10:17:01.464 Status: dzVents: > value_op: 8.0
2023-01-04 10:17:01.464 Status: dzVents: > window_open: false
2023-01-04 10:17:01.464 Status: dzVents: > schedule_profile: 1
2023-01-04 10:17:01.464 Status: dzVents: > schedule: false
2023-01-04 10:17:01.464 Status: dzVents: > pos: 99.1
2023-01-04 10:17:01.464 Status: dzVents: > tmp:
2023-01-04 10:17:01.464 Status: dzVents: > is_valid: true
2023-01-04 10:17:01.464 Status: dzVents: > value: 19.6
2023-01-04 10:17:01.464 Status: dzVents: > units: C
As you can see, this is exactly my difficulty, I "need" target_t to point to the correct "value", but target_t does not contain a value itself.

Re: Reading setpoint from Shelly TRV

Posted: Wednesday 04 January 2023 10:46
by waltervl
Seems OK though....

What did you dumpTable? shellyvalve or item.json?

What if you change line

Code: Select all

local shellyvalve = dz.utils.fromJSON(item.data)
into

Code: Select all

local shellyvalve = item.json
Edit:
You could also try to use

Code: Select all

local setpointshellyvalve = item.json.target_t.value
and remove the line

Code: Select all

local shellyvalve = dz.utils.fromJSON(item.data)

Re: Reading setpoint from Shelly TRV

Posted: Wednesday 04 January 2023 11:16
by vspamn
I let the dumpTable run for a bit and now see that the data is being mixed up with another script. The dumpTable sometimes shows the correct json data of the Shelly TRV, but sometimes shows the data of another thermostat.

I used the script of the other thermostat (see below for the relevant part of it) as a basis for the Shelly TRV script, but I probably need to differentiate. Any tips how to do so?

Code: Select all

return {
	on = {
		timer = {
			'every minute'
		},
		httpResponses = {
			'trigger'
		}
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'http://' .. USER .. ':' .. PASSWORD .. '@' .. IPADDRESS .. '/protect/data.json?heater=0',
				method = 'GET',
				callback = 'trigger',
			})
		end


		if (item.isHTTPResponse) then
			if (item.ok) then

			   -- Parsing JSON data and interpreting data...
			   domoticz.log('processing data...', domoticz.LOG_INFO)
			   local intergas = domoticz.utils.fromJSON(item.data)
			   local roomtemperature = domoticz.utils.round((intergas.room_temp_1_lsb + intergas.room_temp_1_msb * 256) / 100, 1)

Re: Reading setpoint from Shelly TRV  [Solved]

Posted: Wednesday 04 January 2023 11:21
by waltervl
They both use value "trigger" as a httpRespones/callback value
Use a unique httpResponses/callback value for each script.

Re: Reading setpoint from Shelly TRV

Posted: Wednesday 04 January 2023 11:27
by vspamn
Ah, that did the trick, thanks a lot!