Node-Red: writing values into wo using payload

I can more or less easily writing values into hass using the service “set_value {“value”:{{payload}}}”.
But I have several use case where it is not possible for me to use the payload, as the value I would like to write (set) isn’t in the flow. So I tried all kind of stuff that easy works in a script or automation, but nothing works. For instance:

{ “value”: $entities(‘sensor.energy_production_tomorrow’).state / 2}

At the I ended up using a script to write the values:

service: input_number.set_value
target:
  entity_id: input_number.multiplus_minimal_soc_setpoint
data:
  value: |
    {{ states('input_number.victron_soc_etimated_value')}}

As I couldn’t find any working example for this I assume the node is not made to do this and after reading some threads I often get the impression that it ends up using MQTT to do such stuff.
something as publish to MQTT and then creating a sensor in HA using mqtt. But this seems to be pretty complicated too.

I’m not clear on what it is you want to do. It would seem you want two input numbers to mirror each other? An event state node to a call service is all you need.

I see it needs to be divided by 2, in the output section on the bottom of the event state node use

$number($entity().state) / 2

Yep that is what I did.

I made an edit above, is the division, what the problem was?

Nope I want to write a value triggered by a event state. And the source value is not in the flow.

So you want to react to a state change, and then GET the state another entity in homeassistant to do whatever calculation you need to do?

I’m still a little unclear. To bring addinoal values into a flow you can use a current state. You can output the value to any message position, again at the bottom output section, and pass it like {{payload}}.

So msg.whatever can be passed as {{whatever}}

You want to trigger the flow by entity_1 state change in order to retrieve entity_b state for further processing?
If so, then use Trigger:State node to intercept entity_1 change, then redirect its result to Current State or Get Entities node to retrieve state of entity_2 node. Since that the flow will contain value of entity_2 you can do whatever you want with.

Do you mean something like this? I tried that, but having trouble with formatting correctly.

If such would work it would make a lot of my flows easier.

I haven’t figured out if that is possible yet. What I suggested is using a current state node, outputting to a different message position when the event is triggered.

The message it will give me

msg : Object
object
_msgid: "c26facef8283e949"
payload: 2
topic: ""
entity_2: "85.0"

Now I can use {{payload}} and {{entity_2}} in a call service node. The code in the event state output will divide that entity by 2.

[{"id":"b9d1897ed4f01d5d","type":"debug","z":"f80b6c338afd5483","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1030,"y":1220,"wires":[]},{"id":"efbba4107fd31ce4","type":"server-state-changed","z":"f80b6c338afd5483","name":"","server":"","version":3,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_number.number","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"$number($entity().state) / 2","valueType":"jsonata"}],"x":330,"y":1220,"wires":[["cf755d5fb0c33613"]]},{"id":"cf755d5fb0c33613","type":"api-current-state","z":"f80b6c338afd5483","name":"","server":"","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.grow_humidity","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"entity_2","propertyType":"msg","value":"","valueType":"entityState"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":730,"y":1220,"wires":[["b9d1897ed4f01d5d"]]}]
1 Like

Ok got it, you use a “current state” node to inject a new entity as payload. Clever such a use is not mentioned in the docs. And it works in my case.

1 Like

I too spent weeks getting to this point. Using Current State solves it all. Thanks so much!