I currently have some ESPHome relays that turn on/off some solar controllers based on whether the sun is above the horizon or below it.
I now want to create a better automation (that maybe requires creating a sensor or two?) that turns the relays on/off based on a numeric score ( as well as if the sun is above the horizon). I’m new to Home Assistant and definitely need help, especially with creating sensors/templates.
The step I’m stuck at currently is having Home Assistant check the current state of a weather API sensor (such as AccuWeather weather.home
) and then having it convert that state into a number --so that I can create an over-all number score for that weather sensor’s current state.
After accomplishing that, I’d also like to create a sensor that adds up an aggregate/total score of all of the individual weather APIs scores so that I can use an average of multiple weather APIs to make the determination.
Here’s what I’m looking to do …
For each weather API I would want to create a unique sensor, for example weather_score_accuweather
could be the name and it would pull from weather.home
Accuweather API sensor. Then weather_score_nws
could be another, and so on.
If the current state is:
Unknown = 0
Rainy = 0
Cloudy = 0
Partly Cloudy = 1
Sunny = 2
Exceptional = 3
There are additional weather states that would need to have numbers assigned to them, but the ones listed above are most important to get started.
I have tried creating a Template Sensor and adding this to the “State template*” section but it doesn’t seem to work. The resulting value is always “0” and never changes (even when I temporarily assign unknown
a -1 value for testing purposes).
{% set weather_state = states('weather.home') %}
{% if weather_state == 'Cloudy' %}
0
{% elif weather_state == 'Partly cloudy' %}
1
{% elif weather_state == 'Sunny' %}
2
{% elif weather_state == 'Exceptional' %}
3
{% else %}
0
{% endif %}
I used the Helpers Gui to try this. There additional settings that I can enter such as “unit of measurement” but I’m not yet sure what I should enter.
After the individual weather score sensors are created and working, I think that I would need a final “all” sensor to pull the numbers from each individual weather_score sensor (for example, it could be called weather_score_all) to add up a final score.
From there I’d like to create (I’m guessing again) two automations to determine the on/off relay state by a time_pattern
trigger : If “weather_score_all” sensor is above or below a certain amount, the relay switch will respond accordingly. It would go something like this (I think?) :
For the “ON” function:
alias: weather_score_relay_on_automation
description: ""
trigger:
- platform: time_pattern
minutes: /5
condition:
- condition: numeric_state
entity_id: sensor.weather_score_accuweather
above: "0"
- condition: state
entity_id: sun.sun
state: above_horizon
action:
- type: turn_on
device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
entity_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
domain: switch
mode: single
For “OFF” function
description: ""
trigger:
- platform: time_pattern
minutes: /5
condition:
- condition: numeric_state
entity_id: sensor.weather_score_accuweather
below: "1"
- condition: state
entity_id: sun.sun
state: above_horizon
action:
- type: turn_off
device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
entity_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
domain: switch
mode: single
I would probably make the score card values and results a little more complex and weighted after I get a basic version of this running.
But any thoughts or help to get me started would be greatly appreciated.
Thank you!
UPDATE /EDIT :
Turns out I had two issues and the solutions came from two users, Armedad (fixing my template to use set weather_enum =
) and Code-In-Progress (fixing the weather states to the proper unix-style / backend naming vs. the friendly names which got from the sensor’s card on my dashboard). See below …