Graph Ping round trip average attribute

I have a Ping Binary Sensor set up like follows to ping Google so I can keep a track of my internet connection;

binary_sensor:
  - platform: ping
    host: 8.8.8.8
    count: 4
    scan_interval: 60
    name: Ping Google DNS

This sensor exposes some attributes that I would like to display on a history graph. So, I tried to set up a Template Sensor like follows to give me something to graph;

- platform: template
  sensors:
    ping_google_average:
      value_template: {{ states.binary_sensor.ping_google_dns.attributes.round_trip_time_avg }}
      unit_of_measurement: ms

But I get the following error;

Error loading /config/configuration.yaml: invalid key: “OrderedDict([(‘states.binary_sensor.ping_google_dns.attributes.round_trip_time_avg’, None)])”
in “/config/configuration.yaml”, line 97, column 0

Does anyone know how to achieve this graph? Or what I’m doing wrong?

Thanks

It’s a single line template it needs to go in quotes:

value_template: "{{ states.binary_sensor.ping_google_dns.attributes.round_trip_time_avg }}"

(Rule 3):https://www.home-assistant.io/docs/automation/templating/#important-template-rules

Edit: best practice to use this format too:

value_template: "{{ state_attr('states.binary_sensor.ping_google_dns', 'round_trip_time_avg') }}"
1 Like

If anyone else comes here, the example from tom_l is not correct (or no longer works in 0.107.5) The correct value_template syntax for state_attr() should not include the states. prefix. The correct syntax is:

value_template: "{{ state_attr('binary_sensor.ping_google_dns', 'round_trip_time_avg') }}"

Yeah sorry I forgot to remove that when copy and pasting you sensor.

I just found this thread while trying to create a statistics graph for a ping sensor. I attempted to do this by writing the suggested code into configuration.yaml, but when I restarted HA, I got a ‘repair’ alert in the UI stating:

The Ping YAML configuration is being removed
This stops working in version 2024.6.0. Please address before upgrading.
Configuring Ping using YAML is being removed.

Your existing YAML configuration has been imported into the UI automatically.

Remove the ping configuration from your configuration.yaml file and restart Home Assistant to fix this issue.

If the Ping YAML configuration is being removed, and the ping configuration is being automatically imported into the UI, will there be any method for creating ping graphs going forward? There are no options in the UI for getting a graph of ping RTT as far as I can see.

You set the ping sensor up from the UI now.

The template sensor that exposes the round trip attribute from that as a sensor is still created in yaml. Though it too now has a new format:

Old (sensors.yaml file):

- platform: template
  sensors:
    ping_google_average:
      value_template: "{{ state_attr('binary_sensor.ping_google_dns', 'round_trip_time_avg') }}"
      unit_of_measurement: ms

New (configuration.yaml):

template:
  - sensor:
      - name: Ping Google Average
        state: "{{ state_attr('binary_sensor.ping_google_dns', 'round_trip_time_avg') }}"
        unit_of_measurement: ms