Page 1 of 1

Home assistant device control

Posted: Saturday 29 July 2023 22:43
by Maxx
All,

I love Domoticz for the flexibility and for Dzvents. I also use HA, the integrations are difficult to resist. Now I use both and I was trying to control HA device using Dzvents without MQTT (yet).

The following script allows direct control of a HA Device using REST API, maybe it is usefull for someone

Code: Select all

return {
	on = {

		timer = {'every minute'},

		httpResponses = {'Light_Callback'},

	},
    logging =   {   level                   = domoticz.LOG_DEBUG,
                    marker                  = "Home assistant" },

	
	execute = function(domoticz, item)
	    
	    local state = 'turn_on'
	    local pump = '{"entity_id": "switch.smart_plug_4"}'
	    
		if (item.isTimer) then
		    domoticz.log('Timer event')
			domoticz.openURL({
				url = 'http://192.168.1.232:8123/api/services/switch/' .. state,
				headers = {['Authorization'] = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJxxxxxxxxxxxxxxxxxjMxYWM4OTgxNzUzNzFmZDFhZiIsImlhdCI6MTY5MDU3NTAxMCwiZXhwIjoyMDA1OTM1MDEwfQ.LjsUi9oVJB6qXif4lMaDoIX540LGOSmCGXdKE968e6w'},
				method = 'POST',
				callback = 'Light_Callback', 
				postData = pump
			})
	    end
	
    if (item.isHTTPResponse) then
            domoticz.log('HTTP reponse HA was ok')
			if (item.statusCode == 200) and (item.isJSON) then	
	--		domoticz.log(item) 
			domoticz.log('Home assistant')
            end
    end
	
end
}

Re: Home assistant device control

Posted: Tuesday 05 September 2023 9:26
by physiker123
I was just using the same thing, and struggeling with the formation of the headers. Thank you very much, your solution works fine for me!