# \[Solved\] How do save switch state?

**URL:** <https://community.thunkable.com/t/solved-how-do-save-switch-state/454921>\
**Category:** Questions about Thunkable X\
**Tags:** firebase, solved, beginner, arduino, switch\
**Created:** [February 10, 2020, 1:03am UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921 "2020-02-10T01:03:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![olasupoayobami02fa](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@olasupoayobami02fa](https://community.thunkable.com/u/olasupoayobami02fa)\
**Post date:** [February 10, 2020, 1:03am UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/1 "2020-02-10T01:03:41Z")

</div>

Hello guys,

I created an app that I can use to switch on/off appliances using FIrebase and Arduino.

My issue is this: After toggling the switch and I leave the screen that has the switch button, maybe by pressing the back button or navigating to another screen (still within the same session). By the time I launch the screen with the switch again, its status automatically turns to FALSE (the default status), the same thing happens if my Switch value is set to default TRUE in my block properties, the switch status automatically turns to TRUE too.

This does not affect the actual function, i.e the switch toggling automatically to FALSE doesn’t switch off the device it’s controlling

Please, help

 ![Thunkable2](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/1/4/14fcdd75f9eb93d90df5f7ee19efaa15b8e296a3.png) ![Thunkable 1](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/d/c/dc468933a83b8be742d7423de779c26ded893878.png)

---

<div class="post-metadata">

**Author:** ![kartik14](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/kartik14/32/44782_2.png) [@kartik14](https://community.thunkable.com/u/kartik14)\
**Post date:** [February 10, 2020, 4:14am UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/2 "2020-02-10T04:14:37Z")

</div>

Hi, @olasupoayobami02fa! 👋

This will always happen, because every time, the screen initializes, it starts freshly - that’s why, the switch is returned to default _set_ value.

To prevent this, try following these steps -

- Since you are saving the `switch`'s value in `realtimeDB`, you would need to check that value every time the screen is opened - and set switch’s value accordingly.  
But, getting a value from Firebase every time the screen is opened, would not be efficient. Therefore, along with saving in Firebase, save the value in LocalStorage too.
- After saving in LocalStorage, it would be efficient for us to get the value every time (because since it’s stored locally, the retrieval time would be much less).
- Next, grab a `screen Opens` event block, and a `Local Storage - get` block. After getting the value, set the switch’s value to the `value`.

Your final blocks should look like this - _(I have reduced the number of blocks to the minimum required for proper functionality)_

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/7/d/7d005ece1999426b2928a892a216c904fd1cf87a.png)

Hope I helped you. 🙂

Good Luck! 👍  
Thanks! 😊

---

<div class="post-metadata">

**Author:** ![olasupoayobami02fa](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@olasupoayobami02fa](https://community.thunkable.com/u/olasupoayobami02fa)\
**Post date:** [February 10, 2020, 12:08pm UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/3 "2020-02-10T12:08:48Z")

</div>

Thanks so much @kartik14

I’m setting this up but I have another slight issue. I can’t set my `switch`'s value to `value` as you did here

 ![image_LI](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/c/1/c1883525820b8a68b794a965a0298f6c242d4bd8.jpeg)

Here’s mine: I can only toggle between `true` and `false` ![Mine](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/4/a/4ad92030e768424d6e3f48b8e08d273dae1120e3.png)

---

<div class="post-metadata">

**Author:** ![kartik14](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/kartik14/32/44782_2.png) [@kartik14](https://community.thunkable.com/u/kartik14)\
**Post date:** [February 10, 2020, 1:25pm UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/4 "2020-02-10T13:25:36Z")

</div>

Hi, @olasupoayobami02fa! 👋

Just drag the small green `value` block, and attach to the dark green `from Switch13 set value to` block.

Thanks! 😊

---

<div class="post-metadata">

**Author:** ![olasupoayobami02fa](https://avatars.discourse-cdn.com/v4/letter/o/73ab20/32.png) [@olasupoayobami02fa](https://community.thunkable.com/u/olasupoayobami02fa)\
**Post date:** [February 10, 2020, 11:31pm UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/5 "2020-02-10T23:31:54Z")

</div>

Works perfectly.

Thank you! 🤩

---

<div class="post-metadata">

**Author:** ![kartik14](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/kartik14/32/44782_2.png) [@kartik14](https://community.thunkable.com/u/kartik14)\
**Post date:** [February 11, 2020, 1:53am UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/6 "2020-02-11T01:53:49Z")

</div>

**Glad I helped you 😊**

Happy Thunking! 🎉  
Thanks! 😊

---

<div class="post-metadata">

**Author:** ![ANNIE3](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/annie3/32/131378_2.png) [@ANNIE3](https://community.thunkable.com/u/ANNIE3)\
**Post date:** [May 13, 2022, 6:38pm UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/7 "2022-05-13T18:38:24Z")

</div>

Where do you get the “error” block?

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [May 13, 2022, 6:49pm UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/8 "2022-05-13T18:49:24Z")

</div>

Error blocks are built-in to certain blocks. So if you’re using a Realtime\_DB block, you’ll see it’s already attached. But you can also save Firebase values using cloud variables and paths. So it’s not necessary to do that anymore.

---

<div class="post-metadata">

**Author:** ![annayang0211wp5p](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@annayang0211wp5p](https://community.thunkable.com/u/annayang0211wp5p)\
**Post date:** [October 25, 2023, 12:27am UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/9 "2023-10-25T00:27:29Z")

</div>

How do I do this now that the old Thunkable System has changed?  
I want to create a dark mode for my app.

Thank you!

---

<div class="post-metadata">

**Author:** ![ioannis](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/ioannis/32/146956_2.png) [@ioannis](https://community.thunkable.com/u/ioannis)\
**Post date:** [October 26, 2023, 5:51pm UTC](https://community.thunkable.com/t/solved-how-do-save-switch-state/454921/10 "2023-10-26T17:51:18Z")

</div>


