Trying to parse an mqtt string with a template

I have an MQTT sensor that publishes a color as a string in the format “#GGRRBB”. In other words pure green would be #FF0000. I would like to convert that to 3 ints. I’ve be futzing around with the template in the developer tools, but I can’t even get slice to work.

{{states('sensor.maintank_color")}} displays #ffffef which is correct. Is there a way with templates to convert that to 3 ints?

Not sure what you want to do with the numbers, but…

{% set x = states('sensor.maintank_color') %}
{{ x[1:3]|int(base=16) }}
{{ x[3:5]|int(base=16) }}
{{ x[5:7]|int(base=16) }}

The end goal is ultimately mostly pointless. It will allow me to plot the RGB values separately. That said, I have a better understanding of what I was doing wrong and hopefully I’ll be able to apply that in the future. Thanks!

1 Like