Templating in Lovelace card

Hello.
Is it possible to create a new entity within a Lovelace card or is it only possible via Template in the configuration.yaml file? As an example I have a temperature sensor which displays to 1 decimal place (63.3) and I want to round it down (63). The Lovelace YAML is:

 entities:
  - entity: sensor.server_closet_environment_temperature
    name: Temperature

I can use templates to create a new sensor in configuration.yaml:

{% set new_entity = states('sensor.server_closet_environment_temperature') | round(0) %}
{{ new_entity }}

but I am hoping that this could be incorporated into the Lovelace YAML.

 {% set new_entity = states('sensor.server_closet_environment_temperature') | round(0) %}
 {{ new_entity }}
entities:
  - entity: sensor.new_entity
    name: Temperature

This doesn’t work but is the concept viable?
Ron

1 Like

No, you have to make template sensors and expose the template sensors. Lovelace doesn’t allow templates unless you’re using the markdown card or a custom card.

Thank you for the prompt reply. Any thoughts for incorporating this feature? The current situation works but it would be awfully nice to have simple data transformations incorporated into the card.

1 Like

@petro I’d like to fu on this situation. Is there a ‘macro’ or ‘function(x)’ option in home assistant such that a variable is passed to the function and a value is returned?

There are macros, but they can only be used inside a template. You can’t share macros from one template to another.

{% macro abc(foo) %}
  {{ foo * 2 }}
{% endmacro %}
{{ abc(2) }}
{{ abc(3) }}
{{ abc(4) }}

This is all very helpful information but a bit frustrating because of all the coding repetition. Or perhaps I am missing something. All I want is a function to round a number to an arbitrary accuracy w/o the need to make a template each time. My current understanding is:

  • No coding in a Lovelace card
  • Templates only read sensor data (no passing data to a template).
  • Functions can be created within a template but are only usable within that template.

What about scripts?
Thanks for your help.
rms

And that’s not possible.

Templating in Lovelace card can be done using Lovelace Plugins · thomasloven/hass-config Wiki · GitHub

Another option for templating in the UI that I found simpler to use than card-mod is: “template-entity-row” (find it in HACS)

An example:

            - type: custom:template-entity-row
              name: HTPC Kernel
              icon: mdi:linux
              state: "{{ states('sensor.htpc_kernel_version') + ' (' + states('sensor.htpc_version') + ')' }}"