Function? Help

I’m trying to determine how to pass on/off from a comparison of an input_number to an entity like probability of precipitation or humidity and am not succeeding. I think I have to pass one value to a variable of a function and then use the other value as the message entering the function. Does anyone have something like this working they could share. Greatly appreciated.

I’ve done more reading and arrived at this:

var precip = context.get("precip");
var slider = context.get("slider");

if (msg.payload.hasOwnProperty("sensor.dark_sky_precip_probability")) {
  precip = msg.payload.sensor.dark_sky_precip_probability.state;
  context.set("precip", precip);
}

if (msg.payload.hasOwnProperty("input_number.slider1")) {
  slider = msg.payload.input_number.slider1.state;
  context.set("slider", slider);
}

if (precip != undefined && slider != undefined) {
  if(precip >= slider){
    msg.payload = "ON";
  } else {
    msg.payload = "OFF";
    return msg;
  }
}

It doesn’t work. Does anyone see the problem?

Not sure I exactly follow what you’re trying to do but this might help.

// Global object that contains HA states
const states = global.get('homeassistant').homeAssistant.states;

const precip = states["sensor.dark_sky_precip_probability"].state;
const slider = states["input_number.slider1"].state;

msg.payload = precip >= slider ? "ON" : "OFF";

return msg;

@Kermit . Thanks, apparently I was way off. slider1 is a setpoint that I want to compare to chance of precip to then enable of disable zones of sprinkler. Slider is a number from 0 to 100 and chance of precip is a number from 0 to 100. The suggestion above always results with “ON”.

I’m getting the states from events filtered by entity_id out of the “Events: All” node of node-red-contrib-home-assistant-websocket. I have one path to the input of the function with only state change events of precip and another with state change events of slider. When either change I’d like the function output to update on/status based on the comparison of precip and slider. Is the 'homeassistant" line needed? Do the const precip and slider lines work with the state being in “payload.event.new_state.state” of the incoming messages? Thanks again for looking at this.

These do the same thing just in different ways:

Example #1

image

[{"id":"e17cf1a4.66199","type":"server-state-changed","z":"b682664f.0ce8b8","name":"Slider or Precip","version":1,"entityidfilter":"sensor.dark_sky_precip_probability,sensor.dark_sky_precip_probability","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":160,"y":768,"wires":[["a86fdf60.45b3c"]]},{"id":"a86fdf60.45b3c","type":"function","z":"b682664f.0ce8b8","name":"","func":"// Global object that contains HA states\nconst states = global.get('homeassistant').homeAssistant.states;\n\nconst precip = states[\"sensor.dark_sky_precip_probability\"].state;\nconst slider = states[\"input_number.slider1\"].state;\n\nmsg.payload = slider <= precip ? \"ON\" : \"OFF\";\n\nreturn msg;","outputs":1,"noerr":0,"x":322,"y":768,"wires":[["fb5f7d4d.fec4d"]]},{"id":"fb5f7d4d.fec4d","type":"debug","z":"b682664f.0ce8b8","name":"Do stuff: set on/off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":506,"y":768,"wires":[]}]

Example #2

[{"id":"5dfabe5.90ba44","type":"server-state-changed","z":"b682664f.0ce8b8","name":"Slider or Precip","version":1,"entityidfilter":"sensor.dark_sky_precip_probability,sensor.dark_sky_precip_probability","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":176,"y":864,"wires":[["908ad37b.7d6a4"]]},{"id":"5bfaea93.058aa4","type":"debug","z":"b682664f.0ce8b8","name":"Do stuff: set on/off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":954,"y":912,"wires":[]},{"id":"908ad37b.7d6a4","type":"api-current-state","z":"b682664f.0ce8b8","name":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.dark_sky_precip_probability","state_type":"str","state_location":"precip","override_payload":"msg","entity_location":"","override_data":"none","blockInputOverrides":false,"x":478,"y":864,"wires":[["d5cbaced.56755"]]},{"id":"d5cbaced.56755","type":"api-current-state","z":"b682664f.0ce8b8","name":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_number.slider1","state_type":"str","state_location":"slider1","override_payload":"msg","entity_location":"","override_data":"none","blockInputOverrides":false,"x":428,"y":912,"wires":[["7930a7f.c18fa58"]]},{"id":"7930a7f.c18fa58","type":"switch","z":"b682664f.0ce8b8","name":"","property":"slider1","propertyType":"msg","rules":[{"t":"lte","v":"precip","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":642,"y":912,"wires":[["5c374cb2.5183d4"],["8247fe29.32d17"]]},{"id":"5c374cb2.5183d4","type":"change","z":"b682664f.0ce8b8","name":"ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":912,"wires":[["5bfaea93.058aa4"]]},{"id":"8247fe29.32d17","type":"change","z":"b682664f.0ce8b8","name":"OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":960,"wires":[["5bfaea93.058aa4"]]}]

Thanks Kermit, I looked at using a switch and other base nodes before venturing into the function node. I got the second example working with some changes.

[{"id":"7139c350.9bf26c","type":"server-state-changed","z":"a76a74e8.f49168","name":"Precip","server":"b7207b43.b86948","version":1,"entityidfilter":"sensor.dark_sky_precip_probability","entityidfiltertype":"substring","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":370,"y":300,"wires":[["7f4621d.ade82e"]]},{"id":"78974bc8.b973a4","type":"debug","z":"a76a74e8.f49168","name":"Do stuff: set on/off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1290,"y":340,"wires":[]},{"id":"7f4621d.ade82e","type":"api-current-state","z":"a76a74e8.f49168","name":"","server":"b7207b43.b86948","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.dark_sky_precip_probability","state_type":"num","state_location":"precip","override_payload":"msg","entity_location":"","override_data":"msg","blockInputOverrides":false,"x":702,"y":300,"wires":[["6845e82e.e5b6a8"]]},{"id":"6845e82e.e5b6a8","type":"api-current-state","z":"a76a74e8.f49168","name":"","server":"b7207b43.b86948","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_number.slider1","state_type":"num","state_location":"slider1","override_payload":"msg","entity_location":"","override_data":"none","blockInputOverrides":false,"x":652,"y":348,"wires":[["10106d00.3391a3"]]},{"id":"10106d00.3391a3","type":"switch","z":"a76a74e8.f49168","name":"","property":"precip","propertyType":"msg","rules":[{"t":"lte","v":"slider1","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":866,"y":348,"wires":[["ae4eb45c.c0ac18"],["9069e5d0.e85068"]]},{"id":"9069e5d0.e85068","type":"change","z":"a76a74e8.f49168","name":"ON","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":400,"wires":[["78974bc8.b973a4"]]},{"id":"ae4eb45c.c0ac18","type":"change","z":"a76a74e8.f49168","name":"OFF","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":340,"wires":[["78974bc8.b973a4"]]},{"id":"340e3f41.08849","type":"server-state-changed","z":"a76a74e8.f49168","name":"Slider","server":"b7207b43.b86948","version":1,"entityidfilter":"input_number.slider1","entityidfiltertype":"substring","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":370,"y":360,"wires":[["7f4621d.ade82e"]]},{"id":"b7207b43.b86948","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]