HA date data capture

Hello,
before making the post I searched for information but I couldn't find a solution.
I have modified the config.js document with " process.env.TZ = "Europe/Madrid";" But I can't get the solution.
When capturing a poll state, the state of "last_changed:" appears two hours below reality, while in HA it appears correct.
Thank you.

Welcome to the forum @fAdell87.

Unfortunately I think you might have to ask this on a Home Assistant forum as few here use HA, preferring to do everything in node-red, without the complication of HA.

What is the input, give an example in text format?
You maybe able to use JSONata and $moment() to correct the timezone.

$moment($$.last_changed).tz("Europe/Madrid").format()

assuming a timestamp or ISO timestamp . msg.last_changed

Thank you. I have looked for the section of the forum that talks about HA to be able to make the query. I hope someone who uses HA can help me.

Hello E1cid,

What I get is:
last_changed: "2023-09-07T17:48:45.698Z"

but in Home assistant, the information is:

September 7, 2023, 7:48:45 PM

From the HA the correct information appears, but when I send the information to Node-RED, it is 2 hours less. I have tried to change the time zone of node red and I have made the configuration in the config.js file, but the information continues to be entered incorrectly.

One thing that seems curious to me is that if I use the DATE/TIME FORMATER module and configure the OUTPUT FORMAT as HH:mm, the payload is correct without having to adjust the hours. However, if I leave the OUTPUT FORMAT empty, the payload is with the wrong time. In both cases the data within the JSON is with the incorrect time.

Example flow

[{"id":"e96d996e7afed4b4","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"last_changed","v":"2023-09-07T17:48:45.698Z","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":550,"y":240,"wires":[["43dc3685eff5e542"]]},{"id":"43dc3685eff5e542","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"last_changed","pt":"msg","to":"$moment($$.last_changed).tz(\"Europe/Madrid\").format()","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":240,"wires":[["aa16fb7889d3fdae"]]},{"id":"aa16fb7889d3fdae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 329","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"last_changed","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":240,"wires":[]}]

input 2023-09-07T17:48:45.698Z UTC ISO timestamp
output 2023-09-07T19:48:45+02:00 local ISO timestamp

You can use the format() to output other formats
e,g,
format("YYYYMMDD HH:mm:ss")

How to import a flow

Send and storing timestamps in UTC is the way to go, then format to local time for display.

Thank you for the quick response and possible solution, but this solution requires me to carry out the intermediate step to be able to modify the date/time of all the entities I want to work with.
Again, thanks for your help, but is there any option to get the correct information directly without having to perform this intermediate step?

Basically, it is to avoid possible human errors.

I am working on a temperature check, hourly getting the status and it has to be within hours.

The problem is that whenever I want to obtain the modification date of a state, I will have to be performing the conversion, which is why I think it would be more interesting to receive the correct information directly.

[{"id":"62081cfcc737c804","type":"group","z":"3e394ad9d5976fe2","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["c9f9b37dca365a8a","618e705f72a78b77","1f5dafc5249b990f","eea3b83c611e7051","cc2c8dcfe94677f1","f71c8062e9ecd5c0","a8bcc30d015b3526","b4e9a6ed0aa01ebc","9a1ff3bc27d56384","6aa57bd4c738f0b6","1bf07fa36e6c13bc","a887f9cfddeda9c1","fc24487acf441de5","888e78b222038a54","d65b76f8be8cc0ac","edd7b233745b058f"],"x":14,"y":19,"w":952,"h":442},{"id":"c9f9b37dca365a8a","type":"poll-state","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"tmp.virt.","server":"278c5c9f.18b2c4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"5","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":true,"outputonchanged":true,"entity_id":"input_number.virtual_temp_actual","state_type":"num","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":90,"y":120,"wires":[["fc24487acf441de5"]]},{"id":"618e705f72a78b77","type":"function","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"Dentro de horario","func":"var HoraActual = msg.hora_actual;\nvar HoraInicio = msg.hora_inicio;\nvar HoraFinal = msg.hora_final;\n\n\n    if (HoraActual >= HoraInicio && HoraActual <= HoraFinal) {\n        msg.dentro_horario = true;\n        \n    } else {\n        msg.dentro_horario = false;\n       \n    }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":220,"wires":[["b4e9a6ed0aa01ebc"]],"outputLabels":["TRUE"]},{"id":"1f5dafc5249b990f","type":"moment","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"","topic":"","input":"","inputType":"date","inTz":"Europe/Madrid","adjAmount":"2","adjType":"hours","adjDir":"add","format":"","locale":"C","output":"hora_actual","outputType":"msg","outTz":"Europe/Madrid","x":545,"y":120,"wires":[["eea3b83c611e7051"]],"l":false,"info":"Captura la hora actual i la fa un format a tractar"},{"id":"eea3b83c611e7051","type":"api-current-state","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"","server":"278c5c9f.18b2c4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_boolean.virtual_radiador","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"radiador","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":665,"y":120,"wires":[["cc2c8dcfe94677f1"]],"l":false,"info":"Demana a HA el estat actual del radiador"},{"id":"cc2c8dcfe94677f1","type":"api-current-state","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"","server":"278c5c9f.18b2c4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.temp_objetivo","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"tmp_objetivo","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":805,"y":120,"wires":[["618e705f72a78b77"]],"l":false,"info":"Demana a HA la Temperatura Objectiu"},{"id":"f71c8062e9ecd5c0","type":"api-call-service","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"encendre radiador","server":"278c5c9f.18b2c4","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.virtual_radiador"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":850,"y":300,"wires":[[]]},{"id":"a8bcc30d015b3526","type":"api-call-service","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"apagar radiador","server":"278c5c9f.18b2c4","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.virtual_radiador"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":840,"y":420,"wires":[[]]},{"id":"b4e9a6ed0aa01ebc","type":"switch","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"estat radiador","property":"radiador","propertyType":"msg","rules":[{"t":"eq","v":"off","vt":"str"},{"t":"eq","v":"on","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":140,"y":340,"wires":[["9a1ff3bc27d56384"],["1bf07fa36e6c13bc"]]},{"id":"9a1ff3bc27d56384","type":"switch","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"dins horari","property":"dentro_horario","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":320,"wires":[["6aa57bd4c738f0b6"],[]]},{"id":"6aa57bd4c738f0b6","type":"switch","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"objetivo > actual¿?","property":"tmp_objetivo","propertyType":"msg","rules":[{"t":"gt","v":"temp_actual","vt":"msg"},{"t":"lt","v":"temp_actual","vt":"msg"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":300,"wires":[["f71c8062e9ecd5c0"],[]]},{"id":"1bf07fa36e6c13bc","type":"switch","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"dins horari","property":"dentro_horario","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":360,"wires":[["a887f9cfddeda9c1"],["a8bcc30d015b3526"]]},{"id":"a887f9cfddeda9c1","type":"switch","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"objetivo > actual¿?","property":"tmp_objetivo","propertyType":"msg","rules":[{"t":"gt","v":"temp_actual","vt":"msg"},{"t":"lt","v":"temp_actual","vt":"msg"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":360,"wires":[[],["a8bcc30d015b3526"]]},{"id":"fc24487acf441de5","type":"change","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"","rules":[{"t":"set","p":"temp_actual","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":165,"y":120,"wires":[["bfe21ce5d51d0b86"]],"l":false,"info":"Agafa el valor de msg.payload (la temperatura actual)\r\ni crea una nova variable que es msg.temp_actual amb aquest valor"},{"id":"888e78b222038a54","type":"group","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"","style":{"label":true},"nodes":["bfe21ce5d51d0b86","abc303688d372c24"],"x":204,"y":79,"w":122,"h":82,"info":"Demana la informació de hora de inici de HA.\r\nla converteix a un format per a poder tractar\r\n"},{"id":"bfe21ce5d51d0b86","type":"api-current-state","z":"3e394ad9d5976fe2","g":"888e78b222038a54","name":"hora inicio","server":"278c5c9f.18b2c4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.hora_inicio","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":245,"y":120,"wires":[["abc303688d372c24"]],"l":false},{"id":"abc303688d372c24","type":"moment","z":"3e394ad9d5976fe2","g":"888e78b222038a54","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Madrid","adjAmount":"2","adjType":"hours","adjDir":"add","format":"","locale":"C","output":"hora_inicio","outputType":"msg","outTz":"Europe/Madrid","x":285,"y":120,"wires":[["b7822b1bf88fce79"]],"l":false},{"id":"d65b76f8be8cc0ac","type":"group","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"","style":{"label":true},"nodes":["b7822b1bf88fce79","71ea6e2cb8538b8d"],"x":364,"y":79,"w":132,"h":82,"info":"Demana la informació de hora de inici de HA.\r\nla converteix a un format per a poder tractar\r\n"},{"id":"b7822b1bf88fce79","type":"api-current-state","z":"3e394ad9d5976fe2","g":"d65b76f8be8cc0ac","name":"hora fin","server":"278c5c9f.18b2c4","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.hora_fin","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":405,"y":120,"wires":[["71ea6e2cb8538b8d"]],"l":false},{"id":"71ea6e2cb8538b8d","type":"moment","z":"3e394ad9d5976fe2","g":"d65b76f8be8cc0ac","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Europe/Madrid","adjAmount":"26","adjType":"hours","adjDir":"add","format":"","locale":"C","output":"hora_final","outputType":"msg","outTz":"Europe/Madrid","x":455,"y":120,"wires":[["1f5dafc5249b990f"]],"l":false},{"id":"edd7b233745b058f","type":"comment","z":"3e394ad9d5976fe2","g":"62081cfcc737c804","name":"Control temperatura","info":"","x":450,"y":60,"wires":[]},{"id":"278c5c9f.18b2c4","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","statusYear":"numeric","statusMonth":"numeric","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m:s","enableGlobalContextStore":false}]

Again sending and storing in UTC is correct and saves errors later. You then format for display as the machine may be in Tawain, Brazil or Spain. Each gets a UTC timestamp, then formats to display in local time.

You can format for hour using using format("H") which will return the hour.

If you want to find if the timestamps is within a time limit do not format(), use $moment($$.last_changed).diff() you can use a switch node to see if output is > x seconds, hours, days, etc.

example flow check to see how long between inputs.

[{"id":"e96d996e7afed4b4","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"last_changed","v":"$moment().utc().format()","vt":"jsonata"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":650,"y":240,"wires":[["43dc3685eff5e542","7809e82b15c38aef"]]},{"id":"43dc3685eff5e542","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.last_changed).diff($moment($flowContext(\"last\")), \"seconds\")","tot":"jsonata"},{"t":"set","p":"last","pt":"flow","to":"last_changed","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":240,"wires":[["c36c99aeb1ad1298"]]},{"id":"7809e82b15c38aef","type":"debug","z":"b9860b4b9de8c8da","name":"debug 331","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"last_changed","targetType":"msg","statusVal":"payload","statusType":"auto","x":810,"y":160,"wires":[]},{"id":"c36c99aeb1ad1298","type":"switch","z":"b9860b4b9de8c8da","name":"greater than 5 seconds","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"5","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1040,"y":240,"wires":[["aa16fb7889d3fdae"],["2fb45c6b58d96048"]]},{"id":"aa16fb7889d3fdae","type":"debug","z":"b9860b4b9de8c8da","name":"debug 329","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1150,"y":140,"wires":[]},{"id":"2fb45c6b58d96048","type":"debug","z":"b9860b4b9de8c8da","name":"debug 330","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1150,"y":300,"wires":[]}]

p.s. It is best to give all relevant info in first post, then others will have all the information to give an informed answer.

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