Unable to change status variable using Flow Script

Hi,

Maybe someone can help me with this one. I’m trying to change a variable using a Flow Script. However, I’m not successful. The status variable has a custom action, a PHP script. I think this is why I’m unable to change the status variable using a Flow Script. Here’s the PHP script (the PHP script is made by a very helpful forum member):

<?php

//Start writing your code here

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

If ($_IPS['SENDER'] == 'Variable' || $_IPS['SENDER'] == 'WebFront' || $_IPS['SENDER'] == 'VoiceControl' || $_IPS['SENDER'] == 'Action') { // execute this script on variable change only
	$Sender = $_IPS['VARIABLE']; // Which variable triggers 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 only be executed on variable change. Sender was: '.$_IPS['SENDER']);
}
?>

In a flow script, you need to select the variable as target and then select an action like „Switch to Value“ where you can select the new value for the variable.

If that doesn’t help, what exactly is not working or missing? Are there any errors?

That’s exactly what I did, and works without issues on other status variables. However, on this specific status variable (with a custom action configured) it fails, without errors. Is there something I can do to troubleshoot the issue?


Hmm, when I change the string value manually with „Modify variable“ it looks like it works as expected. :thinking:

I don’t know what exactly your action script is meant to do, but it doesn’t seem to update the original variable at all. Can you tell me, what part of the action is not working and what should happen instead? When you modify the variable, you can select if you want to switch it or simulate. What are you doing? Simulating does not use the action script, so I would expect it to work. Switching should have the same issues as the flow script.

Good morning @Dr.Niels ,

After some more experimenting, it looks like the issue is caused by the trigger value of the flow script (change/update) and the configured value for „retain“ of the MQTT Server Devices (On/Off).
What happens is that the action isn’t executed when triggered from the MQTT Server Device/Flow Script. I have to repeat the „trigger“ from MQTT about 5 times and then it works.
I’ll have to experiment some more. Thanks for looking into this.

@Dr.Niels It looks like the issue is solved by forcing „Optimistic mode“ in Home Assistant for the MQTT command topics. (MQTT state/command topics are created in Symcon, Home Assistant subscribes to state/command topics created in Symcon)

When a state topic is not available, the light will work in optimistic mode. In this mode, the light will immediately change state after every command. Otherwise, the light will wait for state confirmation from the device (message from state_topic). The initial state is set to False / off in optimistic mode.

Optimistic mode can be forced, even if the state_topic is available. Try to enable it, if experiencing incorrect light operation.