The EPIC Time Conversion and Manipulation Thread!

ec4_remaining_time:
      unique_id: ec4_remaining_time
      friendly_name: "Remaining Time"
      value_template: |-
        {% set x = states.sensor.citroen_ec4.attributes["energy"][0]["charging"]["remaining_time"] %}
        {% if x not in ['None', 'unknown', 'unavailable'] %}
          {{ (x|as_timedelta).total_seconds() }}
        {% else %}
          {{x}}
        {% endif %}
      availability: {{ states.sensor.citroen_ec4.attributes["energy"][0]["charging"]["remaining_time"] | as_timedelta is not none }}
      device_class: duration
      unit_of_measurement: s

Is that a template sensor? That doesn’t look like a rest sensor. Anyways, when you put that into the UI, the UI should translate it to HH:MM:SS. Try refreshing the page when viewing the entity in the entities card.

Well, you’re right, there is one rest sensor containing many aspects, which are then split out via various template sensors.
Please find an excerpt below, I replaced the real VIN by <<VIN>>

# Citroen ec4
- platform: rest
  name: citroen_ec4
  unique_id: citroen_ec4
  resource: http://172.30.32.1:5000/get_vehicleinfo/<<VIN>>?from_cache=1
  scan_interval: 60
  timeout: 30
  value_template: "OK"
  json_attributes:
    - energy
    - timed_odometer
    - battery
- platform: template
  sensors:
    ec4_battery_voltage:
      unique_id: ec4_battery_voltage
      friendly_name: "Battery Voltage"
      unit_of_measurement: "V"
      device_class: voltage
      value_template: '{{ states.sensor.citroen_ec4.attributes["battery"]["voltage"] * 4 }}'
    ec4_battery_level:
      unique_id: ec4_battery_level
      friendly_name: "Battery"
      unit_of_measurement: "%"
      device_class: battery
      value_template: '{{ states.sensor.citroen_ec4.attributes["energy"][0]["level"] | round(0) }}'
    ec4_charging_status:
      unique_id: ec4_charging_status
      friendly_name: "Charging Status"
      value_template: '{{ states.sensor.citroen_ec4.attributes["energy"][0]["charging"]["status"] }}'
    ec4_remaining_time:
      unique_id: ec4_remaining_time
      friendly_name: "Remaining Time"
      value_template: |-
        {% set x = states.sensor.citroen_ec4.attributes["energy"][0]["charging"]["remaining_time"] %}
        {% if x not in ['None', 'unknown', 'unavailable'] %}
          {{ (x|as_timedelta).total_seconds() }}
        {% else %}
          {{x}}
        {% endif %}
      availability: {{ states.sensor.citroen_ec4.attributes["energy"][0]["charging"]["remaining_time"] | as_timedelta is not none }}
      device_class: duration
      unit_of_measurement: s

Is there a reason you aren’t using the rest integration to do all of this?

rest:
- resource: http://172.30.32.1:5000/get_vehicleinfo/<<VIN>>?from_cache=1
  scan_interval: 60
  timeout: 30
  sensor:
    - unique_id: ec4_battery_voltage
      name: EC4 Battery Voltage
      unit_of_measurement: V
      device_class: voltage
      value_template: '{{ value_json.battery.voltage * 4 }}'
      
    - unique_id: ec4_battery_level
      name: EC4 Battery Level
      unit_of_measurement: %
      device_class: battery
      value_template: '{{ value_json.energy[0].level | round(0) }}'
  
    - unique_id: ec4_charging_status
      name: EC4 Charging Status
      value_template: '{{ value_json.energy[0].charging.status }}'
      
    - unique_id: ec4_remaining_time
      name: EC4 Remaining Time
      value_template: '{{ (value_json.energy[0].charging.remaining_time | as_timedelta).total_seconds() }}'
      availability: '{{ value_json.energy[0].charging.remaining_time | as_timedelta is not none }}'
      device_class: duration
      unit_of_measurement: s

Secondly, the template you are using:

will return an invalid state for duration sensors. Making it not work, you cannot use that template. If you really want to stay with your current rest/template entity method, your value_template should be:

{{ (states.sensor.citroen_ec4.attributes["energy"][0]["charging"] | as_timedelta).total_seconds() }}

Thanks a lot.
and no, there is no reason for staying with my former approach. Will replace as you’ve suggested and report the result here.

EDIT

tried your proposal, and get an error message when verifiying the yaml:

Invalid config for 'rest' from integration 'sensor' at sensors.yaml, line 129: 'sensor' is an invalid option for 'sensor.rest', check: sensor

it does not go in your sensor.yaml file. It goes in configuration.yaml. It’s a separate integration from sensor.

Now I remember why I was using template sensors. because I could change the icon of the template sensor, even in a dynamic manner depending on the value of the sensor.

For example

    ec4_charging_plugged:
      unique_id: ec4_charging_plugged
      friendly_name: "Charging Plugged"
      value_template: '{{ states.sensor.citroen_ec4.attributes["energy"][0]["charging"]["plugged"] }}'
      icon_template: >-
        mdi:power-plug{{ iif(is_state('switch.ec4_charging_plugged', 'True'),'', '-off') }}-outline

till now I couldn’t figure out how to do that with rest sensors.

How does this one get the attribute remaining_time?

You can just make that a binary sensor instead though with a device class.

I have no idea, you haven’t shared the data from the rest end point.