various tidyups

This commit is contained in:
zorruno
2026-08-15 14:58:19 +12:00
parent c85ab28c10
commit dbb0a6a89c
30 changed files with 2977 additions and 684 deletions
+265
View File
@@ -0,0 +1,265 @@
# Capped Cloudflare throughput tests: 2 MB download and 500 kB upload per run.
command_line:
- sensor:
name: "Internet Speed Test"
unique_id: internet_speed_test
command: "/config/scripts/internet_speed_test.sh"
command_timeout: 120
scan_interval: 31536000
unit_of_measurement: "Mbit/s"
device_class: data_rate
state_class: measurement
value_template: "{{ value_json.download_mbps }}"
json_attributes:
- upload_mbps
- binary_sensor:
name: "Internet Connected"
unique_id: internet_connected
command: >-
curl --fail --silent --show-error --max-time 10 --output /dev/null
https://one.one.one.one/cdn-cgi/trace && printf "ON" || printf "OFF"
command_timeout: 15
scan_interval: 60
payload_on: "ON"
payload_off: "OFF"
device_class: connectivity
input_number:
internet_speed_tests_today:
name: "Internet Speed Tests Today"
min: 0
max: 100
step: 1
mode: box
internet_speed_tests_this_month:
name: "Internet Speed Tests This Month"
min: 0
max: 2000
step: 1
mode: box
internet_download_today_average:
name: "Internet Download Today Average"
min: 0
max: 10000
step: 0.01
mode: box
internet_upload_today_average:
name: "Internet Upload Today Average"
min: 0
max: 10000
step: 0.01
mode: box
internet_download_month_average:
name: "Internet Download Month Average"
min: 0
max: 10000
step: 0.01
mode: box
internet_upload_month_average:
name: "Internet Upload Month Average"
min: 0
max: 10000
step: 0.01
mode: box
input_boolean:
internet_speed_test_running:
name: "Internet Speed Test Running"
script:
internet_run_speed_test:
alias: "Internet: Run speed test"
sequence:
- action: input_boolean.turn_on
target:
entity_id: input_boolean.internet_speed_test_running
- variables:
test_started: "{{ as_timestamp(now()) }}"
- action: homeassistant.update_entity
target:
entity_id: sensor.internet_speed_test
# Ignore the command-line sensor's interim unavailable state.
- wait_template: >-
{{
has_value('sensor.internet_speed_test')
and as_timestamp(states.sensor.internet_speed_test.last_updated, 0) > test_started
}}
timeout:
minutes: 2
continue_on_timeout: true
- delay:
seconds: 3
- action: input_boolean.turn_off
target:
entity_id: input_boolean.internet_speed_test_running
mode: single
template:
- sensor:
- name: "Internet Upload Speed Test"
unique_id: internet_upload_speed_test
unit_of_measurement: "Mbit/s"
device_class: data_rate
state_class: measurement
availability: "{{ state_attr('sensor.internet_speed_test', 'upload_mbps') is number }}"
state: "{{ state_attr('sensor.internet_speed_test', 'upload_mbps') | float }}"
- name: "Internet Last Speed Display"
unique_id: internet_last_speed_display
state: >-
{% if is_state('input_boolean.internet_speed_test_running', 'on') %}
Testing
{% elif has_value('sensor.internet_speed_test') and has_value('sensor.internet_upload_speed_test') %}
{{ states('sensor.internet_speed_test') | float(0) | round(0) | int }}/{{ states('sensor.internet_upload_speed_test') | float(0) | round(0) | int }}
{% else %}
--
{% endif %}
- name: "Internet Download Average Today"
unique_id: internet_download_average_today
unit_of_measurement: "Mbit/s"
device_class: data_rate
state_class: measurement
availability: "{{ states('input_number.internet_speed_tests_today') | int(0) > 0 }}"
state: "{{ states('input_number.internet_download_today_average') | float(0) }}"
- name: "Internet Upload Average Today"
unique_id: internet_upload_average_today
unit_of_measurement: "Mbit/s"
device_class: data_rate
state_class: measurement
availability: "{{ states('input_number.internet_speed_tests_today') | int(0) > 0 }}"
state: "{{ states('input_number.internet_upload_today_average') | float(0) }}"
- name: "Internet Download Average This Month"
unique_id: internet_download_average_this_month
unit_of_measurement: "Mbit/s"
device_class: data_rate
state_class: measurement
availability: "{{ states('input_number.internet_speed_tests_this_month') | int(0) > 0 }}"
state: "{{ states('input_number.internet_download_month_average') | float(0) }}"
- name: "Internet Upload Average This Month"
unique_id: internet_upload_average_this_month
unit_of_measurement: "Mbit/s"
device_class: data_rate
state_class: measurement
availability: "{{ states('input_number.internet_speed_tests_this_month') | int(0) > 0 }}"
state: "{{ states('input_number.internet_upload_month_average') | float(0) }}"
sensor:
- platform: statistics
name: "Internet Download Average Last 7 Days"
unique_id: internet_download_average_last_7_days
entity_id: sensor.internet_speed_test
state_characteristic: mean
sampling_size: 400
max_age:
days: 7
- platform: statistics
name: "Internet Upload Average Last 7 Days"
unique_id: internet_upload_average_last_7_days
entity_id: sensor.internet_upload_speed_test
state_characteristic: mean
sampling_size: 400
max_age:
days: 7
automation:
- id: internet_run_speed_test
alias: "Internet: Run capped speed test"
description: "Runs at a random time between five minutes before and after each half-hour."
triggers:
- trigger: time_pattern
minutes: 25
- trigger: time_pattern
minutes: 55
conditions: []
actions:
- delay:
seconds: "{{ range(0, 601) | random }}"
- action: script.internet_run_speed_test
target:
entity_id: sensor.internet_speed_test
mode: single
- id: internet_record_speed_test
alias: "Internet: Record speed test averages"
description: "Records every successful speed test, including the startup test."
triggers:
- trigger: state
entity_id: sensor.internet_speed_test
conditions: []
actions:
# Allow the upload template sensor to update from the command-line result.
- delay:
seconds: 1
- condition: template
value_template: "{{ has_value('sensor.internet_speed_test') and has_value('sensor.internet_upload_speed_test') }}"
- action: input_number.set_value
target:
entity_id: input_number.internet_download_today_average
data:
value: >-
{{ ((states('input_number.internet_download_today_average') | float(0) * states('input_number.internet_speed_tests_today') | int(0)) + states('sensor.internet_speed_test') | float(0)) / (states('input_number.internet_speed_tests_today') | int(0) + 1) }}
- action: input_number.set_value
target:
entity_id: input_number.internet_upload_today_average
data:
value: >-
{{ ((states('input_number.internet_upload_today_average') | float(0) * states('input_number.internet_speed_tests_today') | int(0)) + states('sensor.internet_upload_speed_test') | float(0)) / (states('input_number.internet_speed_tests_today') | int(0) + 1) }}
- action: input_number.set_value
target:
entity_id: input_number.internet_download_month_average
data:
value: >-
{{ ((states('input_number.internet_download_month_average') | float(0) * states('input_number.internet_speed_tests_this_month') | int(0)) + states('sensor.internet_speed_test') | float(0)) / (states('input_number.internet_speed_tests_this_month') | int(0) + 1) }}
- action: input_number.set_value
target:
entity_id: input_number.internet_upload_month_average
data:
value: >-
{{ ((states('input_number.internet_upload_month_average') | float(0) * states('input_number.internet_speed_tests_this_month') | int(0)) + states('sensor.internet_upload_speed_test') | float(0)) / (states('input_number.internet_speed_tests_this_month') | int(0) + 1) }}
- action: input_number.set_value
target:
entity_id: input_number.internet_speed_tests_today
data:
value: "{{ states('input_number.internet_speed_tests_today') | int(0) + 1 }}"
- action: input_number.set_value
target:
entity_id: input_number.internet_speed_tests_this_month
data:
value: "{{ states('input_number.internet_speed_tests_this_month') | int(0) + 1 }}"
mode: queued
- id: internet_reset_daily_speed_averages
alias: "Internet: Reset daily speed averages"
triggers:
- trigger: time
at: "00:00:00"
conditions: []
actions:
- action: input_number.set_value
target:
entity_id:
- input_number.internet_speed_tests_today
- input_number.internet_download_today_average
- input_number.internet_upload_today_average
data:
value: 0
mode: single
- id: internet_reset_monthly_speed_averages
alias: "Internet: Reset monthly speed averages"
triggers:
- trigger: time
at: "00:00:00"
conditions:
- condition: template
value_template: "{{ now().day == 1 }}"
actions:
- action: input_number.set_value
target:
entity_id:
- input_number.internet_speed_tests_this_month
- input_number.internet_download_month_average
- input_number.internet_upload_month_average
data:
value: 0
mode: single