How to get the temperature display units needed for updating the humidity status?

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

Moderator: leecollings

Post Reply
desliem
Posts: 8
Joined: Sunday 04 December 2016 16:51
Target OS: -
Domoticz version: 2022.1
Location: Canada (N.-B.)
Contact:

How to get the temperature display units needed for updating the humidity status?

Post by desliem »

The short humid_status.lua dzVents script shown below updates the humidity status of any Temp + Humidity [ + Baro ] sensor added to the trigger devices list by indirectly using the utils.humidityStatus() function added in V3.0.15. It relies on a user variable named 'TemperatureUnit' to convert, if needed, °F to °C.

Right now, the Lua script, updt_unit.lua, also listed below, uses the "/json.htm?type=settings" API to set the user variable according to the value of "TempUnit" : 0 (for °C) or "TempUnit": 1 (for °F). It is triggered when the system starts and manually with a virtual sensor. I have to remember to activate the virtual switch whenever the temperature display units are changed, which I am sure to forget to do. Presumably the two scripts could be combined, but would increase the execution time by whole 250 ms per sensor update. :o

So finally here are my questions:
  • Is there a quick way to find out which units are used for displaying temperatures?
  • Is there a dzVents trigger available for when the temperature unit setting is modified?
  • Is there a better way to update the humidity status in the first place?
As always, thanks to everyone that contributes to this great project.

Michel


humid_status.lua:

Code: Select all

--[[
Fix up humidity status of "Temp + Humidity" and "Temp + Humidity + Baro" type sensors 
  Add their idx in the devices list below
  Tested with temp+humidity sensors with idx 206 and 208  and  temp+humidity+Baro sensor with idx 207

User variable 'TemperatureUnit' must be 'C' or 'F' indicating unit used to display temperature values
Tested in Domoticz 2023.1, Build f9b9ac774, and dzVents Version: 3.1.8
]]

return 
{
	on = 
	{
		devices = 
		{
			206, 207, 208
		},
	},

	data = {
		updating = { initial = 0 }
	},

	logging = {
        	level = domoticz.LOG_ERROR, -- change LOG_DEBUG to LOG_ERROR when script functions
        	marker = 'fix_humid',
	},

	execute = function(dz, item)
		dz.log('item '..item.name..', type: '..item.deviceType..', idx: '..item.id)
		if dz.data.updating == 0 then
			dz.data.updating = 1
			dz.log('updating item')
			local temp = item.temperature
			if dz.variables('TemperatureUnit').value == 'F' then
				dz.log('Converting '..temp..'°F')
				temp = dz.utils.toCelsius(temp)
				dz.log(' to '..temp..'°C')
			end			
			if string.match(item.deviceType, 'Baro') then
				dz.log('item.updateTempHumBaro')
				item.updateTempHumBaro(temp, item.humidity, dz.HUM_COMPUTE, item.pressure, item.forecast)
			else
				dz.log('item.updateTempHum')
				item.updateTempHum(temp, item.humidity, dz.HUM_COMPUTE)
			end
		elseif dz.data.updating == 1 then
			dz.log('not updating item')
			dz.data.updating = 0
		end
	end
}


updt_unit.lua:

Code: Select all

return {
	on = {
		system = { 'start' },
		devices = { 'Update TemperatureUnit' },
		httpResponses = { 'result' }
	},

	execute = function(dz, item)
		if (item.isSystemEvent) or (item.isDevice) then
			dz.openURL({
				url = dz.settings['Domoticz url'].."/json.htm?type=settings",
				method = 'GET',
				callback = 'result'
		 	})
		end

		if item.isHTTPResponse and item.isJSON then   
			local unit = 'F'
			if item.json.TempUnit == 0 then
				unit = 'C'
			end
			dz.variables('TemperatureUnit').set(unit)
		end
	end
}
User avatar
RonkA
Posts: 100
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: How to get the temperature display units needed for updating the humidity status?

Post by RonkA »

Don't know how to help but just asked chatgpt to combine the 2 scripts with this result:

Code: Select all

return {
    on = {
        devices = { 206, 207, 208 },
        system = { 'start' },
        httpResponses = { 'result' },
        variable = { 'TemperatureUnit' }
    },

    data = {
        updating = { initial = 0 }
    },

    logging = {
        level = domoticz.LOG_ERROR,
        marker = 'fix_humid'
    },

    execute = function(dz, item)
        dz.log('item '..item.name..', type: '..item.deviceType..', idx: '..item.id)

        if item.isSystemEvent or item.isDevice then
            dz.openURL({
                url = dz.settings['Domoticz url'].."/json.htm?type=settings",
                method = 'GET',
                callback = 'result'
            })
        end

        if item.isHTTPResponse and item.isJSON then
            local unit = 'F'
            if item.json.TempUnit == 0 then
                unit = 'C'
            end
            dz.variables('TemperatureUnit').set(unit)
        end

        if dz.data.updating == 0 then
            dz.data.updating = 1
            dz.log('updating item')
            local temp = item.temperature
            if dz.variables('TemperatureUnit').value == 'F' then
                dz.log('Converting '..temp..'°F')
                temp = dz.utils.toCelsius(temp)
                dz.log(' to '..temp..'°C')
            end

            if string.match(item.deviceType, 'Baro') then
                dz.log('item.updateTempHumBaro')
                item.updateTempHumBaro(temp, item.humidity, dz.HUM_COMPUTE, item.pressure, item.forecast)
            else
                dz.log('item.updateTempHum')
                item.updateTempHum(temp, item.humidity, dz.HUM_COMPUTE)
            end
        elseif dz.data.updating == 1 then
            dz.log('not updating item')
            dz.data.updating = 0
        end
    end
}
In this combined script, the on table includes all the events that the script needs to handle: device updates, system events, HTTP responses, and variable updates. The execute function first checks whether the event is a system event or a device update, in which case it sends an HTTP request to get the current temperature unit setting from Domoticz. If the event is an HTTP response and contains the temperature unit setting, it sets the TemperatureUnit variable accordingly. Finally, the script updates the humidity values of the specified devices if the updating flag is not set.

---->Can u make script using dzVents Version: 3.1.8 way to update the humidity status?

Sure, here's an example dzVents script that updates the humidity status of all devices with a Humidity or Barometer device type:

Code: Select all

return {
    on = {
        timer = {
            'every 5 minutes'
        }
    },
    execute = function(domoticz, timer)
        for _, device in ipairs(domoticz.devices) do
            if device.deviceType == 'Humidity' or device.deviceType == 'Barometer' then
                domoticz.log('Updating humidity for device ' .. device.name)
                local temp = device.temperature
                if domoticz.variables('TemperatureUnit').value == 'F' then
                    domoticz.log('Converting ' .. temp .. '°F to °C')
                    temp = domoticz.utils.toCelsius(temp)
                end
                if string.match(device.deviceType, 'Baro') then
                    device.updateTempHumBaro(temp, device.humidity, domoticz.HUM_COMPUTE, device.pressure, device.forecast)
                else
                    device.updateTempHum(temp, device.humidity, domoticz.HUM_COMPUTE)
                end
            end
        end
    end
}
This script uses a timer trigger to run every 5 minutes and updates the humidity status for all devices with a Humidity or Barometer device type. It also checks the TemperatureUnit variable to determine whether to convert the temperature from Fahrenheit to Celsius before updating the humidity status.

Note that this script assumes that you have already defined the TemperatureUnit variable in Domoticz. If you haven't done so, you can create it by going to "Setup" -> "Variables" -> "Add".

Don't know if this is useful but succes with your hunt to an answer.
Greetings Ron.
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How to get the temperature display units needed for updating the humidity status?

Post by waltervl »

desliem wrote: Thursday 23 March 2023 0:16

So finally here are my questions:
  • Is there a quick way to find out which units are used for displaying temperatures?
  • Is there a dzVents trigger available for when the temperature unit setting is modified?
  • Is there a better way to update the humidity status in the first place?
As always, thanks to everyone that contributes to this great project.

Michel
  • Is there a quick way to find out which units are used for displaying temperatures? See your updt_unit.lua as it seems the correct way.
  • Is there a dzVents trigger available for when the temperature unit setting is modified? NO
  • Is there a better way to update the humidity status in the first place? I do not understand this, seems unrelated to C/F display switch.
You could put the function of updt_unit.lua in an helper function. See the documentation: https://www.domoticz.com/wiki/DzVents:_ ... _functions
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
willemd
Posts: 631
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: How to get the temperature display units needed for updating the humidity status?

Post by willemd »

Not an answer but a question: why would you want to check for temperature unit in the first place? Does it ever change? I would expect it is very much determined by the standards used in your country and only set once after installation of Domoticz. (unless your goal is to make a script that the whole world can use...)

I am not aware of another dzvents method to get the temperature unit setting. Reading the documentation it looks like you can only get a few settings directly in dzvents (like location for example). So then a http json call to get the setting and then update a variable or a json call within the combined script are both workable solutions.
desliem
Posts: 8
Joined: Sunday 04 December 2016 16:51
Target OS: -
Domoticz version: 2022.1
Location: Canada (N.-B.)
Contact:

Re: How to get the temperature display units needed for updating the humidity status?

Post by desliem »

Sorry for the late reply - just saw the "Notify me when a reply is posted" checkbox.

@RonkA:
Is chatgpt hell-bent on taking over everything? In any case, that script seems well done, but I won't be trying it because it does require an extra HTTP request each time a temperature is updated.

The script written by a human being is much more interesting. While using a time trigger does not seem appropriate (the device trigger seems better in my estimation) using deviceType to identify when to update the humidity status is better than listing the devices explicitly. I will incorporate your idea.

@wilemd:
Of course you are quite right, setting the temperature units is really a one-time thing that presumably the user does once on setting up Domoticz. But you correctly identified my ambition: the "goal is to make a script that the whole world can use..". Our friends to the south (I live in Canada) need all our help, stuck with their archaic measurement system inherited for 18thC Europe. So after writing my message, I came to my current solution:

Code: Select all

execute = function(dz, item)
                units = 'C'  -- set to 'F' if the display units are set to °Fahrenheit in Domoticz Settings
                .... 
Forget HTTP requests to get the setting, forget a user variable, just let anyone who adopts my magical script set the units when installing it. Or I might just forget "units = 'C'" and instead tell Americans to uncomment a line in the script.

@waltervl:
Did not know about helper functions. I'll have to read the documentation as you suggest and, hopefully, I'll go to bed a little smarter. As for your "I do not understand this, seems unrelated to C/F display switch" comment, I should have been more explicit. There are actually two questions behind my musing on "a better way."

1. Domoticz already calculates the dew point whenever the temperature or the humidity is changed. Would it not make sense that Domoticz update the humidity status at that point?

2. The utils.humidityStatus() function updates the status based on humidity only. It seems that most "experts" think that the dew point is the better, if not very accurate, measure of how humid it feels. I looked at that question back when there was only one sensor in my home and decided that wet, comfortable, dry was a fine enough classification: https://sigmdel.ca/michel/ha/rpi/temp_s ... rh-comfort

Thank you, Ron, Walter and Willem for you much appreciated questions and suggestions.

Cheers, Michel.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest