# Write specific value at a certain time (max of day at 23:58)

**URL:** <https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668>\
**Category:** General\
**Created:** [4 December 2022 13:11 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668 "2022-12-04T13:11:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![realtime](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@realtime](https://discourse.nodered.org/u/realtime)\
**Post date:** [4 December 2022 13:11 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/1 "2022-12-04T13:11:16Z")

</div>

Hallo,  
I am new to NR. I come from time based scripting and this event based stuff gives me headache.  
What I read from tutorials is always **use flows** (instead of function-node).

what it should do:  
find the max of day of a power meter (shelly3EM 3xmqtt)  
I realized this with a join\_to\_arry[0-2] and sum it function-node updating if bigger than context

I found a changenode JSONata variant for the sum  
$sum(payload)/1000 (in KILO watt)  
but I see no point of advantage  
neither the change-node nor the function-node is making it abvios from the outside unless I rename the time

in Simulink I would use a max-block  
max(z^-1;newvalue)  
but I new find any node with two input-points

What is best practice in node-red if I should use no function block? (and refuse to use coding in core-nodes)  
(I could live with this right now but I have more questions to come up)

---

<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:** [4 December 2022 13:47 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/2 "2022-12-04T13:47:32Z")

</div>

There is no reason to avoid the function node. Sometimes it my even be the "right" way. It is recommended to use the regular core & contrib nodes where you can (as opposed to writing complete applications in a function node) so that you get the "visual flow" at a glance.

For example...

I think the below flow (a mix of functions and regular nodes) is a very reasonable solution to my current task...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/158dd2ca4900a5d23bba9fc63617202fdffe06da.png)

However I could easily have written it like this...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c6134bf9ac4c0655857d1e970f427c67944e448.png)

But I might as well be writing a node.js application in this 👆 case. I also lose the ability to add debug nodes in various locations to understand what is happening.

---

<div class="post-metadata">

**Author:** ![realtime](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@realtime](https://discourse.nodered.org/u/realtime)\
**Post date:** [4 December 2022 13:53 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/3 "2022-12-04T13:53:12Z")

</div>

need to lean to paste code first ...

---

<div class="post-metadata">

**Author:** ![realtime](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@realtime](https://discourse.nodered.org/u/realtime)\
**Post date:** [4 December 2022 13:59 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/4 "2022-12-04T13:59:39Z")

</div>

cannot even quote

---

<div class="post-metadata">

**Author:** ![realtime](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@realtime](https://discourse.nodered.org/u/realtime)\
**Post date:** [4 December 2022 14:06 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/5 "2022-12-04T14:06:52Z")

</div>

what I finally want is to write the 'max\_of\_the\_day' to a database at 23:58 o'clock.  
ever time base example triggers a debug  
so I can write 23:59 to a debug  
I could write a fixed string with a triggered change node to a debug (or database)

but what I want is search\_for\_max\_value (must run continously) and  
write\_at\_23\_58\_clock  
must run once a day

joining all the information (time and 3 powers) and spliting them inside a function block again and sending two message out of the function block seems weired

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2022 14:31 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/6 "2022-12-04T14:31:50Z")

</div>

> [@realtime](#):
>
> what I finally want is to write the 'max\_of\_the\_day' to a database at 23:58 o'clock.

Why don't you just write them all to the db and use the db to give you the max when you need it?

What db are you using?

---

<div class="post-metadata">

**Author:** ![RandomSporadicProjs](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@RandomSporadicProjs](https://discourse.nodered.org/u/RandomSporadicProjs)\
**Post date:** [4 December 2022 14:37 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/7 "2022-12-04T14:37:45Z")

</div>

You could do this in a timed manner, but I had a similar type of need, and just do it in a function node using a database (MYSQL) query

```auto
msg.topic = "insert into PowerPlugs (`Date`, `PlugId`, `kWh Today`) values ('" + msg.payload["Time"].substring(0,10) +"', '"+ msg.plug +"', " + msg.payload["ENERGY"]["Today"] + ") ON DUPLICATE KEY UPDATE `kWh Today` = GREATEST(`kWh Today`, " + msg.payload["ENERGY"]["Today"] + "); ";

```

The database table has 3 fields, Date, PlugId and kWh Today in my case, and the primary key for the table is Date and PlugId. So if a particular plug has a value recorded in the database for a given date, then the stored value will be the biggest between the currently saved value, and the new one.

HTH

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [4 December 2022 14:48 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/8 "2022-12-04T14:48:44Z")

</div>

Here is a no-code approach: an inject node to trigger a flow at 23:58 and a flow variable to retain the maximum value seen that day. For simplicity I have assumed the database automatically adds the timestamp to new records.

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/018778bfda9f12854669ac9ec9f331af10eae32d.jpeg)

```auto
[{"id":"edbf92277c2a065b","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"6d2270bd9198cbcb","type":"group","z":"edbf92277c2a065b","name":"Insert into db (MySQL example)","style":{"stroke":"#0070c0","fill":"#bfdbef","fill-opacity":"0.52","label":true},"nodes":["56e196f68da11b69","f71943cb189f095f","7629c42c586946f3"],"x":14,"y":279,"w":672,"h":82},{"id":"25923af295679204","type":"group","z":"edbf92277c2a065b","name":"As readings arrive maintain largest as flow.maxreading","style":{"stroke":"#0070c0","fill":"#bfdbef","fill-opacity":"0.45","label":true},"nodes":["95db38b08620ab31","d13a9934bb100b40","5329268a7b012215","0b6dda8705195704","9c129465334fb50f","af232ceb1dbb0cdd","46ffef4c607e9ec1","d37bde21db120bd4","1958b0d9b0be4ba0"],"x":14,"y":19,"w":972,"h":242},{"id":"56e196f68da11b69","type":"inject","z":"edbf92277c2a065b","g":"6d2270bd9198cbcb","name":"At 23:58","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"58 23 * * *","once":false,"onceDelay":0.1,"topic":"msg.topic = \"INSERT INTO test (maxreading) VALUES(?)\";","payload":"maxreading","payloadType":"flow","x":120,"y":320,"wires":[["f71943cb189f095f"]]},{"id":"f71943cb189f095f","type":"mysql","z":"edbf92277c2a065b","g":"6d2270bd9198cbcb","mydb":"2381cb15.b563bc","name":"MySQL db","x":290,"y":320,"wires":[["7629c42c586946f3"]]},{"id":"95db38b08620ab31","type":"switch","z":"edbf92277c2a065b","g":"25923af295679204","name":"Is flow.maxreading null?","property":"maxreading","propertyType":"flow","rules":[{"t":"null"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":120,"wires":[["46ffef4c607e9ec1"],["d37bde21db120bd4"]]},{"id":"7629c42c586946f3","type":"change","z":"edbf92277c2a065b","g":"6d2270bd9198cbcb","name":"","rules":[{"t":"delete","p":"maxreading","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":320,"wires":[[]]},{"id":"d13a9934bb100b40","type":"inject","z":"edbf92277c2a065b","g":"25923af295679204","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":110,"y":60,"wires":[["95db38b08620ab31"]]},{"id":"5329268a7b012215","type":"inject","z":"edbf92277c2a065b","g":"25923af295679204","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"17","payloadType":"num","x":110,"y":100,"wires":[["95db38b08620ab31"]]},{"id":"0b6dda8705195704","type":"inject","z":"edbf92277c2a065b","g":"25923af295679204","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"24","payloadType":"num","x":110,"y":140,"wires":[["95db38b08620ab31"]]},{"id":"9c129465334fb50f","type":"inject","z":"edbf92277c2a065b","g":"25923af295679204","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"22","payloadType":"num","x":110,"y":180,"wires":[["95db38b08620ab31"]]},{"id":"af232ceb1dbb0cdd","type":"link in","z":"edbf92277c2a065b","g":"25923af295679204","name":"New Readings Arrive Here","links":[],"x":150,"y":220,"wires":[["95db38b08620ab31"]],"l":true},{"id":"46ffef4c607e9ec1","type":"change","z":"edbf92277c2a065b","g":"25923af295679204","name":"","rules":[{"t":"set","p":"maxreading","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":100,"wires":[[]]},{"id":"d37bde21db120bd4","type":"switch","z":"edbf92277c2a065b","g":"25923af295679204","name":"msg.payload > flow.maxreading?","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"maxreading","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":160,"wires":[["46ffef4c607e9ec1"]]},{"id":"1958b0d9b0be4ba0","type":"comment","z":"edbf92277c2a065b","g":"25923af295679204","name":"Must allow for if flow.maxreading is not set","info":"","x":380,"y":80,"wires":[]},{"id":"2381cb15.b563bc","type":"MySQLdatabase","name":"","host":"127.0.0.1","port":"3306","db":"espresso","tz":"","charset":"UTF8"}]

```

I agree with @Colin though, if you store all the data you are ready when the client (you?) demands the daily average, the number of instances in the day above average, the maximum noted during daylight hours, ...

---

<div class="post-metadata">

**Author:** ![realtime](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@realtime](https://discourse.nodered.org/u/realtime)\
**Post date:** [4 December 2022 16:31 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/9 "2022-12-04T16:31:57Z")

</div>

I don't agree to store everything  
it's influxDB  
and even if storge space is endless N-R does not seem to be optimal for calculations

Doing it the database way is taking advantage of "flow"-wide varables. This also breaks the visible flow.

Then I would rather stick with a functional-node with a continous output for further flowing and one output triggered at the disired time.

My question is not so much about the issue '23:58' itself but all kinds of combination of timed and conditional issues.  
_Like count the time that the power was above 1000 W._

I still lack understanding and I have to overcome my simulink thinking.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [4 December 2022 16:39 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/10 "2022-12-04T16:39:45Z")

</div>

> [@realtime](#):
>
> even if storge space is endless N-R does not seem to be optimal for calculations

The point is to _not_ do the calculations in node-red. Influx will easily tell you the max for the day, or the accumulated total for the day, or last month or whatever.

The database can be configured to run a Continuous Query each night to calculate the total and store it in another Measurement. If you really have no further use for the rest of the data then the real time Measurement can be configured with a two day Retention Period so only the last two days are kept. The amount of storage space that would take is trivial.

If you are already using Influxdb then this is definitely the right way to do it. It is the right tool for the job.

---

<div class="post-metadata">

**Author:** ![realtime](https://avatars.discourse-cdn.com/v4/letter/r/f04885/32.png) [@realtime](https://discourse.nodered.org/u/realtime)\
**Post date:** [16 December 2022 07:40 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/12 "2022-12-16T07:40:20Z")

</div>

Thanks for the inspirations.  
I combined the suggestion to take _a value from the 3rd dimension_ (using 'flow.xx') and querying from influx. So I am usung influx as the 3rd dimension.  
running on a pi2\_1gb the querys should not be too complex so I still use the function node.  
query (energymeter at start of day) and (energymeter at end of day) and do the  
(energy of the day calculation) in a node red function block works  
query((energymeter at end of day)-(energymeter at start of day)) freezes the system for 40 minutes.

---

<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 February 2023 07:40 UTC](https://discourse.nodered.org/t/write-specific-value-at-a-certain-time-max-of-day-at-23-58/71668/13 "2023-02-14T07:40:24Z")

</div>

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