What the option for driving state?

breaking my head how i can tell if some1 is driving :

basically the options i tried:
life360 - there’s driving attributes in the device_tracker (see below - didn’t work).
from mobile HA app - there’s no info for speed/driving state.
from the life360 i tried below didn’t work :


  - platform: template
    sensors:
      avi_driving_speed:
        friendly_name: "Avi driving speed"
        unit_of_measurement: 'Km/h'
        value_template: >
          {%   if     states.device_tracker.avi.attributes.driving     %} 
           {{ states.device_tracker.avi.attributes.speed }}
          {% else %}
             0
          {% endif %}
        icon_template: >
          {%   if     states.device_tracker.avi.attributes.speed  | int   < 90 %} 
            mdi:gauge-low
          {% else %}
            mdi:gauge-high
          {% endif %}

also:


binary_sensor:


  - platform: template
    sensors:
      avi_karin_driving:
        friendly_name: 'Avi is driving'
        value_template: >-
          {{ states.device_tracker.avi.attributes.driving }}

any idea?

try:

value_template: >
  {% if is_state_attr('device_tracker.avi', 'driving', 'true')     %} 
    {{ state_attr('device_tracker.avi', 'speed') }}
  {% else %}
    0
  {% endif %}
icon_template: >
  {% if state_attr('device_tracker.avi', 'speed')  | int   < 90 %} 
    mdi:gauge-low
  {% else %}
    mdi:gauge-high
  {% endif %}

I’m not sure why the binary sensor isn’t working.

Can you be a bit more specific about what you mean by “didn’t work”? You got errors? The state of the template sensor didn’t respond as you expected? What did it do or not do that you thought it should?

Have you set the driving speed threshold for the Life360 integration?

Have you checked the driving attribute of device_tracker.avi to see if it is changing as you expect?

As @finity suggests, you need to use the is_state_attr & state_attr functions to guard against unexpected states. Here is what I’d suggest:

sensor:
  - platform: template
    sensors:
      avi_driving_speed:
        friendly_name: "Avi driving speed"
        unit_of_measurement: 'Km/h'
        value_template: >

          {# Use is_state_attr in case device_tracker.avi does not exist, #}
          {# or its driving attribute is null. #}

          {% if is_state_attr('device_tracker.avi', 'driving', true) %}

            {# state_attr not needed here because if above is true, #}
            {# then this exists. But use float (or int) filter with #}
            {# default in case its value is null. #}

            {{ states.device_tracker.avi.attributes.speed | float(0) }}
          {% else %}
             0
          {% endif %}
        icon_template: >

          {# Use state_attr & float filter with default to guard against #}
          {# the entity not existing or its speed attribute being null. #}

          {% if state_attr('device_tracker.avi', 'speed') | float(0) < 90 %}
            mdi:gauge-low
          {% else %}
            mdi:gauge-high
          {% endif %}
binary_sensor:
  - platform: template
    sensors:
      avi_karin_driving:
        friendly_name: 'Avi is driving'
        value_template: >

          {# Use is_state_attr in case device_tracker.avi does not exist, #}
          {# or its driving attribute is null. #}

          {{ is_state_attr('device_tracker.avi', 'driving', true) }}