How to use the current brightness state of a lamp in setting the new state

I am trying to get the current brightness of my light to determine how far I should move it up or down to get a desired brightness.

Background: I use this for the light in the bathroom. During the day I like it to be bright, but at night I like it to be dimmed down so it won’t hurt my eyes or wake me up too much. I currently have it working, but it can only go all the way up or down. I would like to be able to get a value in the middle too. I have a Nordtronic Dimmer and I want to use the move or step commands because they work regardless of whether the light is on or off. I have verified that the entity will report it’s state if it is off.

I have the following YAML code:

service: mqtt.publish
data:
  topic: zigbee2mqtt/Bathroom_Dimmer/set
  payload: "{"brightness_step": '150-{{state_attr('light.Bathroom_Dimmer','brightness')|int}} }"

When I try to save it I get the following error:
Message malformed: template value should be a string for dictionary value @ data[‘action’][0][‘data’]

Does anyone have an idea what I am doing wrong? Thanks in a lot in advance.

Given that the error message seems to indicate this is an automation and you gave us three lines of code all we can go is guess what the issue is.
My guess is your excessive use of " in the last line.
Keep the two outer but make the ones surrounding brightness a ' instead.

@Hellis81:

Thanks a lot for the tip. That did solve the error. It seems the only problem now is that it is not evaluating the numerical expression. You were also right that it is not the hole code. I’ve set it up in the visual editor, and the rest is empty, I have not added a trigger yet.

The whole code is:

alias: Dim Bathroom Light by step2
description: Dim Bathroom Light at Night
trigger: []
condition: []
action:
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/Bathroom_Dimmer/set
      payload: >-
        {"brightness_step":
        150-{{state_attr('light.Bathroom_Dimmer','brightness')|int}}}
mode: single

Now it gives the output:

Message 7 received on zigbee2mqtt/Bathroom_Dimmer/set at 10:07 PM:
{"brightness_step": 150-255}
QoS: 0 - Retain: false 

Do you have any idea how to get it to evaluate?

Use payload_template: not payload:

https://www.home-assistant.io/docs/mqtt/service/#service-mqttpublish

Thanks for the advice.

I have it working for now by moving the “150 -” in to the brackets. It seems the Jinja2 engine is used to process, and I found help on it here:
https://jinja.palletsprojects.com/en/latest/templates/#math
(information intended for others struggling with this)

I will certainly look into the templates further another time (it’s late here), thanks for the pointer!