#HA & Jinja noob needs help with a simple template and jinja reuse

1 messages ยท Page 1 of 1 (latest)

livid escarp
#

I need to calculate current from the values that my inverter is providing

  - sensor:
      - name: "Tesla-HLE-3K Current"
        unit_of_measurement: "A"
        icon: mdi:current-ac
        state: >
          {% set voltage = states('sensor.tesla_hle_3k_voltage') | float(default=0) %}
          {% set power = states('sensor.tesla_hle_3k_power') | float %}
          {% set power_factor = states('sensor.tesla_hle_3k_power_factor') | float %}
          {% set current_divisor = voltage * (power_factor / 100) %}

          {% if (current_divisor != 0) %}
            {{ (power / current_divisor) | abs | round(2, default=0) }}
          {% else %} 'unavailable' {% endif %}

It works fine but I have few questions.

  • For things like this, is using template the recommended way or does HA have another trick up it's sleeve
  • Under certain conditions, the value cannot be calculated and therefore I return unavailable. Is that the correct implementation? I saw availability in the docs but I am confused. If the value unavailable of a state of a sensor does the job, then why is the availability attribute needed? I thought that the state template would not be evaluated if the availability attribute returns False. Please excuse me if that is a dumb question.
  • I had to do the same calculation for a few other energy sensors. Replication bit me a few times so I started looking for a way to reuse. Looks like it's not straightforward in the templating world. I came across https://github.com/home-assistant/core/pull/42244#issuecomment-716042818. python_script does not support imports so I looked at the custom component method but the custom component does not appear to be designed for providing such functionality. Like the iot_class manifest attribute seems irrelevant for such a use case. I looked for an example but could not find a way.
#

HA & Jinja noob needs help with a simple template and jinja reuse

#

BTW I am also a discord noob ๐Ÿ™‚ I mostly use Slack at work.

#

<@&330946878646517761> looks like people don't use threads much. Should I post the question as a normal message in the channel?

narrow tree
#

you should not tag all mods, that's for sure

livid escarp
#

copy that

cinder ore
#

Where is the ๐Ÿ”ฅ ?

#

๐Ÿš’

sour swan
#

@livid escarp Who's dying?

#

Is it you?

livid escarp
#

Sorry guys no fire here.

flat spindle
livid escarp
#

I am new guy here

sour swan
#

And utterly failed at step #1

livid escarp
#

appearing to raising false alrams

sour swan
#

On no Discord server has it ever been a good idea to tag all mods for a question

livid escarp
#

So appologies

flat spindle
#

One thing is for sure. Definitely won't be doing it again ๐Ÿ˜›

livid escarp
#

sure sure

livid escarp
azure relic
#

hello

livid escarp
#

hello, I am little terrified here

#

Going thru #rules again ๐Ÿ˜ฌ

nocturne quartz
#

Welcome. Not the worst start I've seen. Far from the best, but not the worst.

livid escarp
#

thankyou!

nocturne quartz
#

And yes. It's probably best to post directly in the main channel. As you say people don't use threads much. Mainly because they're badly implementen IMO.