Variable Blind Positioning with function node

Hoping this is a simple fix, but I can’t figure out how to apply a variable blind positioning using a function node. I tried using both the below in the data field:

"position": msg.position
"position": {{position}}

But the both give off errors.

If I hard code it like this, it works fine:

"position": 50

Any advice would be really appreciated!

[{"id":"ded221b4724b5100","type":"ha-get-entities","z":"359116ad3423020e","name":"Fetch Blinds","server":"df9a019b.0518b","version":0,"rules":[{"property":"entity_id","logic":"starts_with","value":"cover.master_bedroom_blinds","valueType":"str"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":470,"y":640,"wires":[["a65eccb968e23d0c"]]},{"id":"a65eccb968e23d0c","type":"function","z":"359116ad3423020e","name":"Blinds Switch","func":"if (msg.cron == \"Daily_Sunset\") {\n    msg.state = \"close_cover\"\n    return msg\n}\nelse if (msg.cron == \"Weekend_Awake\") {\n    msg.state = \"set_cover_position\"\n    msg.position = 50\n    return msg\n}\n\nelse if (msg.cron == \"Weekday_Awake\") {\n    msg.state = \"set_cover_position\"\n    msg.position = 100\n    return msg\n\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":640,"wires":[["ea1e05148d106d73"]]},{"id":"ea1e05148d106d73","type":"api-call-service","z":"359116ad3423020e","name":"Toggle Blinds","server":"df9a019b.0518b","version":5,"debugenabled":false,"domain":"cover","service":"{{state}}","areaId":[],"deviceId":[],"entityId":[],"data":"{\t   \"entity_id\": payload.entity_id,\t   \"position\": msg.position \t}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":870,"y":640,"wires":[[]]},{"id":"8135558a0283cc25","type":"inject","z":"359116ad3423020e","name":"Test Injection","props":[{"p":"cron","v":"Weekday_Awake","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":280,"y":640,"wires":[["ded221b4724b5100"]]},{"id":"df9a019b.0518b","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Figured it out (easy fix) - I was missing “msg.” within the entity id.

image

I had a good look at this yesterday but could not see what the issue was. Now your last post has the answer staring me in the face. I think that it is down to the use of the ‘msg.’ that is actually causing the issue.

You are using JSONata in the service call node. Within Node-RED the JSONata implementation uses the message as the top level document, and thus you should ideally use

{
    "entity_id": payload.entity_id,
    "position": position
}

In this, the JSONata expression evaluates the payload.entity_id and the position correctly and without the need for the ‘msg.’ bit at the front.

In the past, ‘msg.’ was required, and to help there is a ‘compatibility’ mode in the edit box. If, at any point within the edit box, you enter ‘msg.’ then the mode is turned on - see the “Compatibility mode enabled” at the top left.

I guess that, with the compatibility mode turned on, then all references within the message now require 'msg. at the front. The issue was not that you did or did not use msg.position, but that you used one field reference with and one without.

A case of mixed messages.

1 Like

That’s exactly right - I knew it had to be something simple… and it was :slight_smile:

Thanks for the more detailed response!

If the error message within NR could have called this out it would have made my life a little easier, haha!