Redundant Flow

Hi, I haven’t been using node-red for a long time, and so far I’ve used it for very simple automations, now I wanted to try something more complicated for me, the node in question works but in my opinion it’s very redundant, can someone help me reduce it? Thanks a lot!

[{"id":"0743b65fbc8f072f","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"42066f9855ca8d4e","type":"server-state-changed","z":"0743b65fbc8f072f","name":"Finestra Bagno","server":"706d0d82.9d7084","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.finestra_bagno_contact"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"15","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":120,"y":40,"wires":[["39ac253778e7b433"],[]]},{"id":"e2a8f6fc44699dbe","type":"server-state-changed","z":"0743b65fbc8f072f","name":"Finestra Camera","server":"706d0d82.9d7084","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.finestra_camera_centrale_contact"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"15","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":120,"y":100,"wires":[["d77e381438f2073d"],[]]},{"id":"c50db4831bd0c3b3","type":"server-state-changed","z":"0743b65fbc8f072f","name":"Finestra Sala SX","server":"706d0d82.9d7084","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.finestra_sala_sinistra_contact"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"15","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":120,"y":160,"wires":[["025e80c7391df4b8"],[]]},{"id":"b98aa4bb15da9321","type":"server-state-changed","z":"0743b65fbc8f072f","name":"Finestra Studio","server":"706d0d82.9d7084","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.finestra_studio_contact"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"15","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":120,"y":220,"wires":[["d826d23803f5033b"],[]]},{"id":"43d51dd661717fe0","type":"server-state-changed","z":"0743b65fbc8f072f","name":"Finestra Veranda","server":"706d0d82.9d7084","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.finestra_veranda_contact"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"15","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":120,"y":280,"wires":[[],[]]},{"id":"39ac253778e7b433","type":"time-range-switch","z":"0743b65fbc8f072f","name":"","lat":"43.54393","lon":"10.31612","startTime":"08:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":310,"y":40,"wires":[["9d49552db3a648b3"],[]]},{"id":"d77e381438f2073d","type":"time-range-switch","z":"0743b65fbc8f072f","name":"","lat":"43.54393","lon":"10.31612","startTime":"08:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":310,"y":100,"wires":[["7cbbae13b00e7089"],[]]},{"id":"025e80c7391df4b8","type":"time-range-switch","z":"0743b65fbc8f072f","name":"","lat":"43.54393","lon":"10.31612","startTime":"08:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":310,"y":160,"wires":[["a981d67d91cda538"],[]]},{"id":"d826d23803f5033b","type":"time-range-switch","z":"0743b65fbc8f072f","name":"","lat":"43.54393","lon":"10.31612","startTime":"08:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":310,"y":220,"wires":[["b5ee9677022fa9be"],[]]},{"id":"ae4becc63985ab0d","type":"time-range-switch","z":"0743b65fbc8f072f","name":"","lat":"43.54393","lon":"10.31612","startTime":"08:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":310,"y":280,"wires":[["dc47c87972218401"],[]]},{"id":"9d49552db3a648b3","type":"api-current-state","z":"0743b65fbc8f072f","name":"T + 15","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"15","halt_if_type":"num","halt_if_compare":"gte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":40,"wires":[["7a3570f136811be1"],[]]},{"id":"7cbbae13b00e7089","type":"api-current-state","z":"0743b65fbc8f072f","name":"T + 15","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"15","halt_if_type":"num","halt_if_compare":"gte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":100,"wires":[["ffa3c7cfbeace7d3"],[]]},{"id":"a981d67d91cda538","type":"api-current-state","z":"0743b65fbc8f072f","name":"T + 15","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"15","halt_if_type":"num","halt_if_compare":"gte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":160,"wires":[["2a921ba33fb081a1"],[]]},{"id":"b5ee9677022fa9be","type":"api-current-state","z":"0743b65fbc8f072f","name":"T + 15","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"15","halt_if_type":"num","halt_if_compare":"gte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":220,"wires":[["3039c0e9a5b3554f"],[]]},{"id":"dc47c87972218401","type":"api-current-state","z":"0743b65fbc8f072f","name":"T + 15","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"15","halt_if_type":"num","halt_if_compare":"gte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":470,"y":280,"wires":[["cb9b6d05d759a944"],[]]},{"id":"7a3570f136811be1","type":"api-current-state","z":"0743b65fbc8f072f","name":"T - 20","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"20","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":610,"y":40,"wires":[["8a6538d78f3eac82"],[]]},{"id":"ffa3c7cfbeace7d3","type":"api-current-state","z":"0743b65fbc8f072f","name":"T - 20","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"20","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":610,"y":100,"wires":[["ebcda3551fc4730e"],[]]},{"id":"2a921ba33fb081a1","type":"api-current-state","z":"0743b65fbc8f072f","name":"T - 20","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"20","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":610,"y":160,"wires":[["99e82d537623c306"],[]]},{"id":"3039c0e9a5b3554f","type":"api-current-state","z":"0743b65fbc8f072f","name":"T - 20","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"20","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":610,"y":220,"wires":[["534ac507852f81d9"],[]]},{"id":"cb9b6d05d759a944","type":"api-current-state","z":"0743b65fbc8f072f","name":"T - 20","server":"706d0d82.9d7084","version":3,"outputs":2,"halt_if":"20","halt_if_type":"num","halt_if_compare":"lte","entity_id":"sensor.livorno_realfeel_temperature_max_0d","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":610,"y":280,"wires":[["0752c6c9c2b8f4e0"],[]]},{"id":"8a6538d78f3eac82","type":"api-render-template","z":"0743b65fbc8f072f","name":"","server":"706d0d82.9d7084","version":0,"template":"Attenzione, la finestra bagno è aperta da oltre 15 minuti,  \n{{ \n[           \n\" forse è meglio chiuderla\",  \n\" ricordati di chiuderla\",  \n\" andrebbe chiusa\", \n]\n|random }} ","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":760,"y":40,"wires":[["a63006f03c256370"]]},{"id":"ebcda3551fc4730e","type":"api-render-template","z":"0743b65fbc8f072f","name":"","server":"706d0d82.9d7084","version":0,"template":"Attenzione, la finestra camera è aperta da oltre 15 minuti,  \n{{ \n[           \n\" forse è meglio chiuderla\",  \n\" ricordati di chiuderla\",  \n\" andrebbe chiusa\", \n]\n|random }} ","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":760,"y":100,"wires":[["a63006f03c256370"]]},{"id":"99e82d537623c306","type":"api-render-template","z":"0743b65fbc8f072f","name":"","server":"706d0d82.9d7084","version":0,"template":"Attenzione, la finestra sala sinistra è aperta da oltre 15 minuti,  \n{{ \n[           \n\" forse è meglio chiuderla\",  \n\" ricordati di chiuderla\",  \n\" andrebbe chiusa\", \n]\n|random }} ","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":760,"y":160,"wires":[["a63006f03c256370"]]},{"id":"534ac507852f81d9","type":"api-render-template","z":"0743b65fbc8f072f","name":"","server":"706d0d82.9d7084","version":0,"template":"Attenzione, la finestra studio è aperta da oltre 15 minuti,  \n{{ \n[           \n\" forse è meglio chiuderla\",  \n\" ricordati di chiuderla\",  \n\" andrebbe chiusa\", \n]|random }} ","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":760,"y":220,"wires":[["a63006f03c256370"]]},{"id":"0752c6c9c2b8f4e0","type":"api-render-template","z":"0743b65fbc8f072f","name":"","server":"706d0d82.9d7084","version":0,"template":"Attenzione, la finestra veranda è aperta da oltre 15 minuti,  \n{{ \n[           \n\" forse è meglio chiuderla\",  \n\" ricordati di chiuderla\",  \n\" andrebbe chiusa\", \n]\n|random }} ","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":760,"y":280,"wires":[["a63006f03c256370"]]},{"id":"a63006f03c256370","type":"alexa-remote-routine","z":"0743b65fbc8f072f","name":"","account":"6fcf9700.4b7f98","routineNode":{"type":"speakAtVolume","payload":{"type":"announcement","text":{"type":"msg","value":"payload"},"volume":{"type":"num","value":"80"},"mode":"set","devices":["G0G2HN033095093D"]}},"x":1050,"y":160,"wires":[[]]},{"id":"706d0d82.9d7084","type":"server","name":"Home Assistant Riccardo","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true},{"id":"6fcf9700.4b7f98","type":"alexa-remote-account","name":"echopage","authMethod":"proxy","proxyOwnIp":"192.168.1.204","proxyPort":"3457","cookieFile":"/config/alexa-nodered.cookie","refreshInterval":"1","alexaServiceHost":"alexa.amazon.it","amazonPage":"amazon.it","acceptLanguage":"it-IT","userAgent":"","useWsMqtt":"on","autoInit":"on"}]

You can add all the binaries in one event state.

image

In the outgoing message we will send the real feel temp in msg.temp to check in a switch node that the temp is between 15 and 20.

We will also send the friendly name of the sensor that triggered the node in msg.friendly_name. That will be used in the final message template.

[{"id":"43d51dd661717fe0","type":"server-state-changed","z":"0743b65fbc8f072f","name":"Finestra Veranda","server":"","version":6,"outputs":2,"exposeAsEntityConfig":"","entities":{"entity":["binary_sensor.finestra_veranda_contact","binary","binary2","binary3"],"substring":[],"regex":[]},"outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"15","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"temp","propertyType":"msg","value":"$entities('sensor.livorno_realfeel_temperature_max_0d').state","valueType":"jsonata"},{"property":"friendly_name","propertyType":"msg","value":"$entity().friendly_name","valueType":"jsonata"}],"x":140,"y":560,"wires":[["ae4becc63985ab0d"],[]]},{"id":"ae4becc63985ab0d","type":"time-range-switch","z":"0743b65fbc8f072f","name":"","lat":"","lon":"","startTime":"08:30","endTime":"22:00","startOffset":0,"endOffset":0,"x":340,"y":560,"wires":[["6c6b84cb0eaaa682"],[]]},{"id":"0752c6c9c2b8f4e0","type":"api-render-template","z":"0743b65fbc8f072f","name":"","server":"","version":0,"template":"Attenzione, la {{ friendly_name }} è aperta da oltre 15 minuti,  \n{{ \n[           \n\" forse è meglio chiuderla\",  \n\" ricordati di chiuderla\",  \n\" andrebbe chiusa\", \n]\n|random }} ","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"","templateLocationType":"none","x":660,"y":560,"wires":[["a63006f03c256370"]]},{"id":"a63006f03c256370","type":"alexa-remote-routine","z":"0743b65fbc8f072f","name":"","account":"6fcf9700.4b7f98","routineNode":{"type":"speakAtVolume","payload":{"type":"announcement","text":{"type":"msg","value":"payload"},"volume":{"type":"num","value":"80"},"mode":"set","devices":["G0G2HN033095093D"]}},"x":930,"y":560,"wires":[[]]},{"id":"6c6b84cb0eaaa682","type":"switch","z":"0743b65fbc8f072f","name":"","property":"temp","propertyType":"msg","rules":[{"t":"btwn","v":"15","vt":"num","v2":"20","v2t":"num"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":560,"wires":[["0752c6c9c2b8f4e0"]]}]

Ok I tried your flow and it works 80% in the sense that the final result is the message without the friendly_name. If I use a debug node connected to the first node it gives me this result. msg.friendly_name : undefined

Can you go to dev tools → states and find the entities. See if they have the friendly name attribute.

Yes they have it
image

Sorry that was my mistake, I gave you the wrong path, Change friendly name to $entity().attributes.friendly_name

Still nothing, same result. I put a debug node with “msg.payload = event data” output and this is the result:

That seems to work for me. You can try $entity().new_state.attributes.friendly_name

If that does not work you can change, in the template node, {{friendly_name}} to {{data.new_state.attributes.friendly_name}}

Edit to use the last option set msg.data to event data