Call to service node with various inputs

Hi I’m still new to HA and Node Red and I’m trying to do the following:
in night mode, I want to switch all heaters which are in auto mode to “eco” mode (which consists in switching in manual mode with a user defined temperature)

I have set an ‘input_number.input_eco’ with the desired temperature
then I have a “get entities” node for filtering the heaters going into a “service call” node with ‘set_temperature’ service to switch the heaters to the right mode and temperature

the issue is to get the desired temperature value so I added a “current state” node to get the value but now I’m not sure how to use the values from the “get entities” node and the “current state” node :expressionless:

here is the flow:

[{"id":"4d567fc2.c426d","type":"ha-get-entities","z":"9cceba3d.e22d68","server":"120937af.20abe8","name":"Liste radiateurs en auto","rules":[{"property":"entity_id","logic":"starts_with","value":"climate","valueType":"str"},{"property":"attributes.operation_mode","logic":"is","value":"auto","valueType":"str"}],"output_type":"split","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":302,"y":560,"wires":[["8290a6e9.3310f8","ef6aa122.352248"]]},{"id":"dd9101e8.0fc4b8","type":"api-call-service","z":"9cceba3d.e22d68","name":"passer en eco","server":"120937af.20abe8","service_domain":"climate","service":"set_temperature","data":"{\"temperature\": {{ flow.payload }}, \"operation_mode\": \"manual\"}","render_data":true,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":700,"y":555,"wires":[[]]},{"id":"5617cb8a.20efac","type":"trigger-state","z":"9cceba3d.e22d68","name":"Mode nuit","server":"120937af.20abe8","entityid":"input_select.home_mode","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"8htoxr8fjxc","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"nuit"},{"id":"thxu0dg1yz","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"nuit"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":94,"y":596,"wires":[["4d567fc2.c426d","d73453da.07e72"],[]]},{"id":"ef6aa122.352248","type":"api-current-state","z":"9cceba3d.e22d68","name":"temperature eco","server":"120937af.20abe8","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_number.input_eco","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":518,"y":614,"wires":[["dd9101e8.0fc4b8"]]},{"id":"8290a6e9.3310f8","type":"debug","z":"9cceba3d.e22d68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":556.5,"y":496,"wires":[]},{"id":"d73453da.07e72","type":"api-call-service","z":"9cceba3d.e22d68","name":"Fermer tous les volets","server":"120937af.20abe8","service_domain":"cover","service":"close_cover","data":"{\"entity_id\":\"group.all_covers\"}","render_data":false,"mergecontext":"","output_location":"payload","output_location_type":"msg","x":390.5,"y":728,"wires":[[]]},{"id":"120937af.20abe8","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]

thanks for your help!

While I’m new to Node-Red myself I think you’ll probably need a function to extract Entity ID and temp data from the message and reform it into payload.data expected by the call service node.

[edit]
My implementation was wrong, try this (replace your ‘temperature eco’ node with those 2)

[{"id":"50082b93.e6ca74","type":"api-current-state","z":"adb62510.cbb138","name":"temperature eco","server":"bce6befa.26a27","outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"input_number.input_eco","state_type":"str","state_location":"temperature","override_payload":"msg","entity_location":"","override_data":"none","x":980,"y":580,"wires":[["c07296cb.b62d98"]]},{"id":"c07296cb.b62d98","type":"function","z":"adb62510.cbb138","name":"Entity + Temp","func":"var newMsg = { payload: { data: { entity_id: msg.payload.entity_id, temperature: msg.temperature } } }\nreturn newMsg;","outputs":1,"noerr":0,"x":1180,"y":580,"wires":[[]]},{"id":"bce6befa.26a27","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open"}]

Move your current-state node before the get-entities node, so it only gets called once and set the State Location to msg.temperature.

Then in the call-service node reference temperature for the value

4 Likes

Thanks!
Easy when you know the answer :smiley:

Just an update for people reading this after version 0.8.0 has been released. This can now be done by removing the current-state node and just referencing the input_boolean state in the data field of the call-service node.

{
    "temperature": "{{states.input_number.input_eco}}",
    "operation_mode": "manual"
}

Nice!
Can states() and state_attr() be used in this template also? (I assume not but it would be nice :slight_smile: )

Had to actually look up what each function did in the templates couldn’t remember.

{{ states.device_tracker.paulus.state }}
{{ states('device_tracker.paulus') }}
{{ state_attr('device_tracker.paulus', 'battery') }}

equivalent would be

{{ states.device_tracker.paulus }} or {{ states.device_tracker.paulus.state }}
{{ states.device_tracker.paulus }} or {{ states.device_tracker.paulus.state }}
{{ states.device_tracker.paulus.attributes.battery }}
1 Like

Yea but my understating is states.device_tracker.paulus.state will error out if state does not exist (after reboot) states(‘device_tracker.paulus’) will return last cashed state in that case. (same with state_attr)

All the template data works off the last received value from home assistant. If it’s not a valid property it will currently fail silently and return an empty string, the default behavior for mustache templates. I could have it throw a warning if the value doesn’t exist.