Smooth RGB transition

I would like to acquire smooth transition between RGB colors.
For example:
Run every 5 mins -> Check last RGB colors, choose new random colors -> make smooth transition between old colors to new colors.

I made this right now, and it works in templates, but I can’t get it to work in automations.
It is only for RED color right now, because I’m only testing…

       {% set oldcolor_r = states.light.philips_ambilight.attributes.rgb_color[0] %}
       {% set oldcolor_g = states.light.philips_ambilight.attributes.rgb_color[1] %}
       {% set oldcolor_b = states.light.philips_ambilight.attributes.rgb_color[2] %}
       {% set newcolor_r = (range(0, 255)|random) %}
       {% if oldcolor_r < newcolor_r %}
       {% for i in range(oldcolor_r, newcolor_r) %}
       '{{i}}','{{ oldcolor_g }}','{{ oldcolor_b }}'
       {% endfor %}
       {% elif oldcolor_r > newcolor_r %}
       {% for i in range(oldcolor_r, newcolor_r, -1) %}
       '{{i}}','{{ oldcolor_g }}','{{ oldcolor_b }}'
       {% endfor %}
       {% endif %}