neilv
(Neil)
August 3, 2024, 8:11pm
1
I want my LED strip to light up (with a “percentage” effect) when a timer is set on a Google Nest speaker and for the LED strip to sync with the set timer.
I found this on Github
I’ve been working on it but can’t get the most important part to work.
I need to create a template that converts the Google Assistant timers time into a percentage.
I’m completely lost after trying for hours.
Does anyone have a solution for this?
The template (from Github) specifies this:
{{ (((as_timestamp(state_attr('sensor.office_echo_dot_next_timer', 'prior_value')) | float - (as_timestamp(now()) | float) | float) / ((state_attr('sensor.office_echo_dot_next_timer', 'sorted_active') | from_json)[0][1].originalDurationInMillis/ 1000)| round(0))*100) | round(0) }}
Among a lot of other things, I tried:
{{ (((((as_datetime(state_attr('sensor.keuken_timers','timers')[0].fire_time))) | float - (as_timestamp(now()) | float) | float) / ((state_attr('sensor.keuken_timers','timers')[0].duration))| round(0))*100) | round(0) }}
These are the times i used with the outcome
{%- set fire= as_datetime(state_attr('sensor.keuken_timers','timers')[0].fire_time).time() %}
{%- set duration=(state_attr('sensor.keuken_timers','timers')[0].duration)%}
{{ fire }}
{{ duration }}
21:13:39 (not correct must be +01:00?)
0:15:00
Status-attributen entity
next_timer_status: set
timers:
- timer_id: timer/ab65c73e-3c5f-4b14-8af9-9c2582ebb1d5
fire_time: 1722716019
local_time: "2024-08-03 22:13:39"
local_time_iso: "2024-08-03T22:13:39+02:00"
duration: "0:15:00"
status: set
label: null
device_class: timestamp
friendly_name: Keuken timers
Edwin_D
(Edwin D.)
August 3, 2024, 9:14pm
4
Ah, ok. That documentation is vital information you left out.
The duration you show cannot be rounded, because it seems to be a string representing a duration. It could also be a timedelta. Does this return true?
{{ state_attr('sensor.keuken_timers','timers')[0].duration is string }}
Hellis81
(Hellis81)
August 3, 2024, 9:43pm
5
Because I’m on my phone it’s hard to write it all.
But this should give you the calculations, you just need to replace the fixed values with your sensors.
{% set left = as_timestamp("2024-08-03 23:13:39") - now().timestamp() %}
{% set duration = "0:15:00".split(":") %}
{% set duration_seconds = duration[0]|int*3600 + duration[1]|int*60 + duration[2]|int %}
{{ (1-(left/duration_seconds)) *100 }}
neilv
(Neil)
August 4, 2024, 11:44am
7
Nice, this works when I test it.
Only when I put this in my config file, the sensor(s) are not showing.
- platform: template
sensors:
progress_bar_assistent_woonkamer:
friendly_name: 'Progress Bar Assistent Woonkamer'
state: "{{ now().strftime('%-H:%M') }}"
trigger:
platform: time_pattern
seconds: "/1"
sensor:
assistent_woonkamer_timer_percentage:
friendly_name: 'Assistent Woonkamer time precentage'
unit_of_measurement: '%'
state: |
{% set left = as_timestamp(state_attr('sensor.woonkamer_timers','timers')[0].local_time) - now().timestamp() %}
{% set duration = (state_attr('sensor.woonkamer_timers','timers')[0].duration).split(":") %}
{% set duration_seconds = duration[0]|int*3600 + duration[1]|int*60 + duration[2]|int %}
{{ (1-(left/duration_seconds)) *100 }}
this is wat the github info gives:
template:
- sensor:
- name: Time AM PM
unique_id: time_am_pm
state: "{{ now().strftime('%-I:%M %p') }}"
- trigger:
- platform: time_pattern
seconds: "/1"
sensor:
- name: "Office Echo Dot Timer Percentage"
unique_id: office_echo_dot_timer_percentage
unit_of_measurement: '%'
state: "{{ (((as_timestamp(state_attr('sensor.office_echo_dot_next_timer', 'prior_value')) | float - (as_timestamp(now()) | float) | float) / ((state_attr('sensor.office_echo_dot_next_timer', 'sorted_active') | from_json)[0][1].originalDurationInMillis/ 1000)| round(0))*100) | round(0) }}"
Hellis81
(Hellis81)
August 4, 2024, 5:05pm
8
I don’t think you need to make this a time triggered sensor.
Sadly the now timestamp only updates every minute if I recall correctly.
So you can use the GUI and make a normal sensor as I see it.
Perhaps there is a way to make it update more often but I don’t know of any ways to do that.
Edwin_D
(Edwin D.)
August 4, 2024, 6:10pm
9
This triggers 31.5 million times a year. How many of those lead to action? I.m.h.o. you should avoid this at all cost.
Edwin_D
(Edwin D.)
August 4, 2024, 7:00pm
11
If the value actually changes each time, you might also want to exclude it from the recorder.
Hellis81
(Hellis81)
August 4, 2024, 7:08pm
12
It only changes when there is a timer.
Most of the time it will probably be unavailable
neilv
(Neil)
August 4, 2024, 7:38pm
13
If i set a timer for two minutes, my leds only update 2 times😪
alias: Progress Bar Timers Woonkamer
description: ""
trigger:
- platform: state
entity_id:
- sensor.timer_assistent_woonkamer
condition:
- condition: not
conditions:
- condition: state
entity_id: sensor.timer_assistent_woonkamer
state: unavailable
action:
- service: number.set_value
data:
value: "{{ states('sensor.timer_assistent_woonkamer') }}"
target:
entity_id: number.wled_woonkamer_intensiteit
mode: single
Hellis81
(Hellis81)
August 5, 2024, 5:54am
14
The only thing I can think of is to create a similar time pattern triggered sensor as you tried before but use sensor.time since this gives you HH:MM and then you add the seconds to it and create the calculation.
So let’s say you want to trigger every 10 seconds.
- trigger:
- platform: time_pattern
seconds: "0"
id: 0
- platform: time_pattern
seconds: "10"
id: 10
- platform: time_pattern
seconds: "20"
id: 20
And so on.
Then you use {{ today_at('sensor.time') + timedelta(seconds=trigger.id) }}
And this is instead of the now()
part of the sensor template.
neilv
(Neil)
August 5, 2024, 7:03am
15
That’s something I don’t fully understand.
The problem is not in the automation, but in fact that the time is not updating often enough to trigger the percentage of the LED bar?? Correct?
Hellis81
(Hellis81)
August 5, 2024, 7:39am
16
template:
- trigger:
- platform: time_pattern
seconds: 0
id: "0"
- platform: time_pattern
seconds: 10
id: "10"
- platform: time_pattern
seconds: 20
id: "20"
- platform: time_pattern
seconds: 30
id: "30"
- platform: time_pattern
seconds: 40
id: "40"
- platform: time_pattern
seconds: 50
id: "50"
sensor:
- name: "assistent_woonkamer_timer_percentage"
friendly_name: 'Assistent Woonkamer time precentage'
unique_id: "assistent_woonkamer_timer_percentage"
unit_of_measurement: '%'
state: >-
{% set left = as_timestamp(state_attr('sensor.woonkamer_timers','timers')[0].local_time) - (today_at(states('sensor.time')) + timedelta(seconds=trigger.id)).timestamp() %}
{% set duration = (state_attr('sensor.woonkamer_timers','timers')[0].duration).split(":") %}
{% set duration_seconds = duration[0]|int*3600 + duration[1]|int*60 + duration[2]|int %}
{{ (1-(left/duration_seconds)) *100 }}
Like this perhaps…
So this triggers every ten seconds and it uses the sensor.time (if you have this) else you could use (now().time()|string)[0:5]
or any other method to get HH:MM time and then we add the seconds to get a more accurate percentage.
Hellis81
(Hellis81)
August 5, 2024, 7:46am
17
I just realized maybe you can use now().timestamp().
All you need is the time triggers and now should update.
Give it a try.
I suggest you keep a copy of your current sensor so that you have something to fall back on
neilv
(Neil)
August 5, 2024, 6:50pm
18
Ok tnx, i will try it shortly.
neilv
(Neil)
August 10, 2024, 10:14am
19
Sorry, I give up. I’ve spent a lot of time trying to make it work, but no luck.
I can’t see the forest for the trees.
Hellis81
(Hellis81)
August 10, 2024, 6:18pm
20
Where have you gone stuck?
Is the sensor reporting a correct percentage?
Hellis81
(Hellis81)
August 10, 2024, 7:59pm
21
I realized there was a few errors in that code…
But I managed to get it working now.
I used some dummy values in the yaml, now I replaced those with your entities and hopefully I did it correctly.
template:
- trigger:
- platform: time_pattern
seconds: 0
id: "0"
- platform: time_pattern
seconds: 10
id: "10"
- platform: time_pattern
seconds: 20
id: "20"
- platform: time_pattern
seconds: 30
id: "30"
- platform: time_pattern
seconds: 40
id: "40"
- platform: time_pattern
seconds: 50
id: "50"
sensor:
- name: "assistent_woonkamer_timer_percentage"
unique_id: "assistent_woonkamer_timer_percentage"
unit_of_measurement: '%'
state: >-
{% set left = as_timestamp(state_attr('sensor.woonkamer_timers','timers')[0].local_time) - (today_at(states('sensor.time')) + timedelta(seconds= trigger.id | int)).timestamp() %}
{% set duration = (state_attr('sensor.woonkamer_timers','timers')[0].duration).split(":") %}
{% set duration_seconds = duration[0]|int*3600 + duration[1]|int*60 + duration[2]|int %}
{% if (1-(left/duration_seconds)) *100 < 101 %}
{{ (1-(left/duration_seconds)) *100 }}
{% else %}
{% endif %}
1 Like