Report date and time of NODE-RED flow in Home Assistant

Something is wrong with your setup.
Are you using a new entity or do you reuse a old one that possibly is used in an automation?

1 Like

I have created a new entity and changed the name in Node-RED (Entity-ID and Data sections) and restarted Home Assistant. After executing the flow in Node-red, entity was updated. However after a new restart, the entity has changed again to 01-01-1970.

Have you changed the editable to true?
Your picture above says editable false.

1 Like

Please see below, the latest status of input_datetime.testset:

That looks correct.
And that is not preserved when you restart?. Sounds strangeā€¦

1 Like

After HA restart, it looks like this:

I will dive in it again this evening. Thanks for your help so far!

In your config did you by chance set the initial attribute? Input Datetime - Initial Attribute

1 Like

I didnā€™t use the initial parameter right now. Should I do that?

No. That can reset your input_datetime on a restart.

1 Like

Yes, I found a solution! It looks like that there is a difference between generating a input_datetime via Configuration - Helpers and via configuration.yaml. Now I have added it via configuration.yaml and now it is working. :grinning:

input_datetime:
  beregening_datetime:
    has_date: true
    has_time: true

  - platform: template
    sensors:
      datum_tijd_laatste_beregening:
        entity_id: input_datetime.beregening_datetime
        value_template: >
          {{ as_timestamp(states('input_datetime.beregening_datetime')) | timestamp_custom('%m-%d-%Y %H:%M') }}

Besides that, I have added a template platform to make sure that the date is being displayed correctly. However, that is very difficult. I have tried a lot of different things to get the date right, unfortunately without success. The date is being displayed in US format:

I would like to see it in the European format like: dd-mm-yyyy hh:mm, so basically exchange the day and the month.

I have updated the template to the following code. The entity looks good, when I check the entity under Developer Tools in Home Assistant, however in Lovelace it is being displayed as ā€˜Invalid dateā€™.

{{ as_timestamp(states('input_datetime.test123')) | timestamp_custom('%d-%m-%Y %H:%M') }}

I have exported the Node-Red flow to make it possible to see the whole story:

[{"id":"6aa7f865.adda18","type":"api-call-service","z":"d571caff.44b17","name":"Date & Time","server":"c725871c.ea75e8","version":1,"debugenabled":false,"service_domain":"input_datetime","service":"set_datetime","entityId":"input_datetime.beregening_datetime","data":"{\"entity_id\":\"input_datetime.beregening_datetime\",\"datetime\":\"{{time}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":510,"y":320,"wires":[[]]},{"id":"c725871c.ea75e8","type":"server","z":"","name":"Home Assistant","addon":true}]

Who knows what to do to solve this last part of the puzzle? Thanks in advance for your support!

Yup that is a downside of Home Assistant, itā€™s forced on you with American date formats.
But at least they ainā€™t getting on our metrics!! :smiley:

Anyways, if you instead of using a datetime use an input_text then what you type in will be preserved.
But then it will be harder to use as a date.
So letā€™s say you want to know how long ago that happened then you canā€™t just take the timestamps, you would have to parse the dates.

That could work, I donā€™t know.

So it all depends on what you need it for.
You could live with it, replace it with input text or have both.
One for displaying on Lovelace and one to use for calculations you may need.

1 Like

I only want to use this date in Lovelace to see when the irrigation system in the garden has been activated. What would be your advice in this situation?

In that case input_text.

You only need to change the entity and ā€œdatetimeā€ to ā€œvalueā€ in the call service node.
(If you still have it the way I posted a while ago. Iā€™m in bed and have not looked at your export).
And in the JavaScript of the function node itā€™s quite obvious that you need to change the values to what you want.
All the values are ā€œlabeledā€ with getYear, getMonth and so onā€¦

When youā€™re done please post the flow :slight_smile:

So it didnā€™t work?
Or did you not even try?

Hi, I didnā€™t have time to give it a try. Will try it tomorrow and let you know the results.

Hi, I have tried to make it working. However it does not work at the moment. The first step I did is to create a input_text entity and updated the platform template with the right entities names (see below):

  - platform: template
    sensors:
      datum_tijd_laatste_beregening:
        entity_id: input_text.beregening_text
        value_template: >
          {{ as_timestamp(states('input_text.beregening_text')) | timestamp_custom('%m-%d-%Y %H:%M') }}
    
input_text:
  beregening_text:

After that, I have updated the Node-RED flow and updated the Service Call node. I didnā€™t update the function Node as it was not clear for me what needs to be updated.

[{"id":"d571caff.44b17","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"24add849.24bbe","type":"inject","z":"d571caff.44b17","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":320,"wires":[["d8418df5.3cd538"]]},{"id":"d8418df5.3cd538","type":"function","z":"d571caff.44b17","name":"","func":"var d = new Date(); \nmsg.time =  d.getFullYear() + \"-\" + ('0' + (d.getMonth()+1)).slice(-2) + \"-\" + d.getDate() + \" \" + ('0' + (d.getHours())).slice(-2) + \":\" + ('0' + (d.getMinutes())).slice(-2) + \":\" + ('0' + (d.getSeconds())).slice(-2);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":355,"y":320,"wires":[["6aa7f865.adda18"]],"l":false},{"id":"6aa7f865.adda18","type":"api-call-service","z":"d571caff.44b17","name":"Date & Time","server":"c725871c.ea75e8","version":1,"debugenabled":false,"service_domain":"input_text","service":"set_value","entityId":"input_text.beregening_text","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":490,"y":300,"wires":[[]]},{"id":"c725871c.ea75e8","type":"server","z":"","name":"Home Assistant","addon":true}]

The current result (input_text.beregening.txt):

It looks this value is updated every time when I execute the Node-RED flow. However the date is not changing, the date below is always being displayed.

Datum laatste beregening (which is displaying input_text.beregening.txt in the right format):

Schermafbeelding 2020-08-19 om 16.51.38

You have replaced the {{time}} with {{payload}} here:


That is what is causing it. I assume the payload in this case is the timestamp sent from the inject node and that is then inserted as unix time (seconds since 1970).

Anyways all you need to do is to change payload to time. And that is because the function node before it looks like below.
I understand that you are not a programmer but, lets work our way through the code.

// create a new variable called "d" and assign a new date to it (now)
var d = new Date(); 

// here we use the message (msg) in node red, as you might have seen in node red you usually have msg.payload, msg.topic, msg.something...
// But here we say create a new one called msg.time.
Assign a string to it with the following value:
 //        full year (2020)    -     month with leading zero              -     date       (space)    hour with leading zero           :         minutes with leading zero        :          seconds with leading zero
msg.time =  d.getFullYear() + "-" + ('0' + (d.getMonth()+1)).slice(-2) + "-" + d.getDate() + " " + ('0' + (d.getHours())).slice(-2) + ":" + ('0' + (d.getMinutes())).slice(-2) + ":" + ('0' + (d.getSeconds())).slice(-2);

// and now return the message to the sequence in node red.
return msg;

So that long line is essentially building a string like ā€œ2020-08-19 20:53:05ā€. (Iā€™m not sure if date needs leading zero but weā€™ll see in two weeks).
If you move the parts around then the date time will look different in the entity of HA.

1 Like

Thanks for your help and explanation. It really helped me. Is it possible to use a platform template to change the date in Lovelace from:

to:

I understand that the input_text entity cannot be treated as a date, but maybe it is possible to change the font size and remove the line under the text.