Template: if value <=1024 unit_of_measurement: "MB" , elseif value >1024 unit_of_measurement: "GB"

I’m used to just using a single unit of measurement for each sensor template like the below sample code. However, I’d like for the unit of measurement to automatically change from MB to GB if the respective value is more than 1024.

      process_0_virt:
        value_template: >
                {% set process_0_virt = (state_attr('sensor.processlist0', 'memory_info'))[0]  %}
                {{ ((process_0_virt / 1024) / 1024) | round(1) }}
        availability_template: >-
                {{ states('sensor.processlist0') not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: "MB"

Is this possible? I’m almost certain the below code is wrong since I was getting a syntax error in the template editor. However, I thought I’d at least post something (even if its wrong) rather than nothing at all:

      process_0_virt:
        value_template: >
                {% set process_0_virt = (state_attr('sensor.processlist0', 'memory_info'))[0]  %}
                {% set value = ((process_0_virt / 1024) / 1024) | round(1) %}
                {{ value }}
        availability_template: >-
                {{ states('sensor.processlist0') not in ["unknown", "unavailable", "none"] }}
                {% if (value <= 1024 ) %}
                  {{ unit_of_measurement: "MB" }}
                {% elif (value > 1024 ) %}
                  {{ unit_of_measurement: "GB" }}

That’s a terrible idea.

For display purposes you could have a master MB sensor and a template GB sensor tracking it, then choose which one to display using e.g. conditional rows on an entities card.

2 Likes

Thanks for the quick response. I didn’t understand when you said “a template GB sensor tracking it,”. Can you give me an example considering below MB sensor is what I have to start with? I’m using a Markdown card, so hopefully, I’ll be able to figure out how to apply to condition between the 2 in my Markdown card. Thank you for your time.

      process_0_virt:
        value_template: >
                {% set process_0_virt = (state_attr('sensor.processlist0', 'memory_info'))[0]  %}
                {{ ((process_0_virt / 1024) / 1024) | round(1) }}
        availability_template: >-
                {{ states('sensor.processlist0') not in ["unknown", "unavailable", "none"] }}
        unit_of_measurement: "MB"

If you’re set on using the legacy format, then:

process_0_virt_gb:
  value_template: "{{ states('sensor.process_0_virt')|float(0) / 1024 }}"
  availability_template: "{{ states('sensor.process_0_virt') not in ['unknown', 'unavailable', 'none'] }}"
  unit_of_measurement: "GB"
1 Like

FYI:

  1. You can only use templates to set the value of a key: value pair. Your template attempts to return both key and value, {{ unit_of_measurement: "MB" }}
  2. You can’t use the availability template to set anything other than the availability of the template sensor. It must only return a result of true or false.
  3. The unit_of_measurement does not accept templates.
  4. The variable value is undefined in your template.

Did you use Chat GPT to help generate this?

3 Likes

Modern format version:

template:
  - sensors:

      - name: "Process 0 Virt MB"
        unique_id: b967808e-41b5-4fd0-a9fa-a38ac08c8928
        state: "{{ state_attr('sensor.processlist0', 'memory_info')[0] / 2**20 }}"
        availability: "{{ states('sensor.process_0_virt') not in ['unknown', 'unavailable', 'none'] }}"
        unit_of_measurement: "MB"

      - name: "Process 0 Virt GB"
        unique_id: 30fae841-93f6-4829-9949-a58b5e36aebf
        state: "{{ state_attr('sensor.processlist0', 'memory_info')[0] / 2**30 }}"
        availability: "{{ states('sensor.process_0_virt') not in ['unknown', 'unavailable', 'none'] }}"
        unit_of_measurement: "GB"
2 Likes

Thank you! This is exactly what I was looking for. I didn’t even know I could use ChatGPT for Home Assistant template creation.

I really appreciate it. I have dozens of legacy templates I need to upgrade to the new template integration. I’ve been putting it off from a while now. I’ll use your examples here to help me migrate all my templates over to the new standard. I can either use your suggested solution or @tom_l 's solution. Both of your responses were very helpful to me.

Keep it that way. It’s no good at it.

1 Like

I did not provide a solution, just details of why your method won’t work.