Something like that, but actually working:
{{ is_state('sensor.ars_computer_audio_peak_volume') > 0 }}
if statement
{% if states('sensor.ars_computer_audio_peak_volume') > '0' %}
True
{% endif %}
How do I add that it has to trigger at some point within the last 5 minutes in order to be True
?
In the current iteration, it has to trigger at this very moment to be True
.
add a nested IF and compare the current time to the last triggered time (if available) and see if its within the past 5 minutes.
Just spitballin
no idea how to do any of that. I’m stuck at the last triggered time thing:
{{ as_timestamp(now()) - as_timestamp(states.ars_computer_audio_peak_volume.last_changed) }}
which produces an error in the template editor:
ValueError: Template error: as_timestamp got invalid input 'None' when rendering template '{{ as_timestamp(now()) - as_timestamp(states.ars_computer_audio_peak_volume.last_changed) }}' but no default was specified
states.sensor.ars_computer_audio_peak_volume.last_changed
What you want though is something like:
{{ states.sensor.ars_computer_audio_peak_volume.last_changed + timedelta(minutes=5) < now() }}
the suggested examples are missing a crucial detail.
all states are strings. you can’t compare a string to a number (well you can but that’s not the point of this thread). You need to convert the string to a number first.
EDIT: thanks @tom_l
{{ states('sensor.ars_computer_audio_peak_volume') | float(0) > 0 }}
That’s not the solution.
by using that you are converting the number to a string and then comparing two strings. Which could (will?) result in unexpected results.
try putting this into the dev tools template editor and you will see that it fails:
{{'13' >'110'}}
While what finity wrote is correct he forgot to edit your template to use the states()
function rather than is_state()
as is_state()
requires two arguments, as per my “other way” example below.
Should be:
{{ states('sensor.ars_computer_audio_peak_volume') | float(0) > 0 }}
Another way assuming the volume can not go negative:
{{ not is_state('sensor.ars_computer_audio_peak_volume','0') }}
I still don’t get how to do the “within the last 5 minutes”–part right.
{{ states('sensor.ars_computer_audio_peak_volume') | float(0) > 0 + timedelta(minutes=5) | float(0) < now() | float(0) }}
results in:
Result type: boolean
false
even though I had audio playing
I believe it’s a lot better if you just explain what you are trying to do.
We only get a small piece of what you are doing, step back and explain the full picture.
I’m trying to make a binary template sensor that checks if the state of sensor.ars_computer_audio_peak_volume was greater than 0 at any point within the last 5 minutes.
sensor.ars_computer_audio_peak_volume being greater than 0 means that audio was played on my computer.
This is a useful trigger/condition for automations because it helps determine if I’m using my PC or not. Although I’m already using a sensor detecting mouse/keyboard input for that, that alone alone isn’t enough when I’m watching a show on Netflix and not inputting anything.
There are windows based companion apps like GamerClassN7/HA_Desktop_Companion: App which is using native HA Api to comunicate and report data to HA (github.com) for example
But if the thought is to use it in an automation, why not just add it to the automation directly? Why create a template sensor first?
I already have the sensor. I’m using HASS.Agent for that.
I suppose without a template it would work like:
trigger:
- platform: numeric_state
entity_id:
- sensor.ars_computer_audio_peak_volume
for:
hours: 0
minutes: 5
seconds: 0
below: 0.01
action:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.playing_media_on_pc
in conjunction with:
trigger:
- platform: numeric_state
entity_id:
- sensor.ars_computer_audio_peak_volume
above: 0
action:
- service: input_boolean.turn_on
target:
entity_id: input_boolean.playing_media_on_pc
I thought using a template sensor would be the cleanest way to do this since they wouldn’t take up 2 automations like what I did above. I’d prefer only having “real” automations in the automation page and not ones that only set up others.
trigger:
- platform: numeric_state
id: "off"
entity_id:
- sensor.ars_computer_audio_peak_volume
for:
hours: 0
minutes: 5
seconds: 0
below: 0.01
- platform: numeric_state
entity_id:
- sensor.ars_computer_audio_peak_volume
above: 0
id: "on"
action:
- service: "input_boolean.turn_{{ trigger.id }}"
target:
entity_id: input_boolean.playing_media_on_pc
I’m not sure you need the “in the last 5 minutes” part. If the volume goes above 0 at all then it will be, by definition, in the last 5 minutes (IOW, 1 second ago is within the last 5 minutes).
create a template binary sensor that you can turn on immediately then use the delay off option to turn it off after the volume is 0 for 5 minutes.