Automation of OpenUV in HomeAssistant
What started as a weekend experiment now that the sun is out turned into a bit of a headache.
After I checked my OpenUV automations from last year I realized that something was not working as intended.
First thing I noticed was that the UV Index card installed from HACS was not displaying any info, now I've discovered in the past that there are a certain amount of calls for the OpenUV api calls to get the info you want.
Fixed Duplicate Sensors
While poking around I've realized I've turned off OpenUV integration and created a new one.
This lead to some duplicated sensors, but this was a quick fix. Once I turned on the original integration my guess is that HomeAssistant know that the other integration is the same and removes it completely
Some entities needed to have their .yaml code revied and just remove the _2 from those entities and all was good.
sensor.openuv_current_uv_index_2
change to
sensor.openuv_current_uv_index
Limiting the Sensors to Avoid API Abuse
Each monitored condition = one API call. So I edited my configuration to only keep what I actually needed.
If math is correct we should not exceed the 50 calls per day, tomorrow I'll need to check after work to see if its all working as intended.
alias: Update OpenUV Every 30 Min (Daylight Only)
description: Safely updates OpenUV data using limited API calls
mode: single
triggers:
- minutes: /30
trigger: time_pattern
conditions:
- condition: sun
after: sunrise
- condition: sun
before: sunset
actions:
- target:
entity_id:
- sensor.openuv_current_uv_index
- sensor.openuv_current_uv_level
- sensor.openuv_current_ozone_level
- sensor.openuv_max_uv_index
action: homeassistant.update_entity
data: {}
Card for Dashboard
Addressing the card in the dashboard I've changed the code around a little to expose uv index at the moment, max uv for the day and skin types exposure.
type: vertical-stack
cards:
- type: custom:uv-index-card
entity: sensor.openuv_current_uv_index
- type: entities
title: Safe Exposure Times
show_header_toggle: false
entities:
- entity: sensor.openuv_max_uv_index
name: Max UV index
- entity: sensor.openuv_skin_type_1_safe_exposure_time
name: Skin type 1
- entity: sensor.openuv_skin_type_2_safe_exposure_time
name: Skin type 2
- entity: sensor.openuv_skin_type_3_safe_exposure_time
name: Skin type 3
- entity: sensor.openuv_skin_type_4_safe_exposure_time
name: Skin type 4
- entity: sensor.openuv_skin_type_5_safe_exposure_time
name: Skin type 5
- entity: sensor.openuv_skin_type_6_safe_exposure_time
name: Skin type 6
Automations
I've figured that the Automations from last year were gone so time to dig in.
I've realized many notifications can be annoying for the family so I've broken it down in two notifications for now per day. One is to start at 10am and later another if the max index changes and the user needs an heads up.
UV Risk – Morning Forecast (10:00 AM)
alias: UV Risk – Morning Forecast (10:00 AM)
description: Notifies if UV max is dangerous today
triggers:
- at: "10:00:00"
trigger: time
conditions:
- condition: sun
after: sunrise
- condition: numeric_state
entity_id: sensor.openuv_max_uv_index
above: 2
actions:
- repeat:
for_each:
- notify.mobile_app_hanas_iphone
- notify.mobile_app_pinaiphone
sequence:
- data:
title: ☀️ Sun Exposure Risk Today
message: >
The UV Index today may reach {{
states('sensor.openuv_max_uv_index') }}. {% set uv =
states('sensor.openuv_max_uv_index') | float %} {% if uv <= 2 %}
No risk. Enjoy the sun safely.
{% elif uv <= 5 %}
Moderate UV. Use sunscreen and sunglasses.
{% elif uv <= 7 %}
High UV! Apply SPF 30+, wear a hat.
{% elif uv <= 10 %}
Very high UV. Limit sun exposure between 10–16h.
{% else %}
EXTREME UV! Avoid prolonged exposure and wear full protection.
{% endif %}
action: "{{ repeat.item }}"
UV Spike Alert – Live Detection
alias: UV Spike Alert – Live Detection
description: Notifies if UV spikes past 6 suddenly
mode: parallel
triggers:
- entity_id: sensor.openuv_current_uv_index
above: 6
trigger: numeric_state
conditions:
- condition: sun
after: sunrise
- condition: sun
before: sunset
- condition: template
value_template: >
{{ (as_timestamp(now()) -
as_timestamp(state_attr('automation.uv_spike_alert_live',
'last_triggered') | default(0))) > 10800 }}
actions:
- repeat:
for_each:
- notify.mobile_app_hanas_iphone
- notify.mobile_app_pinaiphone
sequence:
- data:
title: 🔆 UV Index is now HIGH!
message: >
UV Index just reached {{ states('sensor.openuv_current_uv_index')
}}. Protect your skin — reapply sunscreen and wear a hat.
action: "{{ repeat.item }}"