Type Error Cannot read undefined

Been working on this for a bit, started out ensuring I changed the values from my ac/dryer outputs to values that did not share similar names.

I am now having an issue with my function which compares the values to engage an on off function.
I have a function just like this one below to adapt my dryer values, and its working properly for both of these.
image

Then we come to my compare function below. I continue to get TypeError: Cannot read property 'dry_e/ac_e' of undefined.
image

Any insight on this would be greatly appreciate, just learning java/node red and gotten as far as I could, appreciate your help!

I guess these functions that make the AC and dry messages are in different functions?

You might need a join node it you might just need to combine the functions but it is difficult to say as you have not shared enough info.

If you make s small flow (using injects to simulate the AC/dry messages) that demonstrates the issue and post it in a reply, someone will help out.


Here is the current node setup

dryer function:
image
Dryer Output from function:
image

ac function:
image
ac output from function:
image

Compare Function:
image
Compare function Output:
TypeError: Cannot read property 'ac_e' of undefined
TypeError: Cannot read property 'dry_e' of undefined

Hopefully that is clearer, if i forgot any details i apologize

Not really. Can you capture a message coming out of the blue nodes on the left (use a debug node and the copy button that appears under your mouse pointer when you hover over the debug message) paste them into inject nodes and export a working example flow. I can't make your flow and demonstrate how to improve it from pictures.

Ps. I just want to say, I can see the problem, you definitely need a join node (set to key/value mode) if you want to try figure this out yourself.

See this article in the cookbook for an example of how to join messages into one object.

As both entities are at msg.data.new_state.state and your logic is or as in either of these will turn on /off. No need to join the messages.
This could just be as simple as the example below.

strike that I just saw the and in the lower statement.

Here is an updated version using a join. It uses the msg.payload.data.entity_id to join the messages. In the example they are sensor.t81_ac_b_watts and sensor.t81_drier_b_watts, they may need editing to your entity_id's in the function node.

[{"id":"e2b74283.5c6e2","type":"inject","z":"9b3f9f31.c45298","name":"Dryer 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"old_state\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"state\":null,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 Drier B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:27:46.134297+00:00\",\"last_updated\":\"2021-05-06T03:27:46.134297+00:00\",\"context\":{\"id\":\"c7b7478527321dd3d9a5fda8fa6f42a4\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unavailable\"},\"new_state\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"state\":1,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 Drier B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:27:47.104005+00:00\",\"last_updated\":\"2021-05-06T03:27:47.104005+00:00\",\"context\":{\"id\":\"2cb5aabcdc80736d22d35bbc7d8c4e2c\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"1\",\"timeSinceChangedMs\":9}},\"_msgid\":\"cc606cbb.5e02f\"}","payloadType":"json","x":100,"y":2200,"wires":[["ad8b58bf.9c7da"]]},{"id":"ad8b58bf.9c7da","type":"join","z":"9b3f9f31.c45298","name":"","mode":"custom","build":"object","property":"payload.data","propertyType":"msg","key":"payload.data.entity_id","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":350,"y":2220,"wires":[["20c3429c.6a62d6"]]},{"id":"20c3429c.6a62d6","type":"function","z":"9b3f9f31.c45298","name":"","func":"if(msg.payload.data[\"sensor.t81_ac_b_watts\"].new_state.state >= 1000 || msg.payload.data[\"sensor.t81_drier_b_watts\"].new_state.state >= 1000) {\n    return {topic: \"solar_engaged\", payload: 1000};\n}else if(msg.payload.data[\"sensor.t81_ac_b_watts\"].new_state.state < 1000 && msg.payload.data[\"sensor.t81_drier_b_watts\"].new_state.state < 1000){\n   return {topic: \"solar_disengaged\", payload: 0}; \n}else {\n    return {topic: \"solar_disengaged\", payload: 0};\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":2220,"wires":[["6a027541.bca3a4"]]},{"id":"afa9e146.e614c8","type":"inject","z":"9b3f9f31.c45298","name":"Dryer 1001","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"old_state\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"state\":null,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 Drier B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:27:46.134297+00:00\",\"last_updated\":\"2021-05-06T03:27:46.134297+00:00\",\"context\":{\"id\":\"c7b7478527321dd3d9a5fda8fa6f42a4\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unavailable\"},\"new_state\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"state\":1001,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 Drier B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:27:47.104005+00:00\",\"last_updated\":\"2021-05-06T03:27:47.104005+00:00\",\"context\":{\"id\":\"2cb5aabcdc80736d22d35bbc7d8c4e2c\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"1\",\"timeSinceChangedMs\":9}},\"_msgid\":\"cc606cbb.5e02f\"}","payloadType":"json","x":110,"y":2240,"wires":[["ad8b58bf.9c7da"]]},{"id":"28bb699a.8b6166","type":"inject","z":"9b3f9f31.c45298","name":"AC 1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"old_state\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"state\":0,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 AC B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:35:59.022534+00:00\",\"last_updated\":\"2021-05-06T03:35:59.022534+00:00\",\"context\":{\"id\":\"efd26752a3ee6018693141c9f16641fd\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"0\"},\"new_state\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"state\":null,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 AC B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:38:28.497425+00:00\",\"last_updated\":\"2021-05-06T03:38:28.497425+00:00\",\"context\":{\"id\":\"9a8ec78c5914768f665d63c82dc63b34\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unavailable\",\"timeSinceChangedMs\":89}},\"_msgid\":\"d49ce331.88412\"}","payloadType":"json","x":110,"y":2280,"wires":[["ad8b58bf.9c7da"]]},{"id":"699aeb2f.caa14c","type":"inject","z":"9b3f9f31.c45298","name":"AC 1001","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"data\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"old_state\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"state\":0,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 AC B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:35:59.022534+00:00\",\"last_updated\":\"2021-05-06T03:35:59.022534+00:00\",\"context\":{\"id\":\"efd26752a3ee6018693141c9f16641fd\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"0\"},\"new_state\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"state\":1001,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 AC B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:38:28.497425+00:00\",\"last_updated\":\"2021-05-06T03:38:28.497425+00:00\",\"context\":{\"id\":\"9a8ec78c5914768f665d63c82dc63b34\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unavailable\",\"timeSinceChangedMs\":89}},\"_msgid\":\"d49ce331.88412\"}","payloadType":"json","x":100,"y":2320,"wires":[["ad8b58bf.9c7da"]]},{"id":"6a027541.bca3a4","type":"switch","z":"9b3f9f31.c45298","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"1000","vt":"str"},{"t":"lt","v":"1000","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":660,"y":2220,"wires":[["97a432a4.3efc48"],["97a432a4.3efc48"]]},{"id":"97a432a4.3efc48","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":2160,"wires":[]}]

Sorry for the late reply here is my flow:

[{"id":"22a897f.d306de8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"931bdc3f.506f1","type":"switch","z":"22a897f.d306de8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"1000","vt":"str"},{"t":"lt","v":"1000","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":770,"y":180,"wires":[["a3d9be8e.a85668"],["7025ccb4.170f84"]]},{"id":"917b8a1a.1b15b","type":"function","z":"22a897f.d306de8","name":"","func":"ac = msg.data.entity_id\nenergy = Number(msg.data.new_state.state)\n\nvar topic = {topic: \"Usage\", payload: energy}\n\nreturn {topic};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":220,"wires":[["80a82a60.ffe31"]]},{"id":"1dcd157e.c1f933","type":"function","z":"22a897f.d306de8","name":"","func":"dryer = msg.data.entity_id\nenergy = msg.data.new_state.state\n\nvar topic = {topic: \"Usage\", payload: energy}\n\nreturn {topic};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":140,"wires":[["80a82a60.ffe31"]]},{"id":"80a82a60.ffe31","type":"function","z":"22a897f.d306de8","name":"","func":"ac = \"AC\"\ndry = \"Dryer\"\nenergy = msg.topic.payload\n\nvar on = {topic: \"Solar Engaged\", msg: 1000}\nvar off = {topic:\"Solar Disengaged\", msg: 0}\n\n\nif (energy >= 1000)\n{\n    return (on)\n    Break;\n}\nelse if (energy < 1000)\n{\n    return (off)\n    Break;\n}\nelse\n{\n    return (off);\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":180,"wires":[["931bdc3f.506f1"]]},{"id":"a3d9be8e.a85668","type":"api-call-service","z":"22a897f.d306de8","name":"Phase B Watts High","server":"42c46992.e7d5f8","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.circut1_output","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1040,"y":140,"wires":[[]]},{"id":"7025ccb4.170f84","type":"api-call-service","z":"22a897f.d306de8","name":"Phase B Watts Low ","server":"42c46992.e7d5f8","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.circut1_output","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1030,"y":220,"wires":[[]]},{"id":"8404dc0b.c02a68","type":"inject","z":"22a897f.d306de8","name":"Dryer","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"topic\":\"sensor.t81_drier_b_watts\",\"payload\":1,\"data\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"old_state\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"state\":null,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 Drier B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:27:46.134297+00:00\",\"last_updated\":\"2021-05-06T03:27:46.134297+00:00\",\"context\":{\"id\":\"c7b7478527321dd3d9a5fda8fa6f42a4\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unavailable\"},\"new_state\":{\"entity_id\":\"sensor.t81_drier_b_watts\",\"state\":1,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 Drier B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:27:47.104005+00:00\",\"last_updated\":\"2021-05-06T03:27:47.104005+00:00\",\"context\":{\"id\":\"2cb5aabcdc80736d22d35bbc7d8c4e2c\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"1\",\"timeSinceChangedMs\":9}},\"_msgid\":\"cc606cbb.5e02f\"}","payloadType":"json","x":210,"y":140,"wires":[["1dcd157e.c1f933","400337b2.e68aa8"]]},{"id":"f2b858ca.f76b38","type":"inject","z":"22a897f.d306de8","name":"AC","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"topic\":\"sensor.t81_ac_b_watts\",\"payload\":null,\"data\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"old_state\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"state\":0,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 AC B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:35:59.022534+00:00\",\"last_updated\":\"2021-05-06T03:35:59.022534+00:00\",\"context\":{\"id\":\"efd26752a3ee6018693141c9f16641fd\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"0\"},\"new_state\":{\"entity_id\":\"sensor.t81_ac_b_watts\",\"state\":null,\"attributes\":{\"unit_of_measurement\":\"W\",\"friendly_name\":\"T81 AC B Watts\",\"icon\":\"mdi:flash-circle\"},\"last_changed\":\"2021-05-06T03:38:28.497425+00:00\",\"last_updated\":\"2021-05-06T03:38:28.497425+00:00\",\"context\":{\"id\":\"9a8ec78c5914768f665d63c82dc63b34\",\"parent_id\":null,\"user_id\":null},\"original_state\":\"unavailable\",\"timeSinceChangedMs\":89}},\"_msgid\":\"d49ce331.88412\"}","payloadType":"json","x":210,"y":220,"wires":[["917b8a1a.1b15b"]]},{"id":"400337b2.e68aa8","type":"debug","z":"22a897f.d306de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":430,"y":280,"wires":[]},{"id":"42c46992.e7d5f8","type":"server","name":"Home Assistant","addon":true}]

Hi, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

Did i do that right?

In your function node you have:
dryer = msg.data.entity_id
if you run the flow and open up the debug output do you see msg.data.entity_id? (hint hint)

Was up pretty late but here is the working code in case anyone has to do a similar adapation:
[{"id":"b9b8d064.f12b2","type":"tab","label":"Automatic AC/Dryer","disabled":false,"info":""},{"id":"ed050281.a38a3","type":"server-state-changed","z":"b9b8d064.f12b2","name":"AC Phase B Watts","server":"42c46992.e7d5f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.t81_ac_b_watts","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"gte","outputs":1,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":130,"y":160,"wires":[["e7851e47.435bf"]]},{"id":"4c2b39b7.1cddb8","type":"debug","z":"b9b8d064.f12b2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":240,"wires":[]},{"id":"133add48.f1a6c3","type":"server-state-changed","z":"b9b8d064.f12b2","name":"Dryer Phase B Watts","server":"42c46992.e7d5f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.t81_drier_b_watts","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"gte","outputs":1,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":130,"y":80,"wires":[["ce0df4b7.d6428"]]},{"id":"c9a28208.a1c6","type":"switch","z":"b9b8d064.f12b2","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"1000","vt":"str"},{"t":"lt","v":"1000","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":670,"y":120,"wires":[["358d31c4.0a4e2e"],["6da5a730.585a58"]]},{"id":"358d31c4.0a4e2e","type":"api-call-service","z":"b9b8d064.f12b2","name":"Phase B Watts High","server":"42c46992.e7d5f8","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.circut1_output","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":880,"y":80,"wires":[[]]},{"id":"6da5a730.585a58","type":"api-call-service","z":"b9b8d064.f12b2","name":"Phase B Watts Low ","server":"42c46992.e7d5f8","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.circut1_output","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":870,"y":160,"wires":[[]]},{"id":"e7851e47.435bf","type":"function","z":"b9b8d064.f12b2","name":"","func":"energy = msg.payload\n\nreturn {topic: \"AC\", a_e: energy};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":160,"wires":[["8604dc2e.bffcd"]]},{"id":"ce0df4b7.d6428","type":"function","z":"b9b8d064.f12b2","name":"","func":"energy = msg.payload\n\nreturn {topic: \"Dryer\", d_e: energy};","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":80,"wires":[["8604dc2e.bffcd"]]},{"id":"8604dc2e.bffcd","type":"function","z":"b9b8d064.f12b2","name":"","func":"ac = \"AC\"\ndry = \"Dryer\"\n\nA_E = msg.a_e\nD_E = msg.d_e\n\nif (ac == msg.topic)\n{\n if (A_E >= 1000)\n {\n return {topic: \"Solar Engaged\", payload: 1000};\n }\n\nelse if (dry == msg.topic)\n{\n if (D_E >= 1000)\n {\n return {topic: \"Solar Engaged\", payload: 1000};\n }\n}\n}\nelse if (D_E <1000 && A_E <1000)\n{\n return {topic:\"Solar Disengaged\", payload: 0};\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":120,"wires":[["c9a28208.a1c6","4c2b39b7.1cddb8"]]},{"id":"42c46992.e7d5f8","type":"server","name":"Home Assistant","addon":true}]

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