Page 1 of 1

Script not Working - Getting Temp from a Sensor

Posted: Monday 28 September 2020 22:14
by SiberianTiger
Hi Guys (and Girls?)

I have a "MyStrom" Switch with integrated Temp-Sensor. I would like to get that Data from the Switch and pass it to a Virtual Sensor in Domoticz.

MyStrom Switch API: https://api.mystrom.ch/#982cf1bb-c873-4 ... dfa51e1afe

I have created a Dummy and a Virtual Temperatur Sensor called "Temp Balkon"

Then i searched trough the wiki an came out with this Script below:
It doesn't give me an error in the log, but also the Temperatur does not get Updated it shows only 0°C.
The Output of the Integrated Sensor looks like this:

Code: Select all

{"measured":21.3125,"compensation":11,"compensated":10.3125}
My Script to get the Data is that:

Code: Select all

return {
	on = {
		timer = {
			'every 5 seconds' -- just an example to trigger the request
		},
		httpResponses = {
			'TempBalkon' -- must match with the callback passed to the openURL command
		}
	},
	execute = function(domoticz, item)

		if (item.isTimer) then
			domoticz.openURL({
				url = 'xxx.xxx.xxx.xxx',
				method = 'GET /temp',
				callback = 'TempBalkon', -- see httpResponses above.
			})
		end

		if (item.isHTTPResponse) then

			if (item.ok) then
				if (item.isJSON) then

					local TempBalkon = item.json.compensated  -- just an example

					-- update some device in Domoticz
					domoticz.devices('TempBalkon').updatetemperatur(item.json.compensated)
				end
			else
				domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
				domoticz.log(item, domoticz.LOG_ERROR)
			end

		end

	end
}
What did i wrong? Sorry im new to this whole scripting thing, but i tought that i could make this with reading trought the Wiki...

Thanks in Advance

Re: Script not Working - Getting Temp from a Sensor

Posted: Monday 28 September 2020 23:06
by waaren
SiberianTiger wrote: Monday 28 September 2020 22:14 Then i searched trough the wiki an came out with this Script below:
It doesn't give me an error in the log, but also the Temperatur does not get Updated it shows only 0°C.

What did i wrong? Sorry im new to this whole scripting thing, but i tought that i could make this with reading trought the Wiki...
See my remarks in below script.

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute', -- timer can not be set to a higher frequency then once a minute.
        },

        httpResponses =
        {
            'TempBalkon', -- must match with the callback passed to the openURL command
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all ok
        marker = 'Balcony temperature',
    },

    execute = function(domoticz, item)

        if item.isTimer then
            domoticz.openURL(
            {
                url = '192.168.178.30',
                method = 'GET', -- methods can only be GET, POST, PUT and DELETE
                callback = 'TempBalkon', -- see httpResponses above.
            })
        elseif item.isHTTPResponse then
            if item.ok and item.isJSON then
                    local TempBalkon = item.json.compensated  -- just an example

                    -- update some device in Domoticz
                    domoticz.devices('TempBalkon').updateTemperature(item.json.compensated) -- methods are case sensitive
            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log(item, domoticz.LOG_DEBUG)
            end
        else
            domoticz.log('Kind of unexpected..', domoticz.LOG_ERROR)
            domoticz.log(item, domoticz.LOG_DEBUG)
        end

    end
}

Re: Script not Working - Getting Temp from a Sensor  [Solved]

Posted: Monday 28 September 2020 23:23
by SiberianTiger
Perfect! Simply Perfect! Thanks Man... With your Tips and the Error log i found the Problems!

The now working script is this:

Code: Select all

return
{
    on =
    {
        timer =
        {
            'every minute', -- timer can not be set to a higher frequency then once a minute.
        },

        httpResponses =
        {
            'TempBalkon', -- must match with the callback passed to the openURL command
        },
    },

    logging =
    {
        level = domoticz.LOG_ERROR, -- change to domoticz.LOG_ERROR when all ok
        marker = 'Balcony temperature',
    },

    execute = function(domoticz, item)

        if item.isTimer then
            domoticz.openURL(
            {
                url = 'xxx.xxx.xxx.xxx/temp',
                method = 'GET', -- methods can only be GET, POST, PUT and DELETE
                callback = 'TempBalkon', -- see httpResponses above.
            })
        elseif item.isHTTPResponse then
            if item.ok and item.isJSON then
                    local TempBalkon = item.json.compensated  -- just an example

                    -- update some device in Domoticz
                    domoticz.devices(17).updateTemperature(item.json.compensated) -- methods are case sensitive
            else
                domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
                domoticz.log(item, domoticz.LOG_DEBUG)
            end
        else
            domoticz.log('Kind of unexpected..', domoticz.LOG_ERROR)
            domoticz.log(item, domoticz.LOG_DEBUG)
        end

    end
}