Hide value and other text in markdown card if a sensor value is zero

I have made a markdown card that shows the gas prices in my area. It looks like this:

I would like to hide the “x timer” (x hours in Norwegian) when x is 0, so it will only show minutes, not hours. This is the code for one of the entries:

**Best:** {{ states('sensor.bensinpris_hos_best_mandal') }}, oppdatert for {{ states('sensor.bensinpris_oppdatert_timer_hos_best_mandal') }} timer og {{ states('sensor.bensinpris_oppdatert_minutter_hos_best_mandal') }} minutter siden

I’m sure there is a way to do this, but how?

You can do Jinja2 too.

{% if (states('sensor.prices') !=0) %}
  {{ states('sensor.prices') }}
{% endif %}

But in this case it would probably be easier to just do

**Best:** {{ (states('sensor.bensinpris_hos_best_mandal') }}, oppdatert for {{ states('sensor.bensinpris_oppdatert_timer_hos_best_mandal') }} timer og {{ states('sensor.bensinpris_oppdatert_minutter_hos_best_mandal')).replace('0 timer og','') }} minutter siden

The truck is that this should replace the text you want to remove with nothing and because the 0 is included in the replace command, then other values should not be affected.

1 Like

Thank you very much! I should have thought about that myself, I have a lot of text replacement in my sensors. :roll_eyes:

Edit: I had to change a little bit to make it work, but the working hour part was:

{{ (states('sensor.bensinpris_oppdatert_timer_hos_best_mandal') ~ ' timer og').replace('0 timer og','') }}