Perform maths within service data?

I’m using the set_humidity service to set the humidity. The service data looks like:

{"humidity":20}

I would like to add the value of the entity input_number.condensation_offset to the value of 20. Something like:

{"humidity":20+input_number.condensation_offset}

Anyone know what the correct syntax is?

Given you have shown the data as JSON, I’m guessing you’re using Node Red.

If this is the case, you would use a “Current State” node before this one to load the number into the message (say msg.offset), and then use JSONata instead of JSON with the following:

{"humidity": 20+offset }

If you’re not using Node Red and are doing it in an HA automation, you need to use a template

data: 
  value: "{{ 20 + states('input_number.condensation_offset') | int(0) }}"

Note that you need to convert the offset from a string to a number - I used “int”, but if it should contain decimals, use “float”. I also added a default of 0, so it won’t have a problem if the value is unavailable for some reason.

Thanks! I’m using NodeRed and the Current State node did the trick. I just had to make sure to select “State Type: Number” in the Current State Node.

I have moved your post to the NR category. Please choose the correct category in future to prevent this sort of confusion. Thanks.

{"humidity":20 + $number($entities("input_number.condensation_offset").state)}

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/call-service.html#doing-arithmetic