Skip to content

[Bug] ForceSensor methods return <Async> instead of a value in an async program #2867

Closed

Description

@GizmoBricks

Describe the bug
Any ForceSensor method reterns <Async> instead of a value, when used in async mode.
Other sensors in the same situation work just fine.

To reproduce
Steps to reproduce the behavior:

from pybricks.pupdevices import ColorSensor, ForceSensor
from pybricks.parameters import Color, Port
from pybricks.tools import wait, run_task


fs = ForceSensor(Port.A)
cs = ColorSensor(Port.B)

async def foo():
    return await fs.distance(), await cs.color()

async def main():
    data = await foo()
    print(data)
    await wait(10)

print(fs.distance(), cs.color())  # may be any other ForceSensor method
run_task(main())

Result:
The ForceSensor returns <Async> instead of a value

0.0 Color.NONE   # Normal behaviour outside async
(<Async>, Color.NONE)  # ForceSensor - bug behaviour, ColorSensor - normal behaviour

Expected behavior
ForceSensor methods to work in async mode.

Screenshots
There is a saying that a picture is worth a 1000 words. Screenshots really help to identify and solve problems.

Image

Activity

  1. added
    triage
    on Oct 19, 2026
  2. BertLindeman commented on Oct 19, 2026

    @BertLindeman

    Hi,

    Have tested on v4.1.0 firmware.

    An updated script that shows some of the ForceSensor methods are double wrapped:

    from pybricks.pupdevices import ForceSensor
    from pybricks.parameters import Port
    from pybricks.tools import run_task
    
    fs = ForceSensor(Port.C)
    
    
    async def main():
        d1 = await fs.distance()
        d2 = await d1
        print("distance 1x:", type(d1), " 2x:", type(d2), d2)
    
        f1 = await fs.force()
        f2 = await f1
        print("force    1x:", type(f1), " 2x:", type(f2), f2)
    
        t1 = await fs.touched()
        t2 = await t1
    
        print("touched  1x:", type(t1), " 2x:", type(t2), t2)
    
        p = await fs.pressed()
        print("pressed  1x:", type(p), p)
    
    
    run_task(main())

    Current output:

    distance 1x: <class 'Async'>  2x: <class 'float'> 0.048
    force    1x: <class 'Async'>  2x: <class 'float'> 0.0
    touched  1x: <class 'Async'>  2x: <class 'bool'> False
    pressed  1x: <class 'bool'> False
    
  3. laurensvalk commented on Oct 20, 2026

    @laurensvalk
    Member

    Thanks for reporting. This is definitely a bug.

  4. added
    software: pybricks-micropython
    topic: sensors
    and removed
    triage
    on Oct 20, 2026
  5. added theissue type on Oct 20, 2026
  6. laurensvalk commented on Oct 23, 2026

    @laurensvalk
    Member

    A fix is included in the latest release.

  7. BertLindeman commented on Oct 23, 2026

    @BertLindeman

    Running on ('primehub', '4.1.1', 'ci-build-5123-v4.1.1-6-gdf04d7220 on 2026-10-23')
    I still have this as output on my test script:

    distance 1x: <class 'Async'>  2x: <class 'float'> 0.048
    force    1x: <class 'Async'>  2x: <class 'float'> 0.0
    touched  1x: <class 'Async'>  2x: <class 'bool'> False
    pressed  1x: <class 'bool'> False
    

    Maybe this v4.1.1-6 is not the version you meant?

  8. laurensvalk commented on Oct 24, 2026

    @laurensvalk
    Member

    Thank you. I misread. The blocks were missing await, which is now fixed, but it looks like this is a different issue.

  9. BertLindeman commented on Oct 24, 2026

    @BertLindeman

    Thank you. I misread. The blocks were missing await, which is now fixed, but it looks like this is a different issue.

    reopen or do I add a new issue for "some forcesensor methods return "<Await>" in stead of the value"?

  10. laurensvalk commented on Oct 24, 2026

    @laurensvalk
    Member

    Found it. Should be fixed in a release next week. Thanks!

     // pybricks.pupdevices.ForceSensor.force
     static mp_obj_t get_force(mp_obj_t self_in) {
    @@ -51,7 +51,7 @@ static mp_obj_t get_force(mp_obj_t self_in) {
         mp_obj_t result = pb_obj_new_fraction(force, 1000);
         return pb_type_async_return_result(result, &self->device_base.last_awaitable);
     }
    -static PB_DEFINE_CONST_TYPE_DEVICE_METHOD_OBJ(get_force_obj, LEGO_DEVICE_MODE_PUP_FORCE_SENSOR__FRAW, get_force);
    +static MP_DEFINE_CONST_FUN_OBJ_1(get_force_obj, get_force);
  11. laurensvalk commented on Oct 24, 2026

    @laurensvalk
    Member

    Fixed via pybricks/pybricks-micropython@d234acc when it finishes building.

  12. BertLindeman commented on Oct 24, 2026

    @BertLindeman

    Ran ('primehub', '4.1.1', 'ci-build-5124-v4.1.1-9-gd234acc42 on 2026-10-24')

    Now shows:

    distance 0.048
    pressed  False
    released True
    force    0.0
    touched  touch block missing
    
    Test script: block coded issue_2867_blocks
    # pybricks blocks file:{"blocks":{"languageVersion":0,"blocks":[{"type":"blockGlobalSetup","id":"bjK,wS1MYO7aiYkFSwd{","x":150,"y":100,"deletable":false,"next":{"block":{"type":"variables_set_force_sensor","id":"*5A1]9y_fj8iC3Vk]vwp","fields":{"VAR":{"id":"TBX=V9GHc]:b#ImO^|6D"}},"inputs":{"PORT":{"shadow":{"type":"blockParametersPort","id":")P`e(FrdpCF5[YO-n@i.","fields":{"NAME":"C"}}}}}}},{"type":"blockGlobalStart","id":"3tJe|AWl0baN(wH9a$@.","x":150,"y":300,"deletable":false,"next":{"block":{"type":"blockMultiTask","id":"I.zv|3aY6iwvVuD*0^F*","extraState":{"optionLevel":0},"fields":{"METHOD":"MULTITASK_ALL"},"inputs":{"TASK0":{"block":{"type":"blockFunctionCallerStatement","id":"8=$U_Er9ig|ZwuOJm0JZ","extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"@6z2ZIE!6@IYa$_Q!y{E"}}}}}}}},{"type":"variables_setup_function","id":"JGhX4sU~5$R4([RN78Gq","x":588,"y":84,"extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"%|9Igw|gGMJ4E|m}!Gsm"}},"inputs":{"STACK":{"block":{"type":"blockTaskReturn","id":"EV::kfcqDqO/0B|8mj-s","inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"rS-j[RDG3hv/=sR+TNTj","fields":{"NUM":0}},"block":{"type":"blockDistance","id":"(VHO!]1$D!x~CuISZY56","inputs":{"VAR":{"shadow":{"type":"variables_get_distance_device","id":"|A~B5]z3jQVmHyx.+c?c","fields":{"VAR":{"id":"TBX=V9GHc]:b#ImO^|6D","name":"fs","type":"ForceSensor"}}}}}}}}}}}},{"type":"variables_setup_function","id":"Jwf%K,xm=39eKhM2b10,","x":163,"y":833,"extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"@6z2ZIE!6@IYa$_Q!y{E"}},"inputs":{"STACK":{"block":{"type":"blockPrint","id":"ox5Wi?5|_,hByBR`J=rj","extraState":{"optionLevel":1},"fields":{"SEP":"SEP_SPACE"},"inputs":{"TEXT0":{"shadow":{"type":"text","id":"--gp`?j%$1#:{%N,3Mg%","fields":{"TEXT":"distance"}}},"TEXT1":{"shadow":{"type":"text","id":"*]C)y%9|j0Xg+@:7Nyrq","fields":{"TEXT":"abc"}},"block":{"type":"blockFunctionCallerValue","id":"5.^//AJj@p%mCDKU!*Sw","extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"%|9Igw|gGMJ4E|m}!Gsm"}}}}},"next":{"block":{"type":"blockPrint","id":"U%@W;s{j|-FTmR/Y-b%2","extraState":{"optionLevel":1},"fields":{"SEP":"SEP_SPACE"},"inputs":{"TEXT0":{"shadow":{"type":"text","id":"hj/5El+xN~tMSNu[:hFM","fields":{"TEXT":"pressed "}}},"TEXT1":{"shadow":{"type":"text","id":"*]C)y%9|j0Xg+@:7Nyrq","fields":{"TEXT":"abc"}},"block":{"type":"blockFunctionCallerValue","id":"6QZr,7q@HOZq|){:!ncv","extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"[email protected]=T9RSJ|$XX]g~"}}}}},"next":{"block":{"type":"blockPrint","id":"{56N@=#yK2/f|3R6K`:P","extraState":{"optionLevel":1},"fields":{"SEP":"SEP_SPACE"},"inputs":{"TEXT0":{"shadow":{"type":"text","id":"=#zAPd2ri,UFf2jvTqNs","fields":{"TEXT":"released"}}},"TEXT1":{"shadow":{"type":"text","id":"GnwCY:b7)R+]D{#bD#R.","fields":{"TEXT":"abc"}},"block":{"type":"blockFunctionCallerValue","id":"zd`s}[n2,*:tG|_avAI;","extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"yh1dPXZLIM`^gqIHI70;"}}}}},"next":{"block":{"type":"blockPrint","id":"g5XBcn|%;_NqrzFy%Mew","extraState":{"optionLevel":1},"fields":{"SEP":"SEP_SPACE"},"inputs":{"TEXT0":{"shadow":{"type":"text","id":"L16cbspo:Bn;ANi=%wKk","fields":{"TEXT":"force   "}}},"TEXT1":{"shadow":{"type":"text","id":"yxMGHLK+3bPImT7{/+9-","fields":{"TEXT":"abc"}},"block":{"type":"blockFunctionCallerValue","id":"H?G58|I2{cETq2]IYzRG","extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"9mS=1Oa$3~XO),h)7Dxq"}}}}},"next":{"block":{"type":"blockPrint","id":"w|Cd;EIDHMyO68Z*[nbL","extraState":{"optionLevel":1},"fields":{"SEP":"SEP_SPACE"},"inputs":{"TEXT0":{"shadow":{"type":"text","id":"9$S4v$t[F54+7`kw~2/(","fields":{"TEXT":"touched "}}},"TEXT1":{"shadow":{"type":"text","id":"yxMGHLK+3bPImT7{/+9-","fields":{"TEXT":""}},"block":{"type":"blockFunctionCallerValue","id":"z/hW%?+0?bQz{~ZE;g^:","extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"49/n,}_n-OCpdqnzL#Ka"}}}}}}}}}}}}}}}}},{"type":"variables_setup_function","id":"4+ROIDb(Hku|9w3Xx2G(","x":592,"y":237,"extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"[email protected]=T9RSJ|$XX]g~"}},"inputs":{"STACK":{"block":{"type":"blockTaskReturn","id":"7|b]fx*]x{nR2`)tm9+M","inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"rS-j[RDG3hv/=sR+TNTj","fields":{"NUM":0}},"block":{"type":"blockSensorPressed","id":"i#Ge3v1V~(dewEXx-+o[","fields":{"METHOD":"SENSOR_PRESSED"},"inputs":{"VAR":{"shadow":{"type":"variables_get_touch_device","id":"[email protected]{0e7$Hjs=cJ","fields":{"VAR":{"id":"TBX=V9GHc]:b#ImO^|6D","name":"fs","type":"ForceSensor"}}}}}}}}}}}},{"type":"variables_setup_function","id":"zWI4PN}IB*+C)@_`.BYn","x":598,"y":530,"extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"9mS=1Oa$3~XO),h)7Dxq"}},"inputs":{"STACK":{"block":{"type":"blockTaskReturn","id":"S5D0{2i/vN{D;lhzcYf,","inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"rS-j[RDG3hv/=sR+TNTj","fields":{"NUM":0}},"block":{"type":"blockSensorPressed","id":",J,ii~f1yV56o+O,f6D|","fields":{"METHOD":"SENSOR_FORCE"},"inputs":{"VAR":{"shadow":{"type":"variables_get_touch_device","id":"6XyJy4^x{13y]FVDA8RJ","fields":{"VAR":{"id":"TBX=V9GHc]:b#ImO^|6D","name":"fs","type":"ForceSensor"}}}}}}}}}}}},{"type":"variables_setup_function","id":"Sqpe])t$X7=VoNwh]Pf-","x":596,"y":382,"extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"yh1dPXZLIM`^gqIHI70;"}},"inputs":{"STACK":{"block":{"type":"blockTaskReturn","id":"%Egq`#@+rRzR5hi=5QY3","inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"rS-j[RDG3hv/=sR+TNTj","fields":{"NUM":0}},"block":{"type":"blockSensorPressed","id":"w^_}{6-`(%d{[@{Ou9@:","fields":{"METHOD":"SENSOR_RELEASED"},"inputs":{"VAR":{"shadow":{"type":"variables_get_touch_device","id":"AWcd2.T{wL8Oyx0a~6Kv","fields":{"VAR":{"id":"TBX=V9GHc]:b#ImO^|6D","name":"fs","type":"ForceSensor"}}}}}}}}}}}},{"type":"variables_setup_function","id":":0@I0S|NpQvRq!Zi8bet","x":606,"y":674,"extraState":{"optionLevel":0},"fields":{"ICON":"TASK","VAR":{"id":"49/n,}_n-OCpdqnzL#Ka"}},"inputs":{"STACK":{"block":{"type":"blockTaskReturn","id":"6P.=x!|ny/sf[7QIfX/8","inputs":{"VALUE0":{"shadow":{"type":"blockMathNumber","id":"rS-j[RDG3hv/=sR+TNTj","fields":{"NUM":0}},"block":{"type":"blockTextLiteral","id":"]q1I.0cI7obsc==2-_ww","fields":{"TEXT":"touch block missing"}}}}}}}}]},"variables":[{"name":"red","id":"tv6f$.ks;-,EodXJGo`5","type":"ColorDef"},{"name":"orange","id":"neAqBfLJgEoj(!lU~q`Y","type":"ColorDef"},{"name":"yellow","id":"-_4JZA[*V*,G3$Wu%lII","type":"ColorDef"},{"name":"green","id":"t8eF3L,YMu128F*LhFz,","type":"ColorDef"},{"name":"cyan","id":",cd%w0$wbJwFmy_]X=mr","type":"ColorDef"},{"name":"blue","id":"/To]g_ekel$VO2a{QP*W","type":"ColorDef"},{"name":"violet","id":"hwil@#ftP5;r_2F`wsdK","type":"ColorDef"},{"name":"magenta","id":"XiguE30JZ4])0R^U$4xT","type":"ColorDef"},{"name":"white","id":"3J@,w@T#gf])vx:(qY-Q","type":"ColorDef"},{"name":"none","id":"Au3aI.K|gZYEIK)%xq8o","type":"ColorDef"},{"name":"fs","id":"TBX=V9GHc]:b#ImO^|6D","type":"ForceSensor"},{"name":"dist","id":"%|9Igw|gGMJ4E|m}!Gsm","type":"Function"},{"name":"main","id":"@6z2ZIE!6@IYa$_Q!y{E","type":"Function"},{"name":"pressed","id":"[email protected]=T9RSJ|$XX]g~","type":"Function"},{"name":"force","id":"9mS=1Oa$3~XO),h)7Dxq","type":"Function"},{"name":"rel","id":"yh1dPXZLIM`^gqIHI70;","type":"Function"},{"name":"touched","id":"49/n,}_n-OCpdqnzL#Ka","type":"Function"},{"name":"my task","id":"M=ZMsR50Qw~K$`MYO*[d","type":"Function"}],"info":{"type":"pybricks","version":"2.1.1"},"workspaceOptions":{"scrollX":125.63625443206325,"scrollY":-232.10195602157796,"scale":0.753660650324248}}
    from pybricks.parameters import Port
    from pybricks.pupdevices import ForceSensor
    from pybricks.tools import multitask, run_task, wait
    
    # Set up.
    fs = ForceSensor(Port.C)
    
    async def dist():
        await wait(0)
        return await fs.distance()
    
    async def pressed():
        await wait(0)
        return await fs.pressed()
    
    async def rel():
        await wait(0)
        return not await fs.pressed()
    
    async def force():
        await wait(0)
        return await fs.force()
    
    async def touched():
        await wait(0)
        return 'touch block missing'
    
    async def main2():
        await wait(0)
        print('distance', await dist())
        print('pressed ', await pressed())
        print('released', await rel())
        print('force   ', await force())
        print('touched ', await touched())
    
    async def main():
        await multitask(
            main2(),
        )
    
    
    run_task(main())

    My original program with double await now correctly fails:

    ('primehub', '4.1.1', 'ci-build-5124-v4.1.1-9-gd234acc42 on 2026-10-24')
    Traceback (most recent call last):
      File "issue_2867_explanation.py", line 26, in <module>
      File "issue_2867_explanation.py", line 12, in main
    TypeError: 'float' object isn't iterable
    
    with single awaits: issue_2867_explanation_single_await
    from pybricks.pupdevices import ForceSensor
    from pybricks.parameters import Port
    from pybricks.tools import run_task
    from pybricks import version
    print(version)
    
    fs = ForceSensor(Port.C)
    
    
    async def main():
        d1 = await fs.distance()
        # d2 = await d1
        print("distance 1x:", type(d1), d1)  # , " 2x:", type(d2), d2)
    
        f1 = await fs.force()
        # f2 = await f1
        print("force    1x:", type(f1), f1)  # , " 2x:", type(f2), f2)
    
        t1 = await fs.touched()
        # t2 = await t1
        print("touched  1x:", type(t1), t1)  # , " 2x:", type(t2), t2)
    
        p = await fs.pressed()
        print("pressed  1x:", type(p), p)
    
    run_task(main())

    Result:

    ('primehub', '4.1.1', 'ci-build-5124-v4.1.1-9-gd234acc42 on 2026-10-24')
    distance 1x: <class 'float'> 0.048
    force    1x: <class 'float'> 0.0
    touched  1x: <class 'bool'> False
    pressed  1x: <class 'bool'> False
    

    So fixed in work branch.

  13. laurensvalk commented on Oct 29, 2026

    @laurensvalk
    Member

    Fix has been released. Thanks for reporting and testing!

  14. BertLindeman commented on Oct 29, 2026

    @BertLindeman

    Confirm fixed on v4.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    software: pybricks-micropythontopic: sensors

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions