Trigger template based on Hue switches needs event.time_fired

using

template:

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: auditorium_tap_switch_button
    sensor:
      - name: Hue tap switch last event
        state: >
          {{trigger.event.data.subtype}}

I get a nice sensor showing the button pressed, because subtype holds that info . Next I can trigger upon state change of that sensor (which I find way easier than creating a device_trigger automation in the UI tbh…

:

{
    "event_type": "hue_event",
    "data": {
        "id": "auditorium_tap_switch_button",
        "device_id": "6854508929ede1f8f5e14273b9925bf9",
        "unique_id": "38185770-7c55-4c41-9d31-64a57a8d8dc4",
        "type": "initial_press",
        "subtype": 3
    },
    "origin": "LOCAL",
    "time_fired": "2021-12-06T18:55:51.226364+00:00",
    "context": {
        "id": "dc36b6a7c32518e1d1fad8937d29a8e1",
        "parent_id": null,
        "user_id": null
    }
}
Event 3 fired 19:55:
{
    "event_type": "hue_event",
    "data": {
        "id": "auditorium_tap_switch_button",
        "device_id": "6854508929ede1f8f5e14273b9925bf9",
        "unique_id": "2e26537a-f3ae-4e81-8c08-e52c30c407fe",
        "type": "initial_press",
        "subtype": 4
    },
    "origin": "LOCAL",
    "time_fired": "2021-12-06T18:55:50.025285+00:00",
    "context": {
        "id": "e4ef91ec98b4742344baff4ac153f239",
        "parent_id": null,
        "user_id": null
    }
}

however, it wont change state upon clicking the same button multiple times.

How can I add the time_fired to the trigger, so it re-eveluates upon clicking the same button?

Is not as simple as adding:

template:

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: auditorium_tap_switch_button
    sensor:
      - name: Hue tap switch last event
        state: >
          {{trigger.event.data.subtype}}
        attributes:
          last_triggered: >
            {{trigger.event.time_fired}}

creating the last_triggered:


triggering so I guess it is :wink:

thanks for having a look

Dimmer switch:

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: living_dimmer_switch_button
    sensor:
      - name: Living dimmer switch last event
        state: >
          {{trigger.event.data.subtype}},{{trigger.event.data.type}}
        attributes:
          last_triggered: >
            {{trigger.event.time_fired}}

Dec-06-2021 23-41-12

nice really :wink:

Configs for Hue smart button, Tap switch and dimmer switch:

##########################################################################################
# Templates
##########################################################################################

template:

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: hue_smart_button_1_button
    sensor:
      - unique_id: hue_smart_button_1_last_event
        name: Hue smart button 1
        <<: &multiple
          state: >
            {{trigger.event.data.subtype}},{{trigger.event.data.type}}
          attributes:
            last_triggered: >
              {{trigger.event.time_fired}}

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: auditorium_tap_switch_button
    sensor:
      - unique_id: auditorium_tap_switch_last_event
        name: Auditorium tap switch
        state: >
          {{trigger.event.data.subtype}}
        attributes:
          last_triggered: >
            {{trigger.event.time_fired}}

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: living_dimmer_switch_button
    sensor:
      - unique_id: living_dimmer_switch_last_event
        name: Living dimmer switch
        <<: *multiple

‘unknown’ until triggered…

Hi Marius,

This does indeed look very good (and I almost understand it too which helps).

Am I right in thinking there’s a few lines missing from the end of the templates though or should it really end:-

    sensor:
      - unique_id: living_dimmer_switch_last_event
        name: Living dimmer switch
        <<: *multiple

Shouldn’t there be state and attributes sections like the ones for button and tap ?

Thanks for taking the time to do this,
Ian

no, that is a yaml (paste) anchor, and it copies the yaml &anchor, which is declared above it in the yaml document, see the hue_smart_button

you can declare once (with &anchor_name), and re-use as many times (with *anchor_name) below it in the same document. cuts code and prevents edit errors :wink:

separate post and plug:

custom hue icons made by Andy Allsopp @Andy_Allsopp
and to be found at: GitHub - arallsopp/hass-hue-icons: Additional vector icons for home assistant to model Philips Hue bulbs and fixtures.

works perfectly fine also in the core HA icon picker ui. Highly recommended.

1 Like

Thanks, I think I follow that…

and the icons - I already have those - and very good they are too! Also recommended!

1 Like

small update here, changed the Hue smart button to:

template:

  - trigger:

      - platform: event
        event_type: hue_event
        event_data:
          id: hue_smart_button_1_button
    sensor:
      - unique_id: hue_smart_button_1_last_event
        name: Hue smart button 1
        <<: &multiple
          state: >
            {{trigger.event.data.type}}
          attributes:
            last_triggered: >
              {{trigger.event.time_fired}}

since it only has 1 button… we dont need the {{trigger.event.data.subtype}} at all.

making that:

I was asked how to use these sensors a simple example:

automation:

  - alias: Toggle Security system
    id: Toggle security system
    trigger:
      platform: event
      event_type: hue_event
      event_data:
        id: hue_smart_button_1_button
        type: long_release
    action:
      service: switch.toggle
      target:
        entity_id: switch.security_system

can be replaced by:

automation:

  - alias: Toggle Security system
    id: Toggle security system
    trigger:
      platform: state
      entity_id: sensor.hue_smart_button_1
      to: long_release
    action:
      service: switch.toggle
      target:
        entity_id: switch.security_system

or

  - alias: Test smart button short
    id: Test smart button short
    trigger:
      platform: state
      entity_id: sensor.hue_smart_button_1
      to: short_release
    condition:
      - >
        {{is_state('input_boolean.notify_developing','on')}}
    action:
      service: notify.mobile_app_calltheboss
      data:
        title: Test Smart button
        message: >
          This was a {{trigger.to_state.state}}

not very impressive in this particular case, but when using more complex button press scenario’s it might be helpful

Congratulations! This is great!
I created a panic button for the bathroom for my baby son.

I was looking how to evaluate the button press as sometimes he plays with it.
So I want only to trigger once every 5 minutes.

After I used your code to create the entity I came up with the following condition to use at my automation

condition: template
value_template: >-
  {{ now() | as_timestamp - state_attr('sensor.hue_tap_switch_last_event',
  'last_triggered') | as_timestamp > 120   }}

But unfortunately, it will never go through…
What I am doing wrong?

if you want to trigger every 5 minutes, why not add that to the trigger?

Dear Marius,
I don’t want to trigger every 5 minutes.

What I want, when pressed once, is not to be able to re-triggered within 5 minutes.
(at my example is 120 seconds)

That’s the reason I came up with a condition

  {{ now() | as_timestamp - state_attr('sensor.hue_tap_switch_last_event',
  'last_triggered') | as_timestamp > 120   }}

But although the template is correct it never go through …

I’ve managed to solve my problem with a helper instead of a template condition.

When button is pressed after the action I turn on a helper.
Then I reset it after 5 minutes.

I know that it might look silly to some of you but it solved my problem and I don’t use at all the last_triggered attribute.