YAML rules for an entire dashboard or everywhere

I am using this rule to abbreviate some of the messages in my cards. It’s these sort of rules:

{{ replace(" minutes", "m") | replace(" minute", "m") }}

But I need to add this to every card and every message so far. Is there a way to add this piece of YAML in a generic way so it applies everywhere in my UI or at least in a specific dashboard?

that template is invalid, it will always error. Can you please post an actual template that uses what you’re referring to?

Sorry, I didn’t want to make it complex. Here it is:

{{ relative_time(states.binary_sensor.kitchen_presence_sensor_presence.last_changed) | replace(" minutes", "m") | replace(" minute", "m") }} ago
{{ relative_time(current_time - time_diff) | replace(" minutes", "m") | replace(" minute", "m") | replace(" seconds", "s") }} until off

You can make a macro that does that. Make it a global macro and then use that everywhere.

my_macros.jinja

{%- macro relative_time_abv(dt) -%}
{{- relative_time(dt) | replace(" years", "y") | replace(" year", "y") | replace(" weeks", "w") | replace(" week", "w") | replace(" days", "d") | replace(" day", "d") | replace(" hours", "h") | replace(" hour", "h") | replace(" minutes", "m") | replace(" minute", "m") | replace(" seconds", "s") | replace(" second", "s") -}}
{%- endmacro -%}

then use it

{% from 'my_macros.jinja' import relative_time_abv %}
{{ relative_time_abv(states.binary_sensor.kitchen_presence_sensor_presence.last_changed) }}
1 Like

Thanks for the help and adding all the variants. I have 0 experience with macros, but this sounds promising.

But still, would I need to import the macro in every single card and string of text? Or is there a way to import it once for the entire dashboard?
That’s kind of the point I’m trying to avoid.

Yes

no

Ok :frowning:

For my understanding then. How is it that global dashboard rules apply? I mean, CSS or JS resources that we load on the frontend affect everything in our screen. Not just a single card. Themes are the same etc. How is that different?

I have no idea what you’re asking here.

I’m saying that there are files that can apply to an entire dashboard. Both CSS and JS files. That’s how themes work afaik. Or when we install a new card, it applies to any instance of the card that you add. So those files/rules are being applied globally.

Why can’t we do the same in this instance? Couldn’t I add this macro to the mushroom card files themselves?

Jinja isn’t handled in the frontend, it’s handled in the backend. Secondly, this is how you import reusable functions in jinja. There’s no way around it.

Can I do it in JS then? I’ll search the functions myself, I just want to know if it’s possible and if you can point me in the right direction on how to add the file or snippet so that it affects an entire dashboard.

Probably not, you’ll still need to specify fields that it needs to run on.