Home assistant device control

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

Moderator: leecollings

Post Reply
Maxx
Posts: 61
Joined: Saturday 27 January 2018 20:59
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Home assistant device control

Post 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
}
physiker123
Posts: 37
Joined: Wednesday 25 November 2015 18:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany / France
Contact:

Re: Home assistant device control

Post 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!
Post Reply