NodeRed API error

Hi

have something changed in HA or Nodered so this would be the reason i get an API error It has been running for over 1 year without problems

i get
Call-service error. expected int for dictionary value @ data ‘brightness’
i cant figure out what is wrong :frowning:

[{"id":"8b1d8b480d936f78","type":"server-state-changed","z":"a9420e968b280477","name":"Kitchen motion 01","server":"20501dd8.c17de2","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.kit_motion_01_occupancy","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"off","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"30","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":130,"y":100,"wires":[["d603f44b01deabc8"],["d603f44b01deabc8","c3a479db066983a2"]]},{"id":"f8e977b3ee82d808","type":"api-call-service","z":"a9420e968b280477","name":"LEDS-OFF","server":"20501dd8.c17de2","version":5,"debugenabled":false,"domain":"light","service":"turn_off","areaId":[],"deviceId":[],"entityId":["light.kit_bel_count_01","light.kit_bel_count_02"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":590,"y":180,"wires":[[]]},{"id":"20e38a41b5bcee37","type":"api-call-service","z":"a9420e968b280477","name":"LEDS-ON","server":"20501dd8.c17de2","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kit_bel_count_01","light.kit_bel_count_02"],"data":"{\"brightness\":msg.data.attributes.brightness}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":580,"y":240,"wires":[[]]},{"id":"d603f44b01deabc8","type":"trigger","z":"a9420e968b280477","name":"","op1":"dim","op2":"off","op1type":"str","op2type":"str","duration":"60","extend":false,"overrideDelay":false,"units":"s","reset":"on","bytopic":"all","topic":"topic","outputs":2,"x":390,"y":100,"wires":[["9dae6ba4a1043a10","9ce30cfc2e0e61c3"],["f8e977b3ee82d808"]]},{"id":"c5a5a8fd38724fe7","type":"api-call-service","z":"a9420e968b280477","name":"LED-1-ON","server":"20501dd8.c17de2","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kit_bel_count_01"],"data":"{\"brightness\":msg.data.attributes.brightness/2}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":850,"y":120,"wires":[[]]},{"id":"9dae6ba4a1043a10","type":"api-current-state","z":"a9420e968b280477","name":"Current brightness KCL1","server":"20501dd8.c17de2","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.kit_bel_count_01","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"lightBrightness","propertyType":"flow","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":630,"y":120,"wires":[["c5a5a8fd38724fe7"]]},{"id":"9ce30cfc2e0e61c3","type":"api-current-state","z":"a9420e968b280477","name":"Current brightness KCL2","server":"20501dd8.c17de2","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.kit_bel_count_02","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"lightBrightness","propertyType":"flow","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":630,"y":60,"wires":[["442b98c38477b67d"]]},{"id":"442b98c38477b67d","type":"api-call-service","z":"a9420e968b280477","name":"LED-2-ON","server":"20501dd8.c17de2","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":[],"entityId":["light.kit_bel_count_02"],"data":"{\"brightness\":msg.data.attributes.brightness/2}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":850,"y":60,"wires":[[]]},{"id":"c3a479db066983a2","type":"change","z":"a9420e968b280477","name":"","rules":[{"t":"set","p":"data","pt":"msg","to":"lightBrightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":180,"wires":[["20e38a41b5bcee37"]]},{"id":"2b1dc6109bf609eb","type":"comment","z":"a9420e968b280477","name":"Köket","info":"","x":90,"y":60,"wires":[]},{"id":"20501dd8.c17de2","type":"server","name":"Home Assistant DEC 23","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]

You appear to call the service using

{"brightness":msg.data.attributes.brightness/2}

which comes either from the current brightness setting, or from a saved value in a flow context variable.

Assuming that the flow context variable is being set, and that the read brightness values are always available, I can only see two things.

First point, the use of ‘msg.’ at the front is no longer required, and this automatically sets the ‘compatibility mode’ (see top left in JSONata editor). As a general point, you should be using just data.attributes.brightness

Second point. Assume the brightness is 33. Then 33/2 would be 16.5, which JSONata returns as a float and not an integer. I presume that the service call has been allowing float values and now no longer does.

A quick solution would be something like

{"brightness": $floor(data.attributes.brightness/2)}

tried but get error

"Call-service error. expected int for dictionary value @ data['brightness']"

"The left side of the "/" operator must evaluate to a number"

i start to think it is the node leds-on maybe

should i remove this from the data field

{"brightness":msg.data.attributes.brightness}

Errors with using the call service node are common and arise from many causes.

The call-service error message suggests that the API is expecting an integer for ‘brightness’ and is not getting it. Which is what we already know.

The error message ‘the left side of the / operator must evaluate to a number’ is more helpful, coming from JSONata, and that suggests that

data.attributes.brightness

is probably undefined or a string or something else.

It would help to identify which node is complaining - the turn on node is relying on getting the brightness value from context, and if the sequence of events is such that this value is not set, then you will certainly get an error.

It would help to add a debug node (to show the full message, not just msg.payload) looking at the input to the call service side, then we could see just what is in the data field.

It would help to look in the context debug window in Node-RED to see what the value of your stored msg.data value is.

And yes, if you don’t really need the brightness when turning on, then just set this to a fixed number or leave it out.

Hope this helps.

Thank you for the answers

ill do some more TS and see if i can get it working. the dim before the turnoff was just a helpfull feature so i know to move in the room before the lighst turn off but i can skip it if i cant get it to work :slight_smile:

A long think - and it is likely that your sensors are returning strings (but your integration may have coped with “55” in the past and now wants integers as 27). In that case we need to turn the string into a number first, then divide by two, then turn into an integer. The following might work…

{"brightness" : $floor($number(data.attributes.brightness)/2)}

In JSONata, the $number(string) function will turn a string into a number, but it does not like anything except pure numbers, so fails with an error for unknowns, nulls, unavailable etc.

So, here is a bit of JSONata. Partially tested. You should be able to put this entire block into the JSONata editor. I have commented it so it should be easy to work out what is going on, and change to suit what you need.
Any non-number string will end up as ‘0’

(
    /* pick up the value you are looking for */
    $x:=data.attributes.brightness;

    /* check to see if it exists, and if so, what type it is */
    $y:=$exists($x) ? $type($x) : "undefined";

    /* if it is a string, and it regex matches a valid number     */
    /* apply $number() to it, if number leave, otherwise set to 0 */
    $r:= $y="string" and $contains($x, /^[+-]?(\d*\.)?\d+$/) ? $number($x) : $y="number" ? $x : 0;

    /* as you want integers, then divide by two and floor this in case it is a float */
    $value:=$floor($r/2);

    /* output the data object */
    {"brightness": $value}

)

And yes, I do wonder some times if it is all worth the effort. :grin:

Wauv.
best support i ever gotten here !

ill give it a try and reply back when i have tested it

\Thomas

nope :frowning:
it worked 1 time then i get api error again.

i give up:-(

thank you soooo much for your time
you got a give me a coffe account ?

Yes, debugging software can take a long time and a lot of effort.

I don’t have your integrations and set up so I can’t test it from here. You need to add a few debug nodes (set to full message) and spend time finding out what is happening in the message before it goes into the service call node.

At least it worked once - which suggests that one path in your flow is OK, the problem is with the other…

I do this for fun (yes, just for fun - I learn so much by trying to solve other people’s problems and trying to explain things).

Good luck with your projects!

thank you soo much
Appreciate your effort very much