Need Help with YAML Sensor Creation

Dear All,
I am new in YAML and need to create a individual sensor/Helper for Home Assistant. The name is “PUMA_LOADING_POWER”
The Problem is that the Ford Puma is sending Loading Information also the car is not connected to the wallbox.
I have two entities from my Ford Puma:
elvehcharging which is “NOT_READY” or “IN_PROGRESS”
elvehchargingpower which is “1,00 KW”

To implement my sensor in the Lumina Energy Card the Sensor should return elvehchargingpower if the elvehcharging = “IN_PROGRESS” otherwise the sensor should return “0,00 KW” or something like that.

Can you help me creating the YAML Code?

I tried it but it is not working:
{% set charge = states(‘sensor.fordpass_wf02kxer61tm89035_elvehchargingpower’) %}

{% if states(‘sensor.fordpass_wf02kxer61tm89035_elvehcharging’) == “IN_PROGRESS” %}
{states(‘sensor.fordpass_wf02kxer61tm89035_elvehchargingpower’)}
{% endif %}

You haven’t shared any YAML… just a Jinja template. Are you setting this up using the Helper dashboard, or do you actually want to set it up in YAML? Either can work, but you need to tell us what you are trying to do for us to give accurate help.

Issues with the template itself:

  • It only has single curly brackets for the expression instead of the required double curly brackets.
  • A value is set for a variable and never used.
  • No “else” clause. How should the value be derived if the state is “NOT_READY” or unavailable?

I am using the Helper and the code is from google. I was triing to get it by myself at first. I go to Create Helper → template → sensor. And the I have the little code field in the middle.
Normaly I would use the following code:

IF sensor.fordpass_wf02kxer61tm89035_elvehcharging’) == “IN_PROGRESS”
THEN Return states(‘sensor.fordpass_wf02kxer61tm89035_elvehchargingpower’
ELSE Return “0,00 KW”

But in YAML the if then else expression is not working so I don’t know how to fix it.

Okay, so you should only need to fix the issues I listed previously and place the template in the “State” field in the Helper configuration flow:

{% set charge = states(‘sensor.fordpass_wf02kxer61tm89035_elvehchargingpower’) %}
{% if is_state('sensor.fordpass_wf02kxer61tm89035_elvehcharging','IN_PROGRESS') %}
  {{ charge }}
{% else %}
  0
{% endif %}

Try that and let us know if there are any further issues.

You may also want to add the following in the “Availability” field which can be found under “Additional Options” to avoid error messages or an “unknown” state if there is a issue with the charging power sensor:

{{ has_value(‘sensor.fordpass_wf02kxer61tm89035_elvehchargingpower’) }}

Thank you very much for the quick response!

It is working now and I think I am understanding YAML now much better!

None of that is YAML… :laughing:

If you want a primer on YAML check out YAML For Nonprogrammers.

Note that you cannot use the same kind of template for energy. If you want energy as well, there are two strategies. If your car has a KWh sensor, the most accurate way is to use a utility meter helper in combination with the tariff option. You’d use a tariff for home charging and one for the rest.

If you do not have an energy sensor for the car, you can use an integral helper sensor, set to the left Riemann method. Do not pick trapezoid, it will give wrong results.