My scenario:
I have a fan and I control it’s speed using an input_slider and an automation. I have a sensor that reads the fan speed, and when it changes am automation is triggered to change the value of the input slider.
My issues
When HA is restarted the default value of the input_slider triggers an event and thus might trigger the automation to write the default value to the input_slider to the fan.
When the fan speed is changed on the fan instead of the input_slider the fan speed sensor will trigger the automation to update the input_slider value and the change of the input_slider will trigger an event to write the same value back to the fan.
I don’t really know how to solve this. My first idea was to let an input_slider have a “disabled” property, and with an automation make it enabled when the first value was received from the fan_sensor.
For the second issue I guess it would be possible to see in the automation who triggered the change on the input_slider, if it was the UI or an automation. If it was the sensor the automation can ignore the event and don’t write the value back to the fan.
Does anyone have any input or better ideas? I really don’t want to make any changes that increases the complexity too much.
Your automation is triggered by a “change”. Well, at bootup, the slider is at a default value of zero, I believe, so it should not trigger a “change” event and therefore should not trigger the automation, but then again, I don’t know your HA setup, so I don’t know what is happening at startup and what automations you have triggering and when.
As for changing the speed on the fan manually, both of your automations should be checking that the new value does not already match the other value, so if they match, don’t change the slider or update the fan, and if they don’t match, then you can send the change.
Basically, as long as you are verifying the values before performing the action, then you should never get caught into that loop.
And since I don’t how you are communicating with your fan via the slider, I can only give an example using something like MQTT to do the same verification on the other side:
The first issue was caused by my automation. I was accidentally setting the slider to it’s old value instead of the new value from the sensor. So when the first sensor value arrived I wrote the initial slider value to the device.
The other issue can as you say be prevented with a condition. I never had a loop, I just thought it was an unnecessary call to the device.
I will still write the current value back to the device once when I start HA since the initial value of the input_slider might differ from the first value reported from the sensor. But that is OK really, won’t do any harm.