Hi,
I currently have a sensor with the following attributes taken from the WeCharge HACS integration:
CurrentSOCPct: 79
CruisingRangeElectricKm: 275
RemainingChargingTimeToCompleteMin: 0
ChargingState: readyForCharging
ChargeMode: manual
ChargePowerKW: 0
ChargeRateKmph: 0
MaxChargeCurrentAC: maximum
AutoUnlockPlugWhenCharged: ‘off’
TargetSOCPct: 80
PreferredChargeMode: manual
PlugConnectionState: disconnected
PlugLockState: unlocked
RemainingClimatisationTimeMin: 0
ClimatisationState: ‘off’
TargetTemperatureC: 22
TargetTemperatureF: 72
UnitInCar: celsius
ClimatisationWithoutExternalPower: true
ClimatizationAtUnlock: false
WindowHeatingEnabled: false
ZoneFrontLeftEnabled: false
ZoneFrontRightEnabled: false
TargetTemperatureK: 295.15
TimeInCar: ‘2021-12-17T15:07:21Z’
CarType: electric
TotalRangeKm: 275
unit_of_measurement: ‘%’
device_class: battery
friendly_name: State of charge
What i want to do is take the items in BOLD and convert them into different units of measuerment, as follows:
ChargeRateKmph: 0 > change into Mph (kmph * 0.62137)
TotalRangeKm: 275 > chnage into Miles (km * 0.62137)
How can i create a template sensor that takes these Kmph values and convert them into Mph?
Thanks.
You didn’t share your entity_id… but this is the basic setup.
template:
- sensor:
- name: "WeCharge Charge Rate"
unit_of_measurement: "mph"
state: >
{{ state_attr('YOUR_ENTITY_HERE', 'ChargeRateKmph')|float(0) * 0.62137 }}
availability: >
{{ state_attr('YOUR_ENTITY_HERE', 'ChargeRateKmph') | is_number }}
- name: "WeCharge Total Range"
unit_of_measurement: "miles"
state: >
{{ state_attr('YOUR_ENTITY_HERE', 'TotalRangeKm')|float(0) * 0.62137 }}
availability:
{{ state_attr('YOUR_ENTITY_HERE', 'TotalRangeKm') | is_number}}
You could also add a state_class: measurement
to each entry if you want to be able to use these for long-term statistics.
1 Like
Didgeridrew:
'ChargeRateKmph'
Thanks, this is what i have but it is throwing an error:
- platform: template
sensors:
sensor.xvxvxvxvxvxvx_soc_chargeratemph:
friendly_name: VW Charge Rate Mph
unique_id: charge_rate_mph
unit_of_measurement: "mph"
value_template: "{{ (states('sensor.xvxvxvxvxvxvx_soc', 'ChargeRateKmph) | float * 0.62137 }}"
Any ideas?
You’re missing a closing single quote after
vantech2020:
'ChargeRateKmph
and you have to have the default for you float… float(0)
(This is required as of 2021.12)
Still thorowng an error? Hmm…
- platform: template
sensors:
sensor.xvxvxvxv_soc_chargeratemph:
friendly_name: VW ID4 Charge Rate Mph
unique_id: charge_rate_mph
unit_of_measurement: "mph"
value_template: "{{ (states('sensor.xvxvxvxv_soc', 'ChargeRateKmph') | float(0) * 0.62137 }}"
Same, still an error
- platform: template
sensors:
sensor.xvxvxvxv_soc_chargeratemph:
friendly_name: VW ID4 Charge Rate Mph
unique_id: charge_rate_mph
unit_of_measurement: "mph"
value_template: "{{ (state_attr('sensor.xvxvxvxv_soc', 'ChargeRateKmph') | float(0) * 0.62137 }}"
123
(Taras)
December 17, 2021, 4:35pm
9
vantech2020:
Same, still an error
Due to incorrect indentation.
I recommend you review the following two sections because you don’t appear to understand how to fix even the most basic errors without assistance.
When you get errors, share the error messages.
Without seeing the error message… you’re likely getting an error due to the sensor.
that you have added to the entity name:
sensor.xvxvxvxv_soc_chargeratemph
should be xvxvxvxv_soc_chargeratemph
And your indents are off…
- platform: template
sensors:
xvxvxvxv_soc_chargeratemph:
friendly_name: VW ID4 Charge Rate Mph
unique_id: charge_rate_mph
unit_of_measurement: "mph"
value_template: "{{ state_attr('sensor.xvxvxvxv_soc', 'ChargeRateKmph') | float(0) * 0.62137 }}"
Also, you are using the legacy format. As noted explicitly in the link I gave in my first post,
this format still works but is no longer recommended.
You should be using the current format as I did in my example.
Templating is not my strong point, will check that out
Here is what i have, and indents are aligned to other templates that work correctly:
- platform: template
sensors:
wvgzzz_soc_chargeratemph:
friendly_name: Charge Rate Mph
unique_id: charge_rate_mph
unit_of_measurement: "mph"
value_template: "{{ (state_attr('sensor.wvgzzz_soc', 'ChargeRateKmph') | float(0) * 0.62137 }}"
And here is the error message:
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['sensors']['wvgzzz_soc_chargeratemph']['value_template']. Got "{{ (state_attr('sensor.wvgzzz_soc', 'ChargeRateKmph') | float(0) * 0.62137 }}". (See ?, line ?).
That error is telling you that you have an opening parenthesis (
without a closing parenthesis )
. You need to either delete the (
before state_attr
or add a )
after float(0).
1 Like
finity
December 17, 2021, 4:53pm
14
It’s telling you what the problem is…
You are missing a ‘)’ at the end of your “(state_attr(…)…”
And tbh you don’t need the first “(” so just remove it.
Edit: …didgeridrew beat me to it…
1 Like
Got it working, thanks very much!
Here is my solution:
- platform: template
sensors:
wvgzzz_soc_chargeratemph:
device_class: battery
friendly_name: Charge Rate Mph
unique_id: charge_rate_mph
unit_of_measurement: "mph"
value_template: "{{ state_attr('sensor.wvgzzz_soc', 'ChargeRateKmph') | float(0) * 0.62137 }}"
Thanks again and finity
1 Like