Page 1 of 1

Script trigger does not work. Why?

Posted: Tuesday 24 November 2020 20:44
by Scaevola
Hello everyone,
I've got a ceiling LEDs connected to the ESPEasy and MOSFET. I have a nice slider in Domoticz, that works well.

Code: Select all

-- LED dimmer script script using ESPEasy

commandArray = {}
DomDevice = 'ESP_dimmer';
IP = '192.168.0.108';
PIN = "2";
if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
		print ("OFF dim = "..uservariables['dimmer']);
		CalcValue = 0;
   	else if(devicechanged[DomDevice]=='On') then
   		DomValue = uservariables['dimmer'];
        print ("ON dim = "..uservariables['dimmer']);
        CalcValue = DomValue;
    else
    	print("Other");
        DomValue = otherdevices_svalues[DomDevice];
        CalcValue = math.floor(DomValue / 100 * 1023);
        commandArray['Variable:dimmer'] = tostring(CalcValue);
        print ("dim Level = "..uservariables['dimmer']);
   	end
   	end
   	runcommand = "curl 'http://" .. IP .. "/control?cmd=PWM,"  ..PIN.. "," .. CalcValue .. ",500'";
   	os.execute(runcommand);
	print("PWM value= "..CalcValue);
end
return commandArray
However, I try to automate the dimming by discovering Philips TV being turned on (by pings). I created also a virtual device, that changes the state of the switch depends on TV state.
But there are some bumps:
I try to release the dimming by the TV, but, despite the TV state is discovered well (the log says the tv is on or off), I can't make the dimmer work with that.
I tried a blockly scheme without success.
blockly.png
blockly.png (17.17 KiB) Viewed 310 times
Then I tried a dzVents script, also without succes.

Code: Select all

return {
   	on = {
		devices = {'Telewizor_Philips','ESP_dimmer'	}
		},
	
	execute = function(domoticz, Telewizor_Philips)
	     if Telewizor_Philips.state==('On') then
	       commandArray['Scene:Brighten_LED']='On'
	        domoticz.log('Light dimmed - TV on ')
        else
         commandArray['Scene:Dim_LED']='On'
	        domoticz.log('Light brighten - tv off ')
	    end 
	end
}
return commandArray
What I am doing wrong?

Re: Script trigger does not work. Why?

Posted: Tuesday 24 November 2020 21:49
by waaren
Scaevola wrote: Tuesday 24 November 2020 20:44 I've got a ceiling LEDs connected to the ESPEasy and MOSFET. I have a nice slider in Domoticz, that works well.

Then I tried a dzVents script, also without succes.
What I am doing wrong?
You should have seen some errors in the log because you mix classic Lua with dzVents.

You could try below script but please first take some minutes to read this.
_________________________________________________________________________________________________________________________
When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
___________________________________________________________________________________________________________________________

Code: Select all

return
{
    on =
    {
        devices =
        {
            'Telewizor_Philips',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Philips TV',
    },

    execute = function(dz, item)
        dz.log(item.name .. ', state: ' .. item.state, dz.LOG_DEBUG )
        if item.state == 'On' then
            dz.scenes('Brighten_LED').switchOn()
            dz.log('Light dimmed - TV on ', dz.LOG_DEBUG)
        else
            dz.scenes('Dim_LED').switchOn()
            dz.log('Light brighten - tv off ', dz.LOG_DEBUG)
        end
    end
}
.

Re: Script trigger does not work. Why?

Posted: Thursday 26 November 2020 11:15
by Scaevola
Ok. Thank you. The syntax of dzVents is very odd to me. I try to understand it, but it's quite different from other programming languages.
And do you know why the blockly scheme did not work?

Re: Script trigger does not work. Why?

Posted: Thursday 26 November 2020 12:31
by waaren
Scaevola wrote: Thursday 26 November 2020 11:15 Ok. Thank you. The syntax of dzVents is very odd to me. I try to understand it, but it's quite different from other programming languages.
I am sure you will get up to speed quickly with dzVents if you are used to other programming languages once you start playing with the many examples on this forum.
dzVents is 100% Lua and on the internet there is an enormous amount of useful information about Lua.

[/quote]And do you know why the blockly scheme did not work?[/quote]No.
Did you store the Blockly as being device triggered?
Do you see anything in the domoticz log when changing the state of the virtual switch?