script not proper old vs new node-red version

Moderator: leecollings

Post Reply
edje1205
Posts: 34
Joined: Tuesday 20 December 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Amsterdam Nieuwendam
Contact:

script not proper old vs new node-red version

Post by edje1205 »

Hi all

As I had a very well working node-red script from FireWizard 5 years ago I had to renew it to a new Domoticz version (not compatible)
See this forum
The script what is still working in old node-red gives me errors in new node-red version (latest):
'The workspace contains some nodes that are not properly configured.

Its all about hum_stat and bar_for;
Cannot find name 'hum_stat'.(2304)

See attachements old and new node-red versions

What the difference and the solution to let it function proper again ?

Code: Select all

// This function sorts the comfort level for Domoticz based on the humidity.

function comfortlevel(hum) {
    
switch (true) {
    case hum > 70: { hum_stat = 3 } break;
    case hum < 30: { hum_stat = 2 } break;
    case hum >= 30 && hum <= 45: { hum_stat = 0 } break;
    case hum > 45 && hum <= 70: { hum_stat = 1 } break;
    default: { hum_stat = 0 } break;
}
return hum_stat;
}

// This function sorts the prediction (barometer forecast) for Domoticz based on the barometer pressure.

function bar_forecast(pres) {

switch (true) {
    case pres > 1030: { bar_for = 1 } break;
    case pres > 1010 && pres <= 1030: { bar_for = 2 } break;
    case pres > 990 && pres <= 1010: { bar_for = 3 } break;
    case pres > 970 && pres <= 990: { bar_for = 4 } break;
    default: { bar_for = 0 } break;
}
return bar_for;
}

// This function sorts the wind direction in abbreviated text for Domoticz based on the average bearing.

function winddir(avgb) {

switch (true) {
    case avgb >= 0 && avgb <= 11.25: { dir = "N" } break;
    case avgb >= 11.26 && avgb <= 33.75: { dir = "NNE" } break;
    case avgb >= 33.76 && avgb <= 56.25: { dir = "NE" } break;
    case avgb >= 56.26 && avgb <= 78.75: { dir = "ENE" } break;
    case avgb >= 78.76 && avgb <= 101.25: { dir = "E" } break;
    case avgb >= 101.26 && avgb <= 123.75: { dir = "ESE" } break;
    case avgb >= 123.76 && avgb <= 146.25: { dir = "SE" } break;
    case avgb >= 146.26 && avgb <= 168.75: { dir = "SSE" } break;
    case avgb >= 168.76 && avgb <= 191.25: { dir = "S" } break;
    case avgb >= 191.26 && avgb <= 213.75: { dir = "SSW" } break;
    case avgb >= 213.76 && avgb <= 236.25: { dir = "SW" } break;
    case avgb >= 236.26 && avgb <= 258.75: { dir = "WSW" } break;
    case avgb >= 258.76 && avgb <= 281.25: { dir = "W" } break;
    case avgb >= 281.26 && avgb <= 303.75: { dir = "WNW" } break;
    case avgb >= 303.76 && avgb <= 326.25: { dir = "NW" } break;
    case avgb >= 326.26 && avgb <= 348.75: { dir = "NNW" } break;
    case avgb >= 348.76 && avgb <= 359.99: { dir = "N" } break;
    default: { dir = "?" } break;
}
return dir;
}

var ba = msg.payload.press.toString(); //barometer pressure
var pr = bar_forecast(ba).toString(); // prediction (barometer forecast)
var tm = msg.payload.temp.toString(); // temperature
var hu = msg.payload.hum.toString(); // humidity
var lv = comfortlevel(hu).toString(); // comfort level
var rt = msg.payload.rrate.toString(); // rain rate
var fl = msg.payload.rfall.toString(); // rain fall
var sr = msg.payload.SolarRad.toString(); // solar radiation
var uv = msg.payload.UV.toString(); // UV radiation
var an = msg.payload.bearing.toString(); // wind bearing
var sp = (msg.payload.wspeed * 10).toString(); // wind speed in 0.1 m/s
var gu = (msg.payload.wgust * 10).toString(); // wind gust in 0.1 m/s
var ch = msg.payload.wchill.toString(); // chill temperature
var avgbr = msg.payload.avgbearing; // average bearing
var dr = winddir(avgbr); // wind direction in abbreviated text
var txt = msg.payload.forecast; // weather forecast
var dp = msg.payload.dew.toString(); // dew point

var msg1 = {};
var msg2 = {};
var msg3 = {};
var msg4 = {};
var msg5 = {};
var msg6 = {};
var msg7 = {};
var msg8 = {};
var msg9 = {};
var msg10 = {};
var msg11 = {};
var msg12 = {};
var msg13 = {};

msg1.payload = {"command":"udevice", "idx":30, "svalue":tm};
msg2.payload = {"command":"udevice", "idx":31, "nvalue":parseInt(hu), "svalue":lv};
msg3.payload = {"command":"udevice", "idx":32, "svalue":ba + ";" + pr};
msg4.payload = {"command":"udevice", "idx":33, "svalue":tm + ";" + hu + ";" + lv};
msg5.payload = {"command":"udevice", "idx":34, "svalue":tm + ";" + hu + ";" + lv + ";" + ba + ";" + pr};
msg6.payload = {"command":"udevice", "idx":35, "svalue":tm + ";" + ba + ";" + pr};
msg7.payload = {"command":"udevice", "idx":36, "svalue":rt + ";" + fl};
msg8.payload = {"command":"udevice", "idx":37, "svalue":an + ";" + dr + ";" + sp + ";" + gu + ";" + tm + ";" + ch};
msg9.payload = {"command":"udevice", "idx":38, "svalue":an + ";" + dr + ";" + sp + ";" + gu + ";" + tm + ";" + ch};
msg10.payload = {"command":"udevice", "idx":39, "svalue":uv + ";" + "0"};
msg11.payload = {"command":"udevice", "idx":40, "svalue":sr};
msg12.payload = {"command":"udevice", "idx":41, "svalue":txt};
msg13.payload = {"command":"udevice", "idx":42, "svalue":dp};

return [[msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13]];
'


Thanks in advance !

Ed
Attachments
node-red_oud.jpg
node-red_oud.jpg (83.88 KiB) Viewed 786 times
node-red_new.jpg
node-red_new.jpg (101.87 KiB) Viewed 786 times
User avatar
habahabahaba
Posts: 232
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: script not proper old vs new node-red version

Post by habahabahaba »

the variable is not declared

try to set
var hum_stat = 0;
to the fourth line

the same to bar_for
User avatar
FireWizard
Posts: 1863
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script not proper old vs new node-red version

Post by FireWizard »

Hello @edje1205

You wrote:
As I had a very well working node-red script from FireWizard 5 years ago
That is a long time ago already, but I still do remember this flow.

I assume, that you upgraded your Node-RED to version 4.0.8.

Can you send me your complete flow (not only the contents of the "Function" node, so that I can take a look to it in my test system.

Regards
User avatar
FireWizard
Posts: 1863
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script not proper old vs new node-red version

Post by FireWizard »

Hello @edje1205;

I asked you:
Can you send me your complete flow (not only the contents of the "Function" node, so that I can take a look to it in my test system.
This is not needed anymore as I already loaded the flow in my test system.

In December 2020, you must have used an older version of Node-RED. Probably something like 1.2.6.
This old version of NR was obviously not that strict on declarations of functions in a "Function" node and
that version did not show the missing declarations.
@habahabahaba mentioned also that these declarations were missing. I added them.
The flow ran well, even without the declarations, but this looks much better.

I added 3 declarations as follows:

Code: Select all

let hum_stat;
let bar_for;
let dir;
I also updated all other declarations and changed every "var" to "let".

The contents of the "Function" node should now be as follows:

Code: Select all

// This function sorts the comfort level for Domoticz based on the humidity.

let hum_stat;
let bar_for;
let dir;

function comfortlevel(hum) {
    
switch (true) {
    case hum > 70: { hum_stat = 3 } break;
    case hum < 30: { hum_stat = 2 } break;
    case hum >= 30 && hum <= 45: { hum_stat = 0 } break;
    case hum > 45 && hum <= 70: { hum_stat = 1 } break;
    default: { hum_stat = 0 } break;
}
return hum_stat;
}

// This function sorts the prediction (barometer forecast) for Domoticz based on the barometer pressure.

function bar_forecast(pres) {

switch (true) {
    case pres > 1030: { bar_for = 1 } break;
    case pres > 1010 && pres <= 1030: { bar_for = 2 } break;
    case pres > 990 && pres <= 1010: { bar_for = 3 } break;
    case pres > 970 && pres <= 990: { bar_for = 4 } break;
    default: { bar_for = 0 } break;
}
return bar_for;
}

// This function sorts the wind direction in abbreviated text for Domoticz based on the average bearing.

function winddir(avgb) {

switch (true) {
    case avgb >= 0 && avgb <= 11.25: { dir = "N" } break;
    case avgb >= 11.26 && avgb <= 33.75: { dir = "NNE" } break;
    case avgb >= 33.76 && avgb <= 56.25: { dir = "NE" } break;
    case avgb >= 56.26 && avgb <= 78.75: { dir = "ENE" } break;
    case avgb >= 78.76 && avgb <= 101.25: { dir = "E" } break;
    case avgb >= 101.26 && avgb <= 123.75: { dir = "ESE" } break;
    case avgb >= 123.76 && avgb <= 146.25: { dir = "SE" } break;
    case avgb >= 146.26 && avgb <= 168.75: { dir = "SSE" } break;
    case avgb >= 168.76 && avgb <= 191.25: { dir = "S" } break;
    case avgb >= 191.26 && avgb <= 213.75: { dir = "SSW" } break;
    case avgb >= 213.76 && avgb <= 236.25: { dir = "SW" } break;
    case avgb >= 236.26 && avgb <= 258.75: { dir = "WSW" } break;
    case avgb >= 258.76 && avgb <= 281.25: { dir = "W" } break;
    case avgb >= 281.26 && avgb <= 303.75: { dir = "WNW" } break;
    case avgb >= 303.76 && avgb <= 326.25: { dir = "NW" } break;
    case avgb >= 326.26 && avgb <= 348.75: { dir = "NNW" } break;
    case avgb >= 348.76 && avgb <= 359.99: { dir = "N" } break;
    default: { dir = "?" } break;
}
return dir;
}

let ba = msg.payload.press.toString(); //barometer pressure
let pr = bar_forecast(ba).toString(); // prediction (barometer forecast)
let tm = msg.payload.temp.toString(); // temperature
let hu = msg.payload.hum.toString(); // humidity
let lv = comfortlevel(hu).toString(); // comfort level
let rt = msg.payload.rrate.toString(); // rain rate
let fl = msg.payload.rfall.toString(); // rain fall
let sr = msg.payload.SolarRad.toString(); // solar radiation
let uv = msg.payload.UV.toString(); // UV radiation
let an = msg.payload.bearing.toString(); // wind bearing
let sp = (msg.payload.wspeed * 10).toString(); // wind speed in 0.1 m/s
let gu = (msg.payload.wgust * 10).toString(); // wind gust in 0.1 m/s
let ch = msg.payload.wchill.toString(); // chill temperature
let avgbr = msg.payload.avgbearing; // average bearing
let dr = winddir(avgbr); // wind direction in abbreviated text
let txt = msg.payload.forecast; // weather forecast
let dp = msg.payload.dew.toString(); // dew point

let msg1 = {};
let msg2 = {};
let msg3 = {};
let msg4 = {};
let msg5 = {};
let msg6 = {};
let msg7 = {};
let msg8 = {};
let msg9 = {};
let msg10 = {};
let msg11 = {};
let msg12 = {};
let msg13 = {};

msg1.payload = {"command":"udevice", "idx":30, "svalue":tm};
msg2.payload = {"command":"udevice", "idx":31, "nvalue":parseInt(hu), "svalue":lv};
msg3.payload = {"command":"udevice", "idx":32, "svalue":ba + ";" + pr};
msg4.payload = {"command":"udevice", "idx":33, "svalue":tm + ";" + hu + ";" + lv};
msg5.payload = {"command":"udevice", "idx":34, "svalue":tm + ";" + hu + ";" + lv + ";" + ba + ";" + pr};
msg6.payload = {"command":"udevice", "idx":35, "svalue":tm + ";" + ba + ";" + pr};
msg7.payload = {"command":"udevice", "idx":36, "svalue":rt + ";" + fl};
msg8.payload = {"command":"udevice", "idx":37, "svalue":an + ";" + dr + ";" + sp + ";" + gu + ";" + tm + ";" + ch};
msg9.payload = {"command":"udevice", "idx":38, "svalue":an + ";" + dr + ";" + sp + ";" + gu + ";" + tm + ";" + ch};
msg10.payload = {"command":"udevice", "idx":39, "svalue":uv + ";" + "0"};
msg11.payload = {"command":"udevice", "idx":40, "svalue":sr};
msg12.payload = {"command":"udevice", "idx":41, "svalue":txt};
msg13.payload = {"command":"udevice", "idx":42, "svalue":dp};

return [[msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9,msg10,msg11,msg12,msg13]];
So this model is updated.

Try it and let us know.

Regards
edje1205
Posts: 34
Joined: Tuesday 20 December 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Amsterdam Nieuwendam
Contact:

Re: script not proper old vs new node-red version

Post by edje1205 »

try to set
var hum_stat = 0;
to the fourth line

the same to bar_for
did it !

started from scratch but think need some more advice later

FireWizard noticed your post later on will pay attention tomorrow on it

Thanks again both !!!!
edje1205
Posts: 34
Joined: Tuesday 20 December 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Amsterdam Nieuwendam
Contact:

Re: script not proper old vs new node-red version

Post by edje1205 »

Back again !

Was/Am struggling with RFLink Arduino mega 2560 board (not working send it back)

OT:

Thanks for input both: as said the first option worked prevent errors.



To day I wanted to investigate on FireWizard's script but got a syntax error by importing the script in node-red

Code: Select all

SyntaxError: Unexpected token '/', "// This fu"... is not valid JSON
see att as well

but I don't understand; the script is as same as the original script !? Is it ?

???
Attachments
syntax error node-red json.jpg
syntax error node-red json.jpg (46.83 KiB) Viewed 675 times
User avatar
FireWizard
Posts: 1863
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script not proper old vs new node-red version

Post by FireWizard »

Hello @edje1205,

Oh yes, this is clear to me.

What happened?

The code, I posted in my flow, is not a complete flow or even a part of a flow.
This code is the contents of the "Function" node, that is used in the flow.

To use this, do as follows:

1. Do not use the Node-RED import function!

2. Just press "Select all" and then "Copy" the code and "Paste" this code in the "Function" node.

OR

Use your own existing "Function" node and add

let hum_stat;
let bar_for;
let dir;

as line 3, 4 and 5 in your existing node.

Let me know.

Regards

Regards
edje1205
Posts: 34
Joined: Tuesday 20 December 2016 12:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5877
Location: Amsterdam Nieuwendam
Contact:

Re: script not proper old vs new node-red version

Post by edje1205 »

Thanks

Reply on your post, FireWizard:
Yes thats what I did I have no more errors now !

------------------------------------

A question about Domoticz vs Node-Red what keeps me busy is the warning coming up at the log file of Domoticz:

Code: Select all

2025-02-17 21:35:15.053 Status: Warning: Expecting svalue with at least 4 elements separated by semicolon, 3 elements received ("-0.3;77;3"), notification not sent (Hardware: 5 - Dummy Sensors, ID: 82012, Unit: 1, Type: 54 - Temp + Humidity + Baro, SubType: 1 - THB1 - BTHR918, BTHGN129)
I have 2 forecast devices with those SubTypes and they are updating well, (see att) but I cannot determine or find out from what device the error comes.
Checked all devices in node-red; no result. Or what am I overlooking ????

4 elements separated for 3 elements temp/hum/baro plus ......prediction (barometer forecast ??
Attachments
error flow noder ed.jpg
error flow noder ed.jpg (76.48 KiB) Viewed 320 times
Arnold88
Posts: 1
Joined: Thursday 06 February 2025 13:12
Target OS: OS X
Domoticz version:
Contact:

Re: script not proper old vs new node-red version

Post by Arnold88 »

Your issue is that hum_stat and bar_for are not declared before use Try adding let or var at the beginning of the function, e.g.:
let hum_stat = 0; before switch.Same for bar_for.New Node-RED versions require explicit variable declarations

These errors are common after updates Other variables might also need declarations.Check Node-RED logs, they may show more issues :)
User avatar
FireWizard
Posts: 1863
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script not proper old vs new node-red version

Post by FireWizard »

Hi @edje1205,
You wrote:
Reply on your post, FireWizard:
Yes that's what I did I have no more errors now !
So, this issue has been solved. But you did not said, what you did.
Did you copy/paste the complete contents in a "Function" node or did you edit your existing "Function" node?
A question about Domoticz vs Node-Red what keeps me busy is the warning coming up at the log file of Domoticz:
If I look to your picture and the error message in your Domoticz Log file, I see the following:

1. The error is caused by ID 82012 (or 1405C HEX).
2. Both in the error message and the picture, I see 3 elements are received.
3. I see that this (dummy) sensor is a combined Temperature/Humidity/Barometer device.

If you look at: https://piandmore.wordpress.com/2019/02 ... -domoticz/,
you can read the following:
Temperature+Humidity+Barometer

The temperature+Humidity+Barometer sensor will show just that and an environment level and a prediction. You can send digits for the temperature but only the first is displayed (it is rounded off). The humidity only displays whole numbers (chops off any digits).

{"command":"udevice", "idx":1234, "svalue":"tm;hu;lv;ba;pr"}

where tm is the temperature, hu is the humidity and lv is the environment level, ba is the pressure and pr is the prediction.

lv can have the following values:

0, normal
1, comfortable
2, dry
3, wet

pr can have the following values:

0, no prediction
1, sunny
2, partly cloudy
3, cloudy
4, rain
5, unknown
(oddly enough 6 cloudy/rain does not work for this sensor)
You can see that this device needs 5 elements. Maybe 4, as a missing pr(ediction) may default to 0, no prediction.

You are missing the value for lv, the environment level.

Unfortunately, I cannot tell, why this is missing.
Therefore I have to see the contents of your "Function" node. Can you send this?

Regards
User avatar
waltervl
Posts: 5734
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: script not proper old vs new node-red version

Post by waltervl »

Arnold88 wrote: Tuesday 18 February 2025 13:26 Your issue is that hum_stat and bar_for are not declared before use Try adding let or var at the beginning of the function, e.g.:
let hum_stat = 0; before switch.Same for bar_for.New Node-RED versions require explicit variable declarations

These errors are common after updates Other variables might also need declarations.Check Node-RED logs, they may show more issues :)
I approved this new user comment after FireWizzards answer so perhaps it gives extra explanaition....
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
FireWizard
Posts: 1863
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: script not proper old vs new node-red version

Post by FireWizard »

Hi, @waltervl
I approved this new user comment after FireWizards answer so perhaps it gives extra explanation....
Walter, that's okay.

Both @habahabahaba and myself are aware that variables has to be declared, before they can be used
At the time I made this as a kind of model. See: viewtopic.php?p=263147&hilit=model#p263147
the rules were less strict and perhaps I was not accurate enough. But we know it and answered @edje1205 accordingly.

This model contains 3 functions, called "comfortlevel", "forecast" and "winddir".
None of the returned values were declared, so that was the issue.
As @Arnold88, also wrote, this may be a side effect of upgrading an application (in this case Node-RED) or even a single node.

But it is an extra warning.

Regards
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest