Help with function node

use {{data.entity_id}} instead of {{payload.data.entity_id}}. It does work, I am using it in my flows too.

Can you post a working example? I’ve tried that unsuccessfully.

So my entity_id is in msg.data.entity_id, but you put data.entity_id in the node.

Is the name of the your HA server in NR Home Assistant?

In the above function you would just pass in the entity id as the msg.payload

Through the combined efforts of everyone here, I’ve hacked together something that works, but not quite how I intended. With the inject node, I was able to leverage the topic for entity_id and the payload to turn on/off, but I need it to be triggered by time. Is there a way to inject a topic with big timer or am I back to using a function node? I tried to use a change node, but wasn’t able to get that working either.

No, it is “LRA HA”:

Then the top like of that function would be changed to

const entities = global.get('homeassistant').lrhHa.states;

I still get following error:


"TypeError: Cannot read property 'states' of undefined"

It looks like that ‘states’ is not defined in your msg yet. Usually you should define it at the beginning of your function node.

I had the same error message and was not able to define it within the function-node and used the normal change-node to define it and allocated ‘tbc’ in the change node

can you provide an example of the change node?

i’m currently traveling and have no remote access to my system, but will share when I’m back home (~1week)

Hi Lance, I’m trying to do exactly the same thing (use a subflow that dynamically passes the event_id to a “current state” node)

Did you manage to get this working? And if so could you share your code / flow?

Many thanks

What exactly are you trying to do?

I’m trying to create a reusable subflow for setting up motion sensor flows.

My motion sensors only output “on” when first triggered, then nothing until they are clear (off)

I want to pass a motion sensor input (event_state) to a 1 mins delay (delay) and then check the motion sensors state (current_state) for motion every min until there is no motion.

I’m trying to use a subflow so that when I attach the Motion Sensor event_state node it passes the event_id to the current_state node so that it checks the state of the right sensor. (and I dont have to set which motion sensor I want to check)

What I’m struggling with is how to pass the event_id to the current_state sensor, I cant work out how to do this with either a function or a change node

(any help is appreciated, I’m quite new to Node-Red!)

Here is my flow

[{"id":"b513065d.16ce58","type":"subflow","name":"Check motion every min","info":"","category":"","in":[{"x":160,"y":120,"wires":[{"id":"43b45618.9de8b8"}]}],"out":[{"x":660,"y":200,"wires":[{"id":"b8db6cee.f6ee","port":0}]},{"x":660,"y":260,"wires":[{"id":"b8db6cee.f6ee","port":1}]}],"env":[],"color":"#DDAA99"},{"id":"f09fd07a.d4238","type":"delay","z":"b513065d.16ce58","name":"","pauseType":"delay","timeout":"60","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":280,"wires":[["b8db6cee.f6ee"]]},{"id":"b8db6cee.f6ee","type":"api-current-state","z":"b513065d.16ce58","name":"current presence check","server":"7038aa85.7503b4","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"msg.payload.data.entity_id","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":390,"y":200,"wires":[["f09fd07a.d4238"],[]]},{"id":"43b45618.9de8b8","type":"change","z":"b513065d.16ce58","name":"","rules":[{"t":"set","p":"payload.entity_id","pt":"msg","to":"msg.payload.data.entity_id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":120,"wires":[["b8db6cee.f6ee"]]},{"id":"8e676b7c.cfa3a8","type":"server-state-changed","z":"45349610.ad3cf8","name":"Hallway motion","server":"7038aa85.7503b4","version":1,"entityidfilter":"binary_sensor.presence_hallway","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":220,"y":3840,"wires":[["e9266d0c.bc7dd"],[]]},{"id":"e9266d0c.bc7dd","type":"subflow:b513065d.16ce58","z":"45349610.ad3cf8","name":"","env":[],"x":470,"y":3840,"wires":[["a6dc77ad.f9a078"],[]]},{"id":"a6dc77ad.f9a078","type":"debug","z":"45349610.ad3cf8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":740,"y":3840,"wires":[]},{"id":"7038aa85.7503b4","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

Change your server in node red eg “Home Assistant or Home Assistant *** or anything” again, it works

Change your server in node red eg “Home Assistant or Home Assistant *** or anything” again, it works