Getting the Miyoo Mini Plus battery into Home Assistant

I recently got the retro handheld  Miyoo Mini Plus, love it so far!
I also installed Onion OS on it, never even tried the stock experience. 

Your setup, especially in Home Assistant might differ, so try to adapt this guide to your needs.

But enough intro, you're likely reading this because you want the battery percentage into Home Assistant or some other system. This isn't a food blog where you need to scroll a mile to get to the recipe.

What you will need:
  • A Miyoo Mini Plus
  • Onion OS 4.2 (or newer as far as I know. I run 4.2.3 at the time of writing)
  • Home Assistant with the Syncthing Add-On configured
    • Or some other way of getting synced files read by Home Assistant
  • MicroSD card reader and a computer, of course
  • Some knowledge in Syncthing and Home Assistant
What you need to do:
  1. Install Syncthing on the SD card and start it. Make sure it works.
  2. With the card inn your computer, create a folder on the root of the card, named syncthing
  3. Open the hidden folder named .tmp_update, and inside there, open the file named runtime.sh with some text editor. Notepad, gedit, vscode, whatever.
  4. You should find the part about Syncthing there. Ctrl+f SYNCTHING
  5. Right below, insert this snippet:
        # Background job that copies battery stats to SDCARD every 5 minutes
        while true
        do
            cp /tmp/.axp_result /mnt/SDCARD/syncthing/.axp_result
            sleep 300
        done &
    This will start a background job (note the &) that copies the file with some battery info every 5 minutes (300 seconds).
  6. Save the file, unmount the SD card and insert it into the Miyoo Mini Plus and start it. It should start just fine and work as usual. If not, stop here and seek help on the subreddit where people are much smarter than me.
  7. Visit Syncthing on the device. http://<device_ip>:8384. Add the syncthing folder on the SD card. Should be /mnt/SDCARD/syncthing. Share it with the Syncthing add-on.
  8. Open Syncthing in Home Assistant. The web interface should prompt you to accept the Miyoo Mini Plus and the folder it wants to share. Accept it and set the folder to sync to /share/miyoominiplus/syncthing. The /share folder is one of the folders that the Syncthing Add-on shares with Home Assistant.
  9. Now create a command_line sensor that reads the synced file and parses it. Here is mine:
    - sensor:
        name: Miyoo Mini Plus Battery
        command: "cat /share/miyoominiplus/syncthing/.axp_result"
        json_attributes:
          - battery
          - voltage
          - charging
        value_template: "{{ value_json.battery }}"
        device_class: battery
        unit_of_measurement: "%"
Just restart Home Assistant to apply the sensor, and the entity should show up like this:

And you can use it on your dashboards or automate based on the number or whatever you want.

A note: In Onion OS 4.3, which currently is in beta, step 5 should be done using a startup script.
Another note: Instead of using Syncthing, you could also have the loop in step 5 simply do a curl to your Home Assistant with the contents of the .axp_result file. Oh well, I like Syncthing :) 

Thanks for reading!
/Kevin

Comments

Popular posts from this blog

Hidden settings in the Raspberry Pi Imager

Getting Company Portal apps locally using PowerShell (no auth!)