Lambda dfplayer help for R2d2

So I’ve made the R2d2 bruh multisensory and I’m working in the firmware now.

Looking to make automations for the DF player.
I’d like to find a way to play one random track upon being triggered, then stop at the end of the track.

Right now I am able to play one track and stop, or play a random track and not stop until the entire source directory has played (unless I apply a set delay then stop).

Could anyone please help me write a lambda where a trigger plays one track at random from a directory on the sd card then stops when the track has finished playing.
I think I might have a directory for sounds like beeps and boops, a directory for music tracks, etc… In such a way that button press would play X type of sounds and motion would play Y type of sounds, and acceleration (gyro) would play Z type of sounds, all separated into different directories on the ad card.

I’m also going to want to add animations for the LEDs so some trigger causes him to make noise and flash various light patterns…

Also time based animations too, like if no activity for say one hour and suddenly motion, play a track…

Oh and also Ble presence detection, so if XXX is present after not being present for a a few hours, play a tack…

But i’ll take any help i can get!

Thank you in advance for your help!!!

Ok so I worked out a model to give you guys something to go off of…
I like the way this works, feel pretty animated so it should be a good starting point hopefully…

  - platform: esp32_touch
    name: "$hostname touch"
    pin: GPIO33
    threshold: 1000
    on_press:
      then:
#        - dfplayer.play: 1
#            file: 1
#            loop: false
        - dfplayer.random:
# couldn't figure out how to set a random flicker effect yet so on/off dim as a test
        - light.turn_on:
            id: R2_front_lamp
            effect: Flicker fast
        - light.turn_on:
            id: Cerveau_pos
            effect: Flicker
        - light.turn_on:
            id: Artoo_dome
            effect: Random fast
#        - output.set_level:
#            id: Artoo_front_lamp
#            level: "50%"
        - delay: 3s
        - light.turn_off:
            id: R2_front_lamp
        - light.turn_off:
            id: Cerveau_pos
        - light.turn_off:
            id: Artoo_dome
#        - output.set_level:
#            id: Artoo_front_lamp
#            level: "0%"
        - dfplayer.stop

##        - output.turn_on: buzzer
##        - delay: 1ms
##        - output.turn_off: buzzer
##    on_release:
##      then:
##        - output.turn_off: buzzer

So this makes his head move in a natural looking way, and the lights animate him further in a fun way.

Here’s an example :wink:

Thanks for helping out!

This is where i’m going with it… how far off am i? :upside_down_face:

  - platform: esp32_touch
    name: "$hostname touch"
    pin: GPIO33
    threshold: 1000
    on_press:
      then:
        - dfplayer.play:
            file: int
              !lambda  |- 'return file + srand;'
#            variables:
#            file: int
#            folder: int
#            file: 1
            loop: false
#        - dfplayer.random:

Not sure why this isn’t working…
Plays a track, but no lights for some reason…

  - platform: esp32_touch
    name: "$hostname touch"
    pin: GPIO33
    threshold: 1000
    on_press:
      then: 
        - dfplayer.play_next
    on_release:
#      if: 
      while:
        condition:
          dfplayer.is_playing
        then:
          - light.turn_on:
              id: R2_front_lamp
              effect: Flicker fast
          - light.turn_on:
              id: Cerveau_pos
              effect: Flicker
          - light.turn_on:
              id: Artoo_dome
              effect: Random fast
#        else:
#          - light.turn_off:
#              id: R2_front_lamp
#          - light.turn_off:
#              id: Cerveau_pos
#          - light.turn_off:
#              id: Artoo_dome