You can also use the availability template. Found this on the HA forums and it works really well for me. - platform: template sensors: hvac_attic_delta: unit_of_measurement: "°F" value_template: >- {% set outside = states('sensor.outdoor_temperature')|float(70) %} {% set attic = states('sensor.attic_temperature')| float(70) %} {{ (attic - outside)|round(1) |float (0) }} availability_template: >- {% set items = 'sensor.outdoor_temperature', 'sensor.attic_temperature' %} {{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
Excellent tutorial. Keep up the great work. I have lots of templates and/or sensors that have undefined values (reconfigured and haven't triggered - like the lightning one) and now I have a solution.
Really appreciate you putting the time in to show us this. I have a template that gives an error and this will be an easy way to fix that so it doesn't give the error anymore.
Hi Jeff, another great video, thanks for your efforts. I recently came across template trigger sensors and found them very useful for dealing with this sort of situation. You could have a sensor only convert to celsius when the fahrenheit value is valid, otherwise keep the previous state but also have an attribute which gives the last updated time to potentially handle stale data. Something like this: template: - trigger: - platform: homeassistant event: start id: on_boot - platform: state entity: sensor.basement_air_device_temperature id: sensor_updated sensor: - name: Basement Temp Celsius unique_id: basement_temp_celsius state: | {% if has_value('sensor.basement_air_device_temperature') %} {{ ((states('sensor.basement_air_device_temperature') | int - 32) / 1.8) round(0) }} {% else %} {{ states('sensor.basement_temp_celsius') }} {% endif %} attributes: last_updated: | {% if has_value('sensor.basement_air_device_temperature') %} {{ now() }} {% else %} {{ state_attr('sensor.basement_temp_celsius', 'last_updated') }} {% endif %}
Thank you so much Jeff. This solved a problem I had for quite a long time, Haven't thought of the "default=" syntax. - platform: rest name: Carise's Birthday resource: !secret wolframalpha_carise_day_api value_template: "{{ (value|replace(' days', '')) | int (default=1) }}" unit_of_measurement: Days scan_interval: 43200 The problem I had was wolframalpha returns the unit "day" a day before the event (not "days") so HA returns an error. One quetsion. Do you know if there is a way I can make unit_of_meaurement variable? Making it "Day" if the value is 1 and "Days" if more than 1
I have several 433mhz door switches, connected through a tasmotized RF box, and my doors and windows that aren’t opened for a long time show up as unavailable, is this the same issue?
I don't think so. Unknown is typically in regards to the state not being available. Either expired or the system just booted so it hasn't gotten an recent update from the entity. But the device is still talking to home assistant and potentially sending/updating other attributes. Unavailable is typically related to the device's "heartbeat" so if Home Assistant doesn't see that device as connected or doesn't have a way to reach the device then it goes unavailable. This would be the case in an entity that is restored after reboot, but home assistant hasn't heard anything from it.
@@SlackerLabsthanks for the reply, can you point me to a direction other than going around and opening and closing all the windows and doors, or replacing the switches?
Good question. The short answer is I sucked at planning videos. Longer answer is I was going to cover that in the “next” video. Then I decided this one was needed. And I push that format change to the one after this. And that may get pushed back. But since both legacy and new format work and I have been focused more on the templates I was thinking it wasn’t critical. But we will get there in the video dedicated to building template based entities.
2023.5 has given me this template error... Automation runs fine. Error says it wont. Master Bedroom TV on off sync uses an unknown service The automation "Master Bedroom TV on off sync" (automation.master_bedroom_tv_on_off_sync) has an action that calls an unknown service: homeassistant.turn_unavailable. This error prevents the automation from running correctly. Maybe this service is no longer available, or perhaps a typo caused it. AUTOMATION TEMPLATE SERVICE: service: homeassistant.turn_{{ trigger.to_state.state }}
That looks like it was unable to get the trigger.to_state.state . It was reporting it as unavailable. I dont think any of the trigger entities changed. Maybe it's triggering by something that doesn't have a state? You could wrap that service section in the {% if has_value('trigger.to_state.state') %}... logic and see if hat helps. But it's weird that it is seeing the trigger without a to state .
@@SlackerLabs its always available, and it always works. Home assistant is falsely reporting it as an error. Reported as bug to HA. no ignore button on the "repair" notice either. oh well. no biggy
@@TristynRusselo If you feel it is falsely giving an error alert, I would suggest the forums or log an issue. Your issue is very weird. I've never had a template based around valid values give an error. If trying the tips in this video do not work, I would suggest looking at why the entity in the template has an invalid value. Is it just when yourestart that it reports unknown until it gets the first value in? Things like that are beyond UA-cam comments section though
@@EsotericArctos As I stated in my last post, i have reported it already to HA (via github bug reporting). they were no help for a workaround, thought maybe here would spark an idea. The error message is a "repair" notification. it shows on reboot and after the automation runs. The "value" it is looking for is the trigger to state (on or off). did the trigger entity turn on? or off? heres the whole thing: trigger: - platform: state entity_id: - light.master_bedroom_ceiling action: - service: homeassistant.turn_{{ trigger.to_state.state }} target: entity_id: - light.master_bedroom_tv_lightstrip
@@TristynRusselo Exclude 'unavailable' and 'unknown' from your trigger: trigger: - platform: state not_to: - unavailable - unknown entity_id: - light.master_bedroom_ceiling
Visibility of YAML editor crystal clear and content very helpful - Thanks, Jeff!
You can also use the availability template. Found this on the HA forums and it works really well for me.
- platform: template
sensors:
hvac_attic_delta:
unit_of_measurement: "°F"
value_template: >-
{% set outside = states('sensor.outdoor_temperature')|float(70) %}
{% set attic = states('sensor.attic_temperature')| float(70) %}
{{ (attic - outside)|round(1) |float (0) }}
availability_template: >-
{% set items = 'sensor.outdoor_temperature', 'sensor.attic_temperature' %}
{{ expand(items) | rejectattr('state','in',['unknown','unavailable']) | list | count == items | count }}
Yep ,the "light" mode/scheme helps this old guy see your GREAT demo's. Though, I run in "dark" mode too.
Excellent tutorial. Keep up the great work. I have lots of templates and/or sensors that have undefined values (reconfigured and haven't triggered - like the lightning one) and now I have a solution.
Really appreciate you putting the time in to show us this. I have a template that gives an error and this will be an easy way to fix that so it doesn't give the error anymore.
Thanks, appreciate being able to see the code.
Love your solution, plus never used MQTT sensors. So a cool learning curve.
Ohhhhhh, that's a great example of the use of code logic and of has_value() !!!! Goooooo JINJA ! Gooooo Slacker Labs !!!
Hi Jeff, another great video, thanks for your efforts. I recently came across template trigger sensors and found them very useful for dealing with this sort of situation. You could have a sensor only convert to celsius when the fahrenheit value is valid, otherwise keep the previous state but also have an attribute which gives the last updated time to potentially handle stale data. Something like this:
template:
- trigger:
- platform: homeassistant
event: start
id: on_boot
- platform: state
entity: sensor.basement_air_device_temperature
id: sensor_updated
sensor:
- name: Basement Temp Celsius
unique_id: basement_temp_celsius
state: |
{% if has_value('sensor.basement_air_device_temperature') %}
{{ ((states('sensor.basement_air_device_temperature') | int - 32) / 1.8) round(0) }}
{% else %}
{{ states('sensor.basement_temp_celsius') }}
{% endif %}
attributes:
last_updated: |
{% if has_value('sensor.basement_air_device_temperature') %}
{{ now() }}
{% else %}
{{ state_attr('sensor.basement_temp_celsius', 'last_updated') }}
{% endif %}
Yea. I have a whole video dedicated to trigger based template entities.
Great job and very informative!
Thank you so much Jeff. This solved a problem I had for quite a long time, Haven't thought of the "default=" syntax.
- platform: rest
name: Carise's Birthday
resource: !secret wolframalpha_carise_day_api
value_template: "{{ (value|replace(' days', '')) | int (default=1) }}"
unit_of_measurement: Days
scan_interval: 43200
The problem I had was wolframalpha returns the unit "day" a day before the event (not "days") so HA returns an error.
One quetsion. Do you know if there is a way I can make unit_of_meaurement variable? Making it "Day" if the value is 1 and "Days" if more than 1
I have several 433mhz door switches, connected through a tasmotized RF box, and my doors and windows that aren’t opened for a long time show up as unavailable, is this the same issue?
I don't think so. Unknown is typically in regards to the state not being available. Either expired or the system just booted so it hasn't gotten an recent update from the entity. But the device is still talking to home assistant and potentially sending/updating other attributes. Unavailable is typically related to the device's "heartbeat" so if Home Assistant doesn't see that device as connected or doesn't have a way to reach the device then it goes unavailable. This would be the case in an entity that is restored after reboot, but home assistant hasn't heard anything from it.
@@SlackerLabsthanks for the reply, can you point me to a direction other than going around and opening and closing all the windows and doors, or replacing the switches?
Why do you show the legecy format "-platform: template" and not the new prefered "template: - sensor:"?
Good question. The short answer is I sucked at planning videos. Longer answer is I was going to cover that in the “next” video. Then I decided this one was needed. And I push that format change to the one after this. And that may get pushed back. But since both legacy and new format work and I have been focused more on the templates I was thinking it wasn’t critical. But we will get there in the video dedicated to building template based entities.
2023.5 has given me this template error... Automation runs fine. Error says it wont.
Master Bedroom TV on off sync uses an unknown service
The automation "Master Bedroom TV on off sync" (automation.master_bedroom_tv_on_off_sync) has an action that calls an unknown service: homeassistant.turn_unavailable.
This error prevents the automation from running correctly. Maybe this service is no longer available, or perhaps a typo caused it.
AUTOMATION TEMPLATE SERVICE: service: homeassistant.turn_{{ trigger.to_state.state }}
That looks like it was unable to get the trigger.to_state.state . It was reporting it as unavailable. I dont think any of the trigger entities changed. Maybe it's triggering by something that doesn't have a state? You could wrap that service section in the {% if has_value('trigger.to_state.state') %}... logic and see if hat helps. But it's weird that it is seeing the trigger without a to state
.
@@SlackerLabs its always available, and it always works. Home assistant is falsely reporting it as an error. Reported as bug to HA. no ignore button on the "repair" notice either. oh well. no biggy
@@TristynRusselo If you feel it is falsely giving an error alert, I would suggest the forums or log an issue.
Your issue is very weird. I've never had a template based around valid values give an error. If trying the tips in this video do not work, I would suggest looking at why the entity in the template has an invalid value. Is it just when yourestart that it reports unknown until it gets the first value in?
Things like that are beyond UA-cam comments section though
@@EsotericArctos As I stated in my last post, i have reported it already to HA (via github bug reporting).
they were no help for a workaround, thought maybe here would spark an idea.
The error message is a "repair" notification. it shows on reboot and after the automation runs. The "value" it is looking for is the trigger to state (on or off). did the trigger entity turn on? or off?
heres the whole thing:
trigger:
- platform: state
entity_id:
- light.master_bedroom_ceiling
action:
- service: homeassistant.turn_{{ trigger.to_state.state }}
target:
entity_id:
- light.master_bedroom_tv_lightstrip
@@TristynRusselo Exclude 'unavailable' and 'unknown' from your trigger:
trigger:
- platform: state
not_to:
- unavailable
- unknown
entity_id:
- light.master_bedroom_ceiling
did your wife buy you that shirt?
Umm. Actually. Yes.