Having an issue creating a template sensor

Hi there,

I’m trying to create a sensor for my car’s 'distance until service required". I’ve successfully added the Jaguar integration and can see all of the entities. I’ve run the templating tool and verified that my ‘state’ line works and correctly pulls the distance, but for some reason the sensor is not created. I’m sure I’m doing something stupid… can anyone help?

Thanks

- platform: template
  sensor:
    - name: "Jag Distance to Service"
      icon: "mdi:wrench"
      state: "{{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService }}"

You have mixed the current format with the legacy format. Either format will work, but you cannot mix them.

In the current format your sensor should look like:

template:
  - sensor:
      - name: "Jag Distance to Service"
        icon: "mdi:wrench"
        state: "{{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService }}"
        unit_of_measurement: km
        state_class: measurement
        availability: "{{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService | float('unavailable') is number}}"

EDIT

The legacy format would be:

- platform: template
  sensors: 
    - friendly_name: "Jag Distance to Service"
      icon: "mdi:wrench"
      value_template: >
        {{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService }}
      unit_of_measurement: km
      state_class: measurement
      availability: >
        {{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService | float('unavailable') is number}}

Thanks. Made the changes as follows but still not showing up.


- platform: template
  sensor:
    - name: "Jag Distance to Service"
      icon: "mdi:wrench"
      state: "{{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService }}"
      unit_of_measurement: km
      state_class: measurement
      availability: "{{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService is number}}"

Because you’re still mixing legacy format with modern format. Compare the first two lines of the posted example (modern format) with your version (legacy format).

Thanks for the reply. I’ve updated and was able to create the sensor by moving it into configuration.yaml. I couldn’t quite figure out the format to put it in my split sensors.yaml file.

Also, the value is showing as ‘unavailable’ even though pasting

{{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService }}

in the templating tool returns the correct value. Thoughts?

`

The value may need to change before it updates. Go for a drive?

I guess that’s possible, but why would the template tool return a value and the actual sensor not?

Also, any idea how I implement that in my sensors.yaml file? I’d like to not have those in my configuration.yaml.

Thanks

By defining it exclusively in legacy format.

Did you check your availability clause. You may need to add a | int to cast it to a number.

Usually the sensors.yaml file sets a top level key of sensor: so it is not compatible with the current method which needs that to be template:. As Taras commented above you will need to use the legacy format if you want it to be in that file. Optionally, you could use the current format and keep you configuration.yaml file tidy by setting up a templates.yaml file and !include-ing it in your config.

As Pete’s comment alludes, the attribute might be coming through as a string so you may need to cast it to an integer or float to have availability work correctly.

- platform: template
  sensors: 
    - friendly_name: "Jag Distance to Service"
      icon: "mdi:wrench"
      value_template: >
        {{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService }}
      unit_of_measurement: km
      state_class: measurement
      availability: >
        {{ state_attr('sensor.jag_all_info', 'core status').extKilometersToService | float('unavailable') is number}}

Thanks for all the suggestions and info. I’ll try the cast tonight.

I’m completely lost in the changes that are made to the template integration. I don’t understand why it doesn’t show up.

What I have is a simpel ping as a binary_sensor:

  - platform: ping
    host: 8.8.8.8
    count: 3
    name: "google.com"
    scan_interval: 30

and this template in the new format added to the configuration.yaml:

template:
  - sensor:
    - name: "Google ping"
      state_class: connectivity
      unit_of_measurement: "ms"
      icon_template: "mdi:speedometer"
      state: "{{ state_attr('binary_sensor.google_com', 'round_trip_time_avg')|round(2) }}"

The ping is working correctly, but the “Google ping” template doesn’t show up.
What am I doing wrong?

“connectivity” is not a valid state class… it is a valid device class, but only for binary sensors.

template:
  - sensor:
      - name: Google ping
        unit_of_measurement: ms
        state_class: measurement
        icon: mdi:speedometer
        state: "{{ state_attr('binary_sensor.google_com', 'round_trip_time_avg')|round(2, 0) }}"
        availability: "{{ states('binary_sensor.google_com') is not none }}"

YES! Its working now. Many thanks Drew!