# Countdown from Number

**URL:** https://discourse.nodered.org/t/countdown-from-number/77273
**Category:** General
**Created:** [5 April 2023 12:21 UTC](https://discourse.nodered.org/t/countdown-from-number/77273 "2023-04-05T12:21:31Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![BWTH](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@BWTH](https://discourse.nodered.org/u/BWTH)
#### Post date: [5 April 2023 12:21 UTC](https://discourse.nodered.org/t/countdown-from-number/77273/1 "2023-04-05T12:21:31Z")

</div>

Hi  
I am trying to build an countdown from a number (7777777777).  
Every 4 Seconds it should decrease at 1  
And this number is shown in Dashboard.  
That works all.  
Now my Question: If the Raspyery gets off from Power and restarts the Number starts decreasing from 7777777777. i want to start decreasing from the number where the raspberry get out of power. so i have to give the latest output in an variable, so i think. but how can i do that?  
Thanks for Help

Here is my flow

```auto
[
    {
        "id": "3bcdcc3bcf7c430e",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "0bdf612159e2a8ec",
        "type": "function",
        "z": "3bcdcc3bcf7c430e",
        "name": "function 2",
        "func": "var count = context.get('count') || 0;\ncount += 1;\nmsg.payload = Number (7777777777) - count;\ncontext.set('count', count);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 180,
        "wires": [
            [
                "7fbee56ef37d9748",
                "5f8d5d36cf1f74d6",
                "82d96936305edca3"
            ]
        ]
    },
    {
        "id": "d4b4a1e8b2e8fa31",
        "type": "inject",
        "z": "3bcdcc3bcf7c430e",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "4",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "ON",
        "payloadType": "str",
        "x": 190,
        "y": 180,
        "wires": [
            [
                "0bdf612159e2a8ec"
            ]
        ]
    },
    {
        "id": "7fbee56ef37d9748",
        "type": "debug",
        "z": "3bcdcc3bcf7c430e",
        "name": "debug 4",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 640,
        "y": 140,
        "wires": []
    },
    {
        "id": "5f8d5d36cf1f74d6",
        "type": "ioBroker out",
        "z": "3bcdcc3bcf7c430e",
        "name": "Counter",
        "topic": "0_userdata.0.Counter",
        "ack": "false",
        "autoCreate": "false",
        "stateName": "",
        "role": "",
        "payloadType": "",
        "readonly": "",
        "stateUnit": "",
        "stateMin": "",
        "stateMax": "",
        "x": 640,
        "y": 260,
        "wires": []
    },
    {
        "id": "82d96936305edca3",
        "type": "ui_text",
        "z": "3bcdcc3bcf7c430e",
        "group": "316dca5a2cf81cf0",
        "order": 0,
        "width": "20",
        "height": "6",
        "name": "",
        "label": "Counter",
        "format": "{{msg.payload}}",
        "layout": "col-center",
        "className": "",
        "x": 640,
        "y": 200,
        "wires": []
    },
    {
        "id": "316dca5a2cf81cf0",
        "type": "ui_group",
        "name": "Standard",
        "tab": "0a3fda28b8bfc042",
        "order": 1,
        "disp": false,
        "width": "20",
        "collapse": false,
        "className": ""
    },
    {
        "id": "0a3fda28b8bfc042",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": true
    }
]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 April 2023 12:33 UTC](https://discourse.nodered.org/t/countdown-from-number/77273/2 "2023-04-05T12:33:40Z")

</div>

Are you using file based context or memory?  
[https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system](https://nodered.org/docs/user-guide/context#saving-context-data-to-the-file-system)  
file will survive a reboot

---

<div class="post-metadata">

### Author: ![BWTH](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@BWTH](https://discourse.nodered.org/u/BWTH)
#### Post date: [5 April 2023 13:45 UTC](https://discourse.nodered.org/t/countdown-from-number/77273/3 "2023-04-05T13:45:57Z")

</div>

Hi  
Thanks for the info  
That is to much for mee, i am not shure where to set the parameters

1. make my psg.payload to a variabel  
2 store the variabel to context  
3...how can i but the context back in the funktion to do the encreasing from it

if tried this, do you have any tips for me, thanks

```auto
msg.payload = var countnew

// store the value back

context.set('countnew', countnew);

// make it part of the outgoing msg object

msg.payload = countnew;

return msg;

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [5 April 2023 14:19 UTC](https://discourse.nodered.org/t/countdown-from-number/77273/4 "2023-04-05T14:19:56Z")

</div>

Again unless you use file context your var will not survive a reboot.  
[https://nodered.org/docs/user-guide/runtime/settings-file](https://nodered.org/docs/user-guide/runtime/settings-file)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [5 April 2023 14:36 UTC](https://discourse.nodered.org/t/countdown-from-number/77273/5 "2023-04-05T14:36:34Z")

</div>

This might help you get a better understanding of persistent context:

> [@A guide to understanding 'Persistent Context'](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115):
>
> This is something I wrote up after playing with the context options. I've filed a PR with some of this added to the Node-RED documentation but thought I'd release it here also. I hope people find it useful. Paul A guide to understanding 'Persistent Context' Prior to Node-RED v0.19.0, data could be stored in context as a global, flow or node. This was stored in memory and would be reset with a restart of Node-RED. As of v.0.19.0 you can now store node, flow and global context data in memory O…

---

<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: [4 June 2023 14:36 UTC](https://discourse.nodered.org/t/countdown-from-number/77273/6 "2023-06-04T14:36:45Z")

</div>

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