Combining dimmer with Harmony activities

Easy to use, 100% Lua-based event scripting framework.
Post Reply
kipusoep
Posts: 9
Joined: Monday 19 February 2018 21:11
Target OS: NAS (Synology & others)
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Combining dimmer with Harmony activities

Post by kipusoep » Monday 19 February 2018 21:20

Hi all,

C# developer here and kind of new to the whole scripting thing in Domoticz.
First I tried Blockly, which is nice, but I like more control.
So then I found Lua and tried some little things, which works ok.
Then I found dzVents, which I like more than Lua, so sticking with that for now.

Now I'm running into the following;
I'd like to change a dimmer level when watching a movie or watching TV, both are Logitech Harmony activities (Harmony Elite/Hub).
So I set up 4 events; both a On and Off event for both activities. The On event looks like this:

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'TV kijken',
		}
	},
	execute = function(domoticz, device)
		local light = domoticz.devices('LED spots')
		if (device.state == 'On') then
		    domoticz.globalData.LEDSpotsLevel = light.level
			light.dimTo(40)
		end
	end
}
The Off event looks like this:

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'TV kijken',
		}
	},
	execute = function(domoticz, device)
		local light = domoticz.devices('LED spots')
		if (device.state == 'Off') then
			light.dimTo(domoticz.globalData.LEDSpotsLevel)
		end
	end
}
The only issue with this; when I switch from "TV kijken" to "Film kijken", first the dimmer is set to the LEDSpotsLevel followed by the activity level. But these are following up eachother so fast, the 2nd instruction is ignored.
2018-02-19 20:04:34.976 Harmony Hub: Changing activity
2018-02-19 20:04:42.558 Harmony Hub: Finished changing activity
2018-02-19 20:04:42.633 dzVents: Info: Handling events for: "Film kijken", value: "Off"
2018-02-19 20:04:42.633 dzVents: Info: ------ Start internal script: Film kijken start: Device: "Film kijken (Harmony)", Index: 10
2018-02-19 20:04:42.641 dzVents: Info: ------ Finished Film kijken start
2018-02-19 20:04:42.641 dzVents: Info: ------ Start internal script: Film kijken stop: Device: "Film kijken (Harmony)", Index: 10
2018-02-19 20:04:42.642 dzVents: Off
2018-02-19 20:04:42.642 dzVents: Info: ------ Finished Film kijken stop
2018-02-19 20:04:42.643 EventSystem: Script event triggered: /usr/local/domoticz/var/scripts/dzVents/runtime/dzVents.lua
2018-02-19 20:04:42.720 dzVents: Info: Handling events for: "TV kijken", value: "On"
2018-02-19 20:04:42.721 dzVents: Info: ------ Start internal script: TV kijken start: Device: "TV kijken (Harmony)", Index: 9
2018-02-19 20:04:42.722 dzVents: Info: ------ Finished TV kijken start
2018-02-19 20:04:42.722 dzVents: Info: ------ Start internal script: TV kijken stop: Device: "TV kijken (Harmony)", Index: 9
2018-02-19 20:04:42.723 dzVents: Info: ------ Finished TV kijken stop
2018-02-19 20:04:42.723 EventSystem: Script event triggered: /usr/local/domoticz/var/scripts/dzVents/runtime/dzVents.lua
Question: How can I prevent the 2nd instruction from being ignored, or how can I prevent the 'restore' instruction from being sent at all when there's a follow-up event?

kipusoep
Posts: 9
Joined: Monday 19 February 2018 21:11
Target OS: NAS (Synology & others)
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by kipusoep » Thursday 22 February 2018 15:38

I didn't expect this would be so hard or not done before, right? Nobody knows?

dannybloe
Posts: 1473
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Combining dimmer with Harmony activities

Post by dannybloe » Thursday 22 February 2018 16:22

The scripts you posted above do not match with your log output so It's quite hard to figure out what is happening. Also, you can easily create just one script with different on-triggers and check for them in your execute function. That keeps everything together and perhaps you can use a local spot-level instead of a global one.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

kipusoep
Posts: 9
Joined: Monday 19 February 2018 21:11
Target OS: NAS (Synology & others)
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by kipusoep » Thursday 22 February 2018 17:45

The FIlm kijken events look the same:

On:

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'Film kijken',
		}
	},
	execute = function(domoticz, device)
		local light = domoticz.devices('LED spots')
		if (device.state == 'On') then
		    domoticz.globalData.LEDSpotsLevel = light.level
			light.dimTo(20)
		end
	end
}
Off:

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'Film kijken',
		}
	},
	execute = function(domoticz, device)
		local light = domoticz.devices('LED spots')
		if (device.state == 'Off') then
			light.dimTo(domoticz.globalData.LEDSpotsLevel)
		end
	end
}
Combining into one script might help, but I'm afraid it will be called twice; once for turning activity A off and once for turning activity B on, which gives the same problem as I'm currently experiencing.

dannybloe
Posts: 1473
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Combining dimmer with Harmony activities

Post by dannybloe » Thursday 22 February 2018 20:56

The triggers are the same but the log shows different devices. You have two equal device triggers (Film kijken) but the log says Film kijken (Harmony) and TV kijken (Harmony). So, what are the actual scripts?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

kipusoep
Posts: 9
Joined: Monday 19 February 2018 21:11
Target OS: NAS (Synology & others)
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by kipusoep » Friday 23 February 2018 15:35

No I have 4 triggers for 2 activities; On/off for TV kijken and on/off for Film kijken.
Those 4 scripts are shown above.

Boredcat
Posts: 37
Joined: Friday 17 March 2017 10:31
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by Boredcat » Friday 23 February 2018 16:33

What you can do is forget the off part for TV kijken and Film Kijken. Just create a new script for Power Off (a harmony state which is active when all activities are off).

So when you execute TV kijken dim level is set to 40, when you change to Film kijken it changes to dim level 20. Without first going to the original dimlevel. When you then press the off button the dim level is set back to LedSpotsLevel.

Now you need something to check if the Ledspotlevel needs to be stored since the level must only be stored when a TV kijken or Film Kijken is started.. Create a global variable named Harmony which is set to On then a activity is started and Off when all activity are stopped.



Example for Film Kijken, Added extra check if LED Spots are on. (No need to dim lights when they are off )

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'Film kijken',
		}
	},
	execute = function(domoticz, device)
		local light = domoticz.devices('LED spots')
		if (device.state == 'On'   and light.state == 'On' ) then
			if (domoticz.globalData.Harmony == 'Off') then
			  domoticz.globalData.LEDSpotsLevel = light.level
			  domoticz.globalData.Harmony = 'On'
			end
			light.dimTo(20)
	
			
		end
	end
}

And for the power.

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'Power Off',
		}
	},
	execute = function(domoticz, device)
		local light = domoticz.devices('LED spots')
		if (device.state == 'Off') then
			
			  domoticz.globalData.Harmony = 'Off'
		
			light.dimTo(domoticz.globalData.LEDSpotsLevel)
	
			
		end
	end
}
RFXCom(E) - Yeelight Lights - KaKu / Alexa HA Bridge
Windows 2012 / Windows 2012R2 / Xpenology/ Pi 3 / Milight / Home build ESP8266 / Sonoff

kipusoep
Posts: 9
Joined: Monday 19 February 2018 21:11
Target OS: NAS (Synology & others)
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by kipusoep » Sunday 04 March 2018 15:36

Boredcat wrote:
Friday 23 February 2018 16:33
What you can do is forget the off part for TV kijken and Film Kijken. Just create a new script for Power Off (a harmony state which is active when all activities are off).
I can't check for Power Off only, because there are more activities, like Listen music. Switching to such an activity should also trigger the restore of the dimmer level.

kipusoep
Posts: 9
Joined: Monday 19 February 2018 21:11
Target OS: NAS (Synology & others)
Domoticz version: 3.8153
Location: The Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by kipusoep » Sunday 04 March 2018 16:23

This seems to work quite well:

Code: Select all

return {
    active = true,
    on = {
        devices = {
            'Film kijken',
            'TV kijken'
        }
    },
    data = {
        previousLevel = { initial = 60 }
    },
    logging = {
        level = domoticz.LOG_DEBUG,
        marker = "LED dimmer"
    },
    execute = function(domoticz, device)
        domoticz.log('Enter event for ' .. device.name .. ' | State ' .. device.state)
        local light = domoticz.devices('LED spots')
        domoticz.log('Previous level on start ' .. domoticz.data.previousLevel .. ' | Light level is ' .. light.level)
        if (light.state == 'On') then
            domoticz.log('LED on')
            if (device.state == 'On') then
                domoticz.log('Device on')
                domoticz.log('Previous level currently ' .. domoticz.data.previousLevel)
                if (light.level ~= 20) then
                    domoticz.data.previousLevel = light.level
                    domoticz.log('Previous level set to ' .. domoticz.data.previousLevel)
                end
                light.dimTo(20).afterSec(1)
                domoticz.log('Dimming to 20')
            elseif (device.state == 'Off') then
                domoticz.log('Device off')
                light.dimTo(domoticz.data.previousLevel)
                domoticz.log('Dimming to ' .. domoticz.data.previousLevel)
            end
        else
            domoticz.log('LED off, bye bye')
        end
    end
}
Especially the part light.dimTo(20).afterSec(1) fixes my issues.

Boredcat
Posts: 37
Joined: Friday 17 March 2017 10:31
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Netherlands
Contact:

Re: Combining dimmer with Harmony activities

Post by Boredcat » Friday 16 March 2018 17:44

Going to use some parts of your script. ;-)
RFXCom(E) - Yeelight Lights - KaKu / Alexa HA Bridge
Windows 2012 / Windows 2012R2 / Xpenology/ Pi 3 / Milight / Home build ESP8266 / Sonoff

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests