Page 1 sur 2

[Fermé] Widget - Status des devices gérés par le plugin

Publié : 20 déc. 2023, 22:34
par hairv
Salut à tous,

comme j'ai quelques soucis de stabilité en ce moment je me suis fait un petit widget alimenté par un script qui donne le nombre de devices Live / Lost / Undefined. Pour cela il interroge l'API du plugin.

J'ai créé un capteur virtuel de type "Alerte" afin d'avoir un visuel couleur.
Image

Code : Tout sélectionner


local Alertidx = 126 -- Change to your idx for the Virtual Alert sensor you have created for this script
local devicesLive=0
local devicesLost=0
local devicesUndefined=0
local urlWebUI = "http://192.168.1.68" -- Change to your local ip (127.0.0.1 most of the time)
local portWebUI = "9440"  -- Change WebUI port 
local apiPath = '/rest-z4d/1/zdevice-name'

-- no changes needed below this section (except timer if you want to change frequency)

return {
	logging = {
	    level = domoticz.LOG_ERROR,
		marker = '(Zigate Status)'
	},
	on = {
		timer =  {'every 15 minutes'},   
		httpResponses = {'triggerSA'} -- must match with the callback passed to the openURL command
    	},
	execute = function(domoticz, item)

	if (item.isTimer) then
	   domoticz.devices(Alertidx).updateAlertSensor(domoticz.ALERTLEVEL_GREY, "Requesting ...")
	    domoticz.openURL({
			url = urlWebUI .. ':' .. portWebUI .. apiPath,
			method = 'GET',
			callback = 'triggerSA' -- see httpResponses above.
		})
	end

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

				local result_table = item.json
				local tc = #result_table
				
				for i = 1, tc do
				    if result_table[i].Health == "Live" then
                        devicesLive=devicesLive+1
                    elseif result_table[i].Health == "Not Reachable" then
                        devicesLost=devicesLost+1
                    else
                        devicesUndefined=devicesUndefined+1
                    end
                end
				
				
				if devicesLost == 0 and devicesUndefined==0 then
				    level = domoticz.ALERTLEVEL_GREEN 	-- domoticz.ALERTLEVEL_GREY, ALERTLEVEL_GREEN, ALERTLEVEL_YELLOW, ALERTLEVEL_ORANGE, ALERTLEVEL_RED
				    AlertText = devicesLive	
				elseif devicesLost==0 then
				    level = domoticz.ALERTLEVEL_ORANGE 	
				    AlertText = devicesUndefined
				else 
				    level = domoticz.ALERTLEVEL_RED 	
				    AlertText = devicesLost
                end
                AlertText="Live: " .. devicesLive .. "\nUnknown: " .. devicesUndefined .. "\nLost: " ..devicesLost 
				-- update device in Domoticz
				domoticz.devices(Alertidx).updateAlertSensor(level, AlertText)
			end
		else
			domoticz.log('There was a problem handling the request' .. url, domoticz.LOG_ERROR)

		end

	end
end
}
Seul bémol l'objet "Alerte" n'est pas reconnu dans HomeHabit, si quelqu'un sait comment l'intégrer je suis preneur.

Re: Widget - Status des devices gérés par le plugin

Publié : 20 déc. 2023, 23:04
par Keros
Merci pour ce script.

Tu pourrais nous le partager sur le wiki : https://github.com/zigbeefordomoticz/wi ... er/Contrib ?

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 09:09
par hairv
Ok, je vais faire ça après avoir légèrement modifié le script pour prendre en compte user/password.

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 09:40
par Keros
Top, merci :)

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 10:26
par hairv
C'est fait, j'ai créé une pull request (je pense que c'est ce qu'il fallait faire :geek: )

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 10:40
par DiliaK
Ah merci !
Je vais tester.

Le script se met dans PLUS D'OPTIONS/ EVENEMENTS ?
Quel type de script choisir ?

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 10:58
par Keros
Merci hairv, je vais le merger ;)

@Diliak, c'est du DzVents > pas besoin de type. C'est défini dans la partie On={}

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 11:00
par hairv
Bonne question en effet : Plus d'options / événements / dzVents / HTTP Request.

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 11:06
par hairv
Petite correction (suppression de code inutile)

Code : Tout sélectionner


local Alertidx = 126 -- Change to your idx for the Virtual Alert sensor you have created for this script
local hostWebUI = "192.168.1.68" -- Change to your local ip (127.0.0.1 most of the time)
local portWebUI = "9440"  -- Change WebUI port 
local protocol = 'http' -- http or https
local user = ''  -- '' if no user/password
local password = '' -- '' if no user/password

-- no changes needed below this section (except timer if you want to change frequency)
local apiPath = '/rest-z4d/1/zdevice-name'
local devicesLive=0
local devicesLost=0
local devicesUndefined=0


return {
	logging = {
	    level = domoticz.LOG_ERROR,
		marker = '(Zigate Status)'
	},
	on = {
		timer =  {'every 15 minutes'},   
		httpResponses = {'triggerSA'} -- must match with the callback passed to the openURL command
    	},
	execute = function(domoticz, item)

	if (item.isTimer) then
	   domoticz.devices(Alertidx).updateAlertSensor(domoticz.ALERTLEVEL_GREY, "Requesting ...")
        if (user == '') then
            fullURL = protocol .. '://' .. hostWebUI .. ':' .. portWebUI .. apiPath
        else
            fullURL = protocol .. '://' .. user .. ':' .. password .. '@' .. hostWebUI .. ':' .. portWebUI .. apiPath
        end
	    domoticz.openURL({
    	    url = fullURL,
			method = 'GET',
			callback = 'triggerSA' -- see httpResponses above.
		})
	end

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

				local result_table = item.json
				local tc = #result_table
				
				for i = 1, tc do
				    if result_table[i].Health == "Live" then
                        devicesLive=devicesLive+1
                    elseif result_table[i].Health == "Not Reachable" then
                        devicesLost=devicesLost+1
                    else
                        devicesUndefined=devicesUndefined+1
                    end
                end
				
				
				if devicesLost == 0 and devicesUndefined==0 then
				    level = domoticz.ALERTLEVEL_GREEN 	-- domoticz.ALERTLEVEL_GREY, ALERTLEVEL_GREEN, ALERTLEVEL_YELLOW, ALERTLEVEL_ORANGE, ALERTLEVEL_RED
				elseif devicesLost==0 then
				    level = domoticz.ALERTLEVEL_ORANGE 	
				else 
				    level = domoticz.ALERTLEVEL_RED 	
                end
                AlertText="Live: " .. devicesLive .. "\nUnknown: " .. devicesUndefined .. "\nLost: " ..devicesLost 
				-- update device in Domoticz
				domoticz.devices(Alertidx).updateAlertSensor(level, AlertText)
			end
		else
			domoticz.log('There was a problem handling the request' .. url, domoticz.LOG_ERROR)
            domoticz.devices(Alertidx).updateAlertSensor(domoticz.ALERTLEVEL_RED, "Unable to retrieve devices status ... check plugin and settings")
		end

	end
end
}

Re: Widget - Status des devices gérés par le plugin

Publié : 21 déc. 2023, 12:35
par DiliaK
Merci pour le script.

Par contre, ça m'active des lignes DEBUG dans les logs même pour des objets non Zigbee.

Code joint, on voit bien script en marche et après coupure.

Code : Tout sélectionner

 2023-12-21 12:30:19.669 Status: dzVents: Debug: Event triggers:
2023-12-21 12:30:19.669 Status: dzVents: Debug: - Device: Température processeur
2023-12-21 12:30:19.971 Status: dzVents: Debug: Dumping domoticz data to /home/diliak/domoticz/scripts/dzVents/domoticzData.lua
2023-12-21 12:30:19.985 Status: dzVents: Debug: Processing device-adapter for Tension PC cabanon: Voltage device adapter
2023-12-21 12:30:19.985 Status: dzVents: Debug: dzVents version: 3.1.8
2023-12-21 12:30:19.985 Status: dzVents: Debug: Event triggers:
2023-12-21 12:30:19.985 Status: dzVents: Debug: - Device: Tension PC cabanon
2023-12-21 12:30:21.034 Status: EventSystem: reset all events...
2023-12-21 12:30:29.639 Domo: General/Percentage (Utilisation CPU)
2023-12-21 12:30:38.735 RFX0: Temp (Congel)
2023-12-21 12:30:39.934 RFX0: Temp + Humidity (Cuisine)
2023-12-21 12:30:40.606 RFX0: Temp + Humidity (Chambre)
2023-12-21 12:30:44.510 RFX0: Temp + Humidity (Exterieur)
2023-12-21 12:30:45.005 RFX0: Temp + Humidity (Cave)
2023-12-21 12:30:49.517 RFX0: Temp (Frigo)
2023-12-21 12:30:49.654 Domo: General/Percentage (HDD /boot/efi)
2023-12-21 12:30:49.657 Domo: General/Percentage (HDD /)
2023-12-21 12:30:51.165 RFX0: Temp + Humidity (Salle de bains)
2023-12-21 12:30:52.093 RFX0: Temp + Humidity (Salon)
2023-12-21 12:30:59.658 Domo: General/Percentage (Utilisation CPU)
2023-12-21 12:31:00.054 Status: LUA: Device based event fired on 'Frigo', value '2.5'
2023-12-21 12:31:00.055 Status: LUA: Device based event fired on 'Puissance PC bureau', value '204.0'
2023-12-21 12:31:00.055 Status: LUA: Device based event fired on 'Tension PC bureau', value '237.0;237.0'
2023-12-21 12:31:00.056 Status: LUA: Device based event fired on 'Salon', value '21.1;45;1'
2023-12-21 12:31:00.056 Status: LUA: Device based event fired on 'Congel', value '-19.3'
2023-12-21 12:31:00.056 Status: LUA: Device based event fired on 'Exterieur', value '6.4;92;3'
2023-12-21 12:31:00.062 Status: LUA: 1015.8;0
2023-12-21 12:31:00.062
2023-12-21 12:31:00.064 Status: EventSystem: Script event triggered: Pression atmo
2023-12-21 12:31:00.071 Status: LUA: 1.28;0
2023-12-21 12:31:00.071
2023-12-21 12:31:00.073 Status: EventSystem: Script event triggered: Pression chaudière