MQTT light json template

I have tried the following template in the Developer Tools - Home Assistant page and it returne fine:
‘{% set mylight = states.light.rpibedroomscreen.attributes.brightness %} {“piscreen”: {“power”: 1, “brightness”: {{ mylight }} }}’

If the light is on and I change the brightness it passes the previous brightness in the value.
/RPi/bedroom/set {“piscreen”: {“power”: 1, “brightness”: 50 }}

no matter what the new value is changed to
If the light is off it passes the null value
/RPi/bedroom/set {“piscreen”: {“power”: 1, “brightness”: }}

How can I get the system to update the brightness before the command and retain brightness when off

- platform: mqtt
   schema: template
   name: "RPiBedroomScreen"
   unique_id: "RPiBedroomScreen"
   state_topic: "/RPi/bedroom/status"
   state_template: "{{ value_json.data.piscreen.power }}"
   value_template: "{{ value_json.data.piscreen.brightness }}"
   brightness_template: "{{ value_json.data.piscreen.brightness }}"
   command_topic: "/RPi/bedroom/set"
   command_off_template: '{"piscreen": {"power":0}}'
   command_on_template: '{% set mylight = states.light.rpibedroomscreen.attributes.brightness %} {"piscreen": {"power": 1, "brightness": {{ mylight }} }}'
   json_attributes_topic: "/RPi/bedroom/status"

I am still working on it.