Value from HA entity to custom Node-red node's setting (power saver)

I’m a beginner, have tried google and searched forums with no luck.

I have created a helper in HA (input_number.maxprice). I want to use the number to set the value of on of the settings in a node (node is from node-red-contrib-power-saver). I found some code I tried but the filed seems only to accept number and I can’t change the field type to accept the code…

I have not used this particular node set, however the extensive documentation does provide a clue.

Part I

The docs suggest that dynamic configuration can be applied to the ‘lowest price’ node. This allows for the node UI configuration settings to be modified using input messages.

https://powersaver.no/nodes/dynamic-config.html

Like some other standard nodes, the developer of this node helpfully made it is possible to change the configuration settings by sending an input message constructed to issue a command.

It looks as if a message with payload as

{"config": {"maxPrice":numbervalue }}

would do the trick, where ‘numbervalue’ is the max price as a number.

To test this configuration message you can easily use a standard change node to test the configuration using a fixed number. If you try an inject node, then change node, with ‘set’, using ‘{}’ (JSON) for ‘msg.payload’ to

{"config":{"maxPrice":31}}

and link this into your saver node, then it should change the setting. I can only assume that editing the node will show in the UI the new (added/changed) figure for the max price…

I am not going to import and test this node myself, but I would suggest that if you get this to work then it is possible to do what you want.

Part II

Now we need to get the number out of the input_number.maxprice (which is an entity in HA) and use that rather than a fixed value.

To do this you will have to use the Home Assistant websocket node set, and in particular the current state node.

I am going to assume that you do have the HA websocket node set installed and working, thus it is a simple case of setting up the current state node, using the entity ‘input_number.maxprice’.

If you set the output proptery to ‘msg.payload’ (you can delete the second one as we only need to set msg.payload) and use the J: (JSONata) option with

{"config": {"maxPrice": $entity().state}}

that should do the trick. I am being economical by doing the work to build the configuration message within the one node using JSONata. The $entity().state bit gets the state value of the entity just called. This saves on the use of a change node to do the work.

You will need to trigger the current state node as you require, or alternatively use an events:state node set up in the same way. An events:state node would trigger every time you change the input_number value, and this would update the lowest price setting.

Again, this is not tested, but should work if the documentation is correct.

2 Likes

Thanks! Worked perfect!