Mi Cube Side Template

Hi there,

I’ve recently added a Mi Cube to my setup, and I wanted to somehow track which side it is facing up currently to automate rotation action. Upon searching, all I could find is either use a template sensor or a blueprint. My cube is not supported by blueprints as it seems because I’m using Xiaomi Gateway3 integration, not ZHA (but I probably should at some point).

The integraton exposes a cube action entity, which briefly changes to the action triggered by cube and then goes blank. I’ve come up with this template sensor that reads the last side that was facing up either from side or to_side attribute. I know that its a total mess, as I’ve just started messing with Jinja and templates. The thing is, the sensor also briefly shows the side number and then goes blank too. How do I preserve it?

Thanks in advance!

{% if is_state("sensor.cube_action", "slide") -%}
  {{state_attr('sensor.cube_action', "side")}}
{%- elif is_state("sensor.cube_action", "flip90") -%}
  {{state_attr('sensor.cube_action', "to_side")}}
{%- elif is_state("sensor.cube_action", "flip180") -%}
  {{state_attr('sensor.cube_action', "to_side")}}
{%- elif is_state("sensor.cube_action", "tap") -%}
  {{state_attr('sensor.cube_action', "side")}}
{%- endif %}

image