Voice PE - Implementing display of Named Timers in HA *working... sort of*

Sounds like a good candidate for a feature request. So I changed it for you. :wink:

don’t forget to vote

1 Like

Ok, I’m missing something here… how/where do I vote?
EDIT: Never mind! I see it at the top now.

Things were going splendidly, transitioning the home to Preview Edition. Until this bump. Wifey wants to be able to keep an eye on her baking timers from the companion app. I’m sure the HA folks will add this but definitely feels like a misstep not having this feature at launch. Voted!

1 Like

Yep this and swapping out microwakeword for my own are the two things preventing me from deployment.

I know how to do the microwakeword one… Now just need this. It’s definitely a showstopper.

IMHO there’s just should be a timer entity created that the esp can manipulate. Then let me use the normal builtin controls.

1 Like

I’m working on some experimental code now.

I’ve added these changes to the VoicePE yaml config file:

on_boot:
    priority: 375
    then:
      - sensor.template.publish:
          id: next_timer
          state: -1    

and a new sensor

sensor:
  - platform: template
    id: next_timer
    name: "Next timer"
    update_interval: never
    device_class: duration
    unit_of_measurement: s
    icon: "mdi:clock"

and timer automations

  on_timer_started:
    - script.execute: control_leds
    - lambda: | 
          int seconds_left = id(first_active_timer).seconds_left;
          id(next_timer).publish_state(seconds_left);
  on_timer_cancelled:
    - script.execute: control_leds
    - lambda: | 
          int seconds_left = 0;
          id(next_timer).publish_state(seconds_left);    
  on_timer_updated:
    - script.execute: control_leds
    - lambda: | 
          int seconds_left = id(first_active_timer).seconds_left;
          id(next_timer).publish_state(seconds_left);    
  on_timer_tick:
    - script.execute: control_leds
    #only update the timer in HA in increments of 5 seconds so we don't spam the ESPH API
    - lambda: | 
          int seconds_left = id(first_active_timer).seconds_left;
          int lastDigit = std::abs(seconds_left) % 10; 
          if (lastDigit == 0) {
           id(next_timer).publish_state(seconds_left); 
            } else if (lastDigit == 5) {
              id(next_timer).publish_state(seconds_left); 
            }

I then set a timer for 1 hour and it is immediately updated in HA:

60 seconds:
image

ok, so this is all completely doable! Now it’s just time investment in coding for all timers and HA entities. routines need to be written for create, stop, modify, etc.

I’m NOT a very good programmer, but I can stumble my way through and brute force this stuff. For all our sakes!

 on_wife_complaint:
     - lambda: (Happy_Wife == Happy_Life);
4 Likes

This works like a champ, however, I cannot for the life of me figure out how to extract the names of the timers.

Short video with a timer card.

1 Like

You’re welcome! :slight_smile:
Did you try id(first_active_timer).name?

1 Like

You Rock!

No, I didn’t try that, but I will…

1 Like

Great work @jazzmonger – I’ll probably give the HA folks some time to integrate this natively but for anyone that needs this now, this looks like a great workaround. Do you think this would work for device timers? In other words, if I say “Okay Nabu, turn on the Den lights in 10 minutes”, could timer entities also be created for those situations?

1 Like

I figured out timer names. Thanks @formatBCE

Again, this is all experimentation. Now, how to handle multiple timers? I’ve taken it this far, who’s going to polish it up?

Mods:

text_sensor:
  - platform: template
    id: timer_name
    name: "Next Timer Name"
    icon: "mdi:clock"
    - script.execute: control_leds
    - lambda: | 
          int seconds_left = id(first_active_timer).seconds_left;
          id(next_timer).publish_state(seconds_left);
           id(timer_name).publish_state(id(first_active_timer).name);
1 Like

These timers are on HA-side, and they’re effectively just delayed actions. I don’t think there’s way to get them.

1 Like

Nope. They are pure ESPHome timers. In real time.

I’m talking about those commands to voice assistant like “in 10 minutes turn TV off”.

1 Like

Aha. Very possibly. I cant find them anywhere exposed in HA and they certainly aren’t set on the VPE… I just verified that. Of course they are announced there when executed.

Yes, I think there should be the whole Timers integration, and also Alarm clock intergation.

There were already WTH threads about it:

So…am I reading this right that all timers are baked into the firmware? Not inside of HA?!

I really need to read up a LOT more on how to configure things with Voice PE. And customize my own actions/etc.

Yup. Crazy, I know. I mean take the little bit of time to do it right instead of hacking it in firmware.

I am actually working on a custom integration to sync the Voice PE timers with the HA Timer Helper - Right now I have a real struggle with creating Timer Helper instances on the fly.

I also read into the source code for the Voice PE for this. Basically I also extended the ESPHome like @jazzmonger did. To control the timers on the Voice PE I’m calling the C++ functions directly to, for example, to stop the timer:

  services:
    - service: stop_timer
      variables:
        timer_id: string
      then:
        - lambda:  |-
            api::VoiceAssistantTimerEventResponse msg;
            msg.timer_id = timer_id;
            msg.event_type = api::enums::VOICE_ASSISTANT_TIMER_CANCELLED;
            msg.total_seconds = 0;
            msg.seconds_left = 0;
            msg.is_active = false;
            id(va).on_timer_event(msg);

But there’s still a bug, since the assistant will tell me the timer still persists (it won’t ring though) Opened an issue here but didn’t get any response.

1 Like

Thanks for the inspiration!
Have used variation of your code to create 2 objects in HA:

  1. sensor having “finished timer” name
  2. event firing on timer finishing.

That allows to create an automation that giving announcement: “Timer XXXX finished”. Sometimes it is great to hear exact name of timer, and not just alarm sound.
concept is like that:

text_sensor:
  - platform: template
    id: finished_timer_name
    name: "Finished timer name"
    update_interval: never
    icon: "mdi:clock"

event:
  - platform: template
    id: timer_event
    name: "Timer"
    icon: mdi:camera-timer
    device_class: button
    event_types:
      - timer_running
      - timer_finished


voice_assistant:
.....
  on_timer_finished:
    - switch.turn_on: timer_ringing
    - event.trigger:
        id: timer_event
        event_type: "timer_finished"
    - lambda: | 
          id(finished_timer_name).publish_state(id(first_active_timer).name);
          if (is_timer_active) {
            id(timer_event).trigger("timer_running");
          }

I honestly dont know why setting a timer from the VPE doesnt start a timer in HA. Once its set there you can act on it in so many ways. I don’t think the timer stuff in VPE is long for the world. Too many issues and problems.

But, all this work and investigation kinda proves that.

And honestly I would turn this on for ALL timers. We’re constanly asking Alexa “what is the name of that timer?”

As a chef, I sometimes have 4-5 timers running at a time.