Node Red JSONata function

Ok i’m new at node red and what looks simple seems to be harder than i expect.
I’m trying to manage the brightness of my lights, i’m sure this is the “hello world” of Node Red, but i also want to learn how it works.

so i can do this

and it sets my light to 5% brightness, makes sense the property “brightness_pct” is added to the msg and the brightness of the light is set!

but i don’t always want my light to be dim, so I’ve replaced my

{"brightness_pct": 5}

with a JSONata function

{
   "brightness_pct": $round(
       msg.attributes.brightness/2.55,
       0
   )+10
}

and that nicely returns a result

{
    "brightness_pct": 15
}

but now the brightness_pct property isn’t being added to the message and the brightness isn’t being set. This makes me sad, any one have any hints as to the probably very simple thing ive missed.

Thanks
Tim

ps the non working node looks like this.

Maybe it can’t be done inside a service call? If so then perhaps adding a function node upstream that formats the proper data can be used to feed the service call?

I don’t have it in front of me to test, but I’m fairly sure msg.attributes.brightness should be attributes.brightness. You skip the msg part within JSONata. It used to warn you about this, but maybe the latest version no longer does. So maybe you’re effectively getting msg.msg.attributes.brightness which doesn’t exist and so the property doesn’t get set.

What do you mean “isn’t being added to the message”? Check Show Debug Information in the call-service node and to show what the node is sending to HA.

Nothing looks wrong with your JSONata expression. @michaelblight is correct you don’t need to prefix it with msg but it will work either way.

@Kermit
The fixed JSON version produces

{"domain":"light","service":"turn_on","data":{"brightness_pct":5,"entity_id":"light.ikea_test2"}}

with JSONata

{"domain":"light","service":"turn_on","data":{"entity_id":"light.ikea_test2"}}

missing the “brightness_pct”

@michaelblight
you are correct i don’t need the msg statement

{
   "brightness_pct": $round(
       attributes.brightness/2.55,
       0
   )+10
}

give me the same output

Thanks
Tim

I would guess attributes.brightness is the incorrect path. Drop a debug node with full output before the call-service node. My guess would be you need to use data.attributes.brightness.

Maybe i’m not communicating clearly.

{
    "entity_id": "light.ikea_test2",
    "state": "on",
    "attributes": {
        "brightness": 12,
        "off_brightness": null,
        "friendly_name": "IKEA of Sweden TRADFRI Driver 30W 396606fe level, on_off",
        "supported_features": 33
    },
    "last_changed": "2020-02-27T18:46:26.841787+00:00",
    "last_updated": "2020-02-27T18:46:26.841787+00:00",
    "context": {
        "id": "3242baddf173488b8044e058ddd3a0b7",
        "parent_id": null,
        "user_id": null
    },
    "timeSinceChangedMs": 3476991
}

is the debug from the previous node, i’m using it in the JSONata editor test message.

I’m accessing the incoming message fine, “attributes.brightness” is correctly being read as 12 and is going through my maths, to become "brightness_pct": 15 as per the results above.

I’m struggling because a JSONata result of

{
    "brightness_pct": 15
}

behaves differently to JSON

{"brightness_pct": 15}

the JSON version ends up in my output message and the JSONata version doesn’t, as per the debug dumps in my last message.

and I’ve spent hours trying to work out why.

Thanks Tim

I’ve not solved this but HA 106 give another option

brightness_step_pct

much easier, so I’ve moved on and not understood why this doesn’t work.
Thanks for the help

Tim