Recall/Save dimmer intensity when using on/off variable

At the end, an own action script for the „Status“ variable was needed. We made this generic script that can used for all instances of that type of dimmer device.

<?php

$IdentState = 'StatusVariable';
$IdentIntensity = 'IntensityVariable';

If ($_IPS['SENDER'] == 'Variable' || $_IPS['SENDER'] == 'WebFront' || $_IPS['SENDER'] == 'VoiceControl' ) { // execute this script on variable change only
	$Sender = $_IPS['VARIABLE']; // What variable trigger this script?
	$ParentInstance = IPS_GetParent($Sender); // Who is the parent instance of this variable?
	$ChildsOfInstance = IPS_GetChildrenIDs($ParentInstance); // array of all instance childs
	If (IPS_GetObject($Sender)['ObjectIdent'] == $IdentState) // if the trigger variable is the state variable
	{
		If ($_IPS['VALUE']) // boolean true
		//If requested action is „on“ and current status is „off“, load intensity value from saved variable.
		{
			$Intensity = (int)IPS_GetObject($ParentInstance)['ObjectInfo']; // reading the intensity, stored in the instance info area 
			foreach ($ChildsOfInstance as $Child) //looking for the matching intensity variable, under the instance
			{
				If (IPS_GetObject($Child)['ObjectIdent'] == $IdentIntensity) //found intensity variable
				{
					RequestAction($Child, $Intensity); // send the read intensity value the device
				}
			}
		}
		else // boolean false
		// If requested action is „off“ and current status is „on“, save Intensity value to some variable, when successful change „status“ to „off“
		{
			foreach ($ChildsOfInstance as $Child) //looking for the matching intensity variable, under the instance
			{
				If (IPS_GetObject($Child)['ObjectIdent'] == $IdentIntensity) //found intensity variable
				{
					$Intensity = GetValue($Child); // getting the current value
					IPS_SetInfo($ParentInstance,$Intensity); // store the current intensity into the info area of the instance
					RequestAction($Child, 0); // send intensity 0 to the device to switch it off
				}
			}
		}
	}
}
else
{
	IPS_LogMessage($_IPS['SELF'],'Script can executed on variable change only. Sender was: '.$_IPS['SENDER']);
}

Thanks for the good conversation.

1 „Gefällt mir“

Hi @Attain ,

I’m still using your script on my dimmers, thanks for that. Would it be possible to only save $Intensity to IPS_SetInfo if $Intensity is greater than 0 (or 10)? I tried to edit the following lines, but failed miserably.

$Intensity = GetValue($Child); // getting the current value
IPS_SetInfo($ParentInstance,$Intensity); // store the current intensity into the info area of the instance
RequestAction($Child, 0); // send intensity 0 to the device to switch it off

Try this:

$Intensity = GetValue($Child); // getting the current value
if ($Intensity > 10)
{
  IPS_SetInfo($ParentInstance,$Intensity); // store the current intensity into the info area of the instance
}
RequestAction($Child, 0); // send intensity 0 to the device to switch it off

Thanks! I managed to get it working, but your code looks much cleaner. :slightly_smiling_face:
I want to prevent a situation where an „ON“ request could lead to „ON to 0 brightness“, which is of course „OFF“.

1 „Gefällt mir“