# Dimming with single button - cannot toggle flow variable

**URL:** <https://discourse.nodered.org/t/dimming-with-single-button-cannot-toggle-flow-variable/69819>\
**Category:** General\
**Tags:** home-assistant\
**Created:** [31 October 2022 09:35 UTC](https://discourse.nodered.org/t/dimming-with-single-button-cannot-toggle-flow-variable/69819 "2022-10-31T09:35:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ver/32/69953_2.png) [@ver](https://discourse.nodered.org/u/ver)\
**Post date:** [31 October 2022 09:35 UTC](https://discourse.nodered.org/t/dimming-with-single-button-cannot-toggle-flow-variable/69819/1 "2022-10-31T09:35:24Z")

</div>

Hi. I'm learning NodeRED (in HomeAssistant) and trying to create simple flow for dimming lights with single button. Seems like noob task. Right now I'm ignoring long/short press functionality, instead I want to achieve dimming toggled up/down on hold. So on first button hold it will set brightness up, on second down, on third up etc. To do that I'm using flow variable. So on press with switch node I'm checking previous dimming direction, and then in change node I'm changing it to opposite direction. But I have no idea why this does not work. On every click it's dimming down. Flow variable is not stored between start/end of flow?

I probably can simplify that by installing toggle node, or just use ready flow, but that's not the point. I just want to know what I did wrong 🙂

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/880bc908e30fff1817deb6637616fe409b67b2d2.png)

```auto
[{"id":"27cee93afcf45c24","type":"server-state-changed","z":"afc2c32c7f4bbb52","name":"","server":"60f2db74.303074","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.living_room_cabinet_light_dimmer_switch_l1","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":230,"y":440,"wires":[["b791bbb9cca25b37"]]},{"id":"4d5b76d9615eb99d","type":"switch","z":"afc2c32c7f4bbb52","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1130,"y":460,"wires":[["fb733d490a5b45cc"],["235a0988af0df69f"]]},{"id":"7d7a0d7bb7d97bfc","type":"mqtt out","z":"afc2c32c7f4bbb52","name":"","topic":"zigbee2mqtt/kitchen-countertop-light-power-supply/set","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"eca6af44.5297b","x":2040,"y":440,"wires":[]},{"id":"020964a3bb283ff6","type":"change","z":"afc2c32c7f4bbb52","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"brightness_move\": 40}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":320,"wires":[["7d7a0d7bb7d97bfc","3a9e265d0ccecff3"]]},{"id":"235a0988af0df69f","type":"change","z":"afc2c32c7f4bbb52","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"brightness_move\": 0}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1340,"y":480,"wires":[["7d7a0d7bb7d97bfc","3a9e265d0ccecff3"]]},{"id":"591c166261892b8a","type":"change","z":"afc2c32c7f4bbb52","name":"set direction up","rules":[{"t":"set","p":"direction","pt":"flow","to":"up","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":500,"wires":[["4d5b76d9615eb99d"]]},{"id":"b791bbb9cca25b37","type":"switch","z":"afc2c32c7f4bbb52","name":"last direction","property":"direction","propertyType":"flow","rules":[{"t":"eq","v":"up","vt":"str"},{"t":"eq","v":"down","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":610,"y":440,"wires":[["19d7f28611a06903","c162d98f2e1fcd1e"],["591c166261892b8a","c162d98f2e1fcd1e"],[]]},{"id":"19d7f28611a06903","type":"change","z":"afc2c32c7f4bbb52","name":"set direction down","rules":[{"t":"set","p":"direction","pt":"flow","to":"down","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":420,"wires":[["4d5b76d9615eb99d"]]},{"id":"fb733d490a5b45cc","type":"switch","z":"afc2c32c7f4bbb52","name":"","property":"direction","propertyType":"flow","rules":[{"t":"eq","v":"up","vt":"str"},{"t":"eq","v":"down","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1330,"y":340,"wires":[["020964a3bb283ff6"],["2871a0109e9f845b"]]},{"id":"2871a0109e9f845b","type":"change","z":"afc2c32c7f4bbb52","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"brightness_move\": -40}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1520,"y":360,"wires":[["7d7a0d7bb7d97bfc","3a9e265d0ccecff3"]]},{"id":"3a9e265d0ccecff3","type":"debug","z":"afc2c32c7f4bbb52","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1900,"y":340,"wires":[]},{"id":"c162d98f2e1fcd1e","type":"debug","z":"afc2c32c7f4bbb52","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1020,"y":260,"wires":[]},{"id":"4f6bd72b987b29c6","type":"inject","z":"afc2c32c7f4bbb52","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":230,"y":240,"wires":[["b791bbb9cca25b37"]]},{"id":"586620399cde0dcf","type":"inject","z":"afc2c32c7f4bbb52","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":230,"y":280,"wires":[["b791bbb9cca25b37"]]},{"id":"60f2db74.303074","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"},{"id":"eca6af44.5297b","type":"mqtt-broker","name":"MQTT","broker":"homeassistant.local","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [31 October 2022 09:57 UTC](https://discourse.nodered.org/t/dimming-with-single-button-cannot-toggle-flow-variable/69819/2 "2022-10-31T09:57:11Z")

</div>

If you put separate (named) debug nodes in the places I've marked you should be able to see why it is not working.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/0/a0d64900913a80fcadd03e19a62f25954fc8c751.png)

NOTE: If you want a user to try out your flow, you must not include nodes that produce data (in this case the blue HA node) but instead, capture its output and replace them nodes with INJECT or CHANGE nodes to _simulate_ the data. Lets just say, no one has access to your HA or your buttons/sensors except for you. Essentially, we have no idea what data is coming out of the blue node & that makes it difficult (sometimes impossible) to assist with anything other than questions, tips or speculation.

---

<div class="post-metadata">

**Author:** ![ver](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ver/32/69953_2.png) [@ver](https://discourse.nodered.org/u/ver)\
**Post date:** [31 October 2022 21:38 UTC](https://discourse.nodered.org/t/dimming-with-single-button-cannot-toggle-flow-variable/69819/3 "2022-10-31T21:38:14Z")

</div>

Yeah, off state was changing flow variable too. So switch for on/off should be first. Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 November 2022 21:38 UTC](https://discourse.nodered.org/t/dimming-with-single-button-cannot-toggle-flow-variable/69819/4 "2022-11-14T21:38:31Z")

</div>

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