Reading setpoint from Shelly TRV  [Solved]

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

Moderator: leecollings

Post Reply
vspamn
Posts: 8
Joined: Thursday 02 April 2020 21:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Reading setpoint from Shelly TRV

Post 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
}
User avatar
waltervl
Posts: 5902
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV

Post by waltervl »

You can check first if value ~= nil then updateSetPoint() to prevent this error.
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: 5902
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV

Post 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'?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
HvdW
Posts: 617
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Reading setpoint from Shelly TRV

Post by HvdW »

Screenshot_20230104-013033.png
Screenshot_20230104-013033.png (198.23 KiB) Viewed 785 times
Maybe dump table gives the information you need.
Bugs bug me.
vspamn
Posts: 8
Joined: Thursday 02 April 2020 21:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV

Post 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.
User avatar
waltervl
Posts: 5902
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV

Post 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)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
vspamn
Posts: 8
Joined: Thursday 02 April 2020 21:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV

Post 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)
User avatar
waltervl
Posts: 5902
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV  [Solved]

Post by waltervl »

They both use value "trigger" as a httpRespones/callback value
Use a unique httpResponses/callback value for each script.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
vspamn
Posts: 8
Joined: Thursday 02 April 2020 21:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: Reading setpoint from Shelly TRV

Post by vspamn »

Ah, that did the trick, thanks a lot!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest