Feedback loop issues - Home Assistant set_value and and state_trigger

Hi smart people :slight_smile:

I'm trying to integrate my Air Conditioner (Tuya based :frowning: ) to Home Assistant (HA) and I've created this flow:

[{"id":"b2a1f22e.056c3","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"5ff38c3.6cecc74","type":"function","z":"b2a1f22e.056c3","name":"Turn on","func":"msg.payload ={\n dps : 2,\n set : msg.payload*10\n}\n\n/*\n\n    */\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":1120,"wires":[["4fd928d0.929f28","30c18e90.67a7f2"]]},{"id":"4fd928d0.929f28","type":"function","z":"b2a1f22e.056c3","name":"Air Conditioner","func":"msg.payload = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":800,"wires":[["fb05d962.4cd2f8"]]},{"id":"fb05d962.4cd2f8","type":"change","z":"b2a1f22e.056c3","name":"","rules":[{"t":"move","p":"payload.data.dps","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1220,"y":1120,"wires":[["5946ffdb.db614"]]},{"id":"5946ffdb.db614","type":"split","z":"b2a1f22e.056c3","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1410,"y":1120,"wires":[["6f02b39b.14856c"]]},{"id":"6f02b39b.14856c","type":"switch","z":"b2a1f22e.056c3","name":"","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"},{"t":"eq","v":"4","vt":"str"},{"t":"eq","v":"5","vt":"str"},{"t":"eq","v":"12","vt":"str"},{"t":"eq","v":"14","vt":"str"},{"t":"eq","v":"15","vt":"str"},{"t":"eq","v":"22","vt":"str"},{"t":"eq","v":"26","vt":"str"},{"t":"eq","v":"36","vt":"str"},{"t":"eq","v":"101","vt":"str"},{"t":"eq","v":"102","vt":"str"},{"t":"eq","v":"103","vt":"str"}],"checkall":"true","repair":false,"outputs":14,"x":1570,"y":1120,"wires":[[],["a14bcf2e.7de4d"],[],[],[],[],[],[],[],[],[],[],[],[]]},{"id":"a14bcf2e.7de4d","type":"switch","z":"b2a1f22e.056c3","name":"Below or above 16","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"160","vt":"str"},{"t":"lt","v":"160","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1490,"y":800,"wires":[["c9d7100f.72405"],["c129feeb.05799"]]},{"id":"c9d7100f.72405","type":"function","z":"b2a1f22e.056c3","name":"","func":"msg.payload = msg.payload / 10;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1680,"y":780,"wires":[["92094df5.9944"]]},{"id":"92094df5.9944","type":"api-call-service","z":"b2a1f22e.056c3","name":"AC Kontor Cooling Temp","server":"1dbb4297.18e25d","version":1,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"input_number.ac_office_cooling_temp","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1890,"y":760,"wires":[["2643b8fc.154dd8"]]},{"id":"2643b8fc.154dd8","type":"debug","z":"b2a1f22e.056c3","name":"Target temp","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2110,"y":760,"wires":[]},{"id":"c129feeb.05799","type":"api-call-service","z":"b2a1f22e.056c3","name":"AC Kontor Cool Storage Temp","server":"1dbb4297.18e25d","version":1,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"input_number.ac_office_coolstorage_temp","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1810,"y":840,"wires":[["d763b660.3ba428"]]},{"id":"d763b660.3ba428","type":"debug","z":"b2a1f22e.056c3","name":"Target Cooling temp","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2080,"y":840,"wires":[]},{"id":"e69a41e1.b96ab","type":"inject","z":"b2a1f22e.056c3","name":"Set cooling temp 16 degree (160)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"devId\":\"XXXXXXXXXXXXXX\",\"dps\":{\"2\":160}},\"deviceId\":\"XXXXXXXXX\",\"deviceName\":\"Airconditioner\"}","payloadType":"json","x":950,"y":660,"wires":[["fb05d962.4cd2f8"]]},{"id":"d64df7cf.91e318","type":"inject","z":"b2a1f22e.056c3","name":"Set cooling temp 20 degree (200)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"devId\":\"XXXXXXXXXXXXXX\",\"dps\":{\"2\":200}},\"deviceId\":\"XXXXXXXXX\",\"deviceName\":\"Airconditioner\"}","payloadType":"json","x":950,"y":700,"wires":[["fb05d962.4cd2f8"]]},{"id":"30c18e90.67a7f2","type":"debug","z":"b2a1f22e.056c3","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":950,"y":1120,"wires":[]},{"id":"13bf8f39.0dc3f1","type":"trigger-state","z":"b2a1f22e.056c3","name":"","server":"1dbb4297.18e25d","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_number.ac_office_cooling_temp","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":510,"y":1120,"wires":[["5ff38c3.6cecc74"],[]]},{"id":"1dbb4297.18e25d","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I'm facing a problem with feedback loops I cannot get around. There are no issues when the actions are triggered from Home Assistant (HA) itself but becuase the AC can trigger value changes itself, either temperature changes or when you change values on the device, I will face a problem with feedback loops. I've just created a test function called Air Conditioner and two inject triggers for temperature change simulation from the AC and this is the part of the flow that will cause the problem.

The wanted behaviour of this flow:

  • State change triggered from HA GUI: send to value to AC and do no more
  • State change triggered from AC: send to HomeAssistant and do no more

What actually happens:

  • State change triggered from HA GUI: sends to AC and send back to HA and thankfully stops because it's the same value as it already sent
  • State change triggered from AC: sends to HA and sends back to AC because it's not same as the previous value (of course it could be). This is what I want to stop. Basically any change triggered from the AC should not be seen as a state change.

What is the smart solution here?

  1. To create a flow variable for each msg payload item representing either AC triggered or GUI triggered from HomeAssistant?
  2. Another possible solution could be to create separate set and get variables in HA?

Will be very thankful for any input to this challenge!

I assume the trigger and set_value both act on the same input_number field. A quick-and-dirty option would be to set a flow variable just before the set_value and check it after the trigger. So for example, set "adjusting temp" to true, and then after the trigger, only proceed if it is false (remembering also to reset it to false otherwise).

Regardless, won't it only feedback once anyway? If the value is, say, 20 and you click the inject to set it to 16, that will call the set-value, which will fire the trigger, which will set it to 16 again. But this time it fires the trigger it will be changing from 16 to 16, which you can stop in the trigger (if it even calls it).

1 Like

Thanks for the input Michael!

I went with the approach to create a flow "flag". At first this did not work but I guess I just didn't have the right type for the comparison, debug messages really help here :slight_smile:

Working flow:

[{"id":"b2a1f22e.056c3","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"5ff38c3.6cecc74","type":"function","z":"b2a1f22e.056c3","name":"Turn on","func":"msg.payload ={\n dps : 2,\n set : msg.payload*10\n}\n\n/*\n\n    */\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":800,"wires":[["4fd928d0.929f28","82c8cd84.93a74"]]},{"id":"4fd928d0.929f28","type":"function","z":"b2a1f22e.056c3","name":"Air Conditioner","func":"msg.payload = msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":800,"wires":[["fb05d962.4cd2f8"]]},{"id":"fb05d962.4cd2f8","type":"change","z":"b2a1f22e.056c3","name":"","rules":[{"t":"move","p":"payload.data.dps","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1220,"y":800,"wires":[["5946ffdb.db614"]]},{"id":"5946ffdb.db614","type":"split","z":"b2a1f22e.056c3","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1370,"y":900,"wires":[["6f02b39b.14856c"]]},{"id":"6f02b39b.14856c","type":"switch","z":"b2a1f22e.056c3","name":"","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"eq","v":"2","vt":"str"},{"t":"eq","v":"3","vt":"str"},{"t":"eq","v":"4","vt":"str"},{"t":"eq","v":"5","vt":"str"},{"t":"eq","v":"12","vt":"str"},{"t":"eq","v":"14","vt":"str"},{"t":"eq","v":"15","vt":"str"},{"t":"eq","v":"22","vt":"str"},{"t":"eq","v":"26","vt":"str"},{"t":"eq","v":"36","vt":"str"},{"t":"eq","v":"101","vt":"str"},{"t":"eq","v":"102","vt":"str"},{"t":"eq","v":"103","vt":"str"}],"checkall":"true","repair":false,"outputs":14,"x":1510,"y":760,"wires":[[],["a14bcf2e.7de4d"],[],[],[],[],[],[],[],[],[],[],[],[]]},{"id":"a14bcf2e.7de4d","type":"switch","z":"b2a1f22e.056c3","name":"Below or above 16","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"160","vt":"str"},{"t":"lt","v":"160","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1590,"y":580,"wires":[["c9d7100f.72405"],["c129feeb.05799"]]},{"id":"c9d7100f.72405","type":"function","z":"b2a1f22e.056c3","name":"","func":"msg.payload = msg.payload / 10;\n\nac_cool_temp_flag = msg.payload;\n// store the value back\nflow.set('ac_cool_temp_flag',ac_cool_temp_flag);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1780,"y":580,"wires":[["92094df5.9944"]]},{"id":"92094df5.9944","type":"api-call-service","z":"b2a1f22e.056c3","name":"AC Kontor Cooling Temp","server":"1dbb4297.18e25d","version":1,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"input_number.ac_office_cooling_temp","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2010,"y":580,"wires":[["2643b8fc.154dd8"]]},{"id":"2643b8fc.154dd8","type":"debug","z":"b2a1f22e.056c3","name":"Target temp","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2230,"y":580,"wires":[]},{"id":"c129feeb.05799","type":"api-call-service","z":"b2a1f22e.056c3","name":"AC Kontor Cool Storage Temp","server":"1dbb4297.18e25d","version":1,"debugenabled":false,"service_domain":"input_number","service":"set_value","entityId":"input_number.ac_office_coolstorage_temp","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1850,"y":640,"wires":[["d763b660.3ba428"]]},{"id":"d763b660.3ba428","type":"debug","z":"b2a1f22e.056c3","name":"Target Cooling temp","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2200,"y":640,"wires":[]},{"id":"e69a41e1.b96ab","type":"inject","z":"b2a1f22e.056c3","name":"Set cooling temp 16 degree (160)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"devId\":\"XXXXXXXXXXXXXX\",\"dps\":{\"2\":160}},\"deviceId\":\"XXXXXXXXX\",\"deviceName\":\"Airconditioner\"}","payloadType":"json","x":870,"y":540,"wires":[["fb05d962.4cd2f8"]]},{"id":"d64df7cf.91e318","type":"inject","z":"b2a1f22e.056c3","name":"Set cooling temp 20 degree (200)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"devId\":\"XXXXXXXXXXXXXX\",\"dps\":{\"2\":200}},\"deviceId\":\"XXXXXXXXX\",\"deviceName\":\"Airconditioner\"}","payloadType":"json","x":870,"y":580,"wires":[["fb05d962.4cd2f8"]]},{"id":"13bf8f39.0dc3f1","type":"trigger-state","z":"b2a1f22e.056c3","name":"AC cooling input","server":"1dbb4297.18e25d","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_number.ac_office_cooling_temp","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":200,"y":800,"wires":[["c058f255.02908"],[]]},{"id":"ae5ef8a3.636fd8","type":"function","z":"b2a1f22e.056c3","name":"Reset flow flag","func":"ac_cool_temp_flag = 0;\n// store the value back\nflow.set('ac_cool_temp_flag',ac_cool_temp_flag);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":800,"wires":[["5ff38c3.6cecc74"]]},{"id":"c058f255.02908","type":"switch","z":"b2a1f22e.056c3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"ac_cool_temp_flag","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":800,"wires":[["ae5ef8a3.636fd8"],[]]},{"id":"82c8cd84.93a74","type":"debug","z":"b2a1f22e.056c3","name":"","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1030,"y":920,"wires":[]},{"id":"1dbb4297.18e25d","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.