Olivier1974
(Olivier Toussaint)
February 18, 2024, 12:29pm
1
I have a kinetic momentary switch on MQTT.
It is great and I’ve setup a binary_sensor on the events like so:
mqtt:
- binary_sensor:
- name: "Niko ZGP - Button A0"
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_1"
payload_off: "release_1"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
Now I have a sensor from on
to off
, so far, so good.
But now, I’d like to detect one of the following 2 scenarios in a template trigger : short press (on
/off
within one second), long press (on
for more than one second).
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
binary_sensor:
- name: "Button A0 - Short Press"
state: >
{{ }}
- name: "Button A0 - Long Press"
state: >
{{ }}
But I’m struggling with something to put as Jinja here.
Maybe the approach is wrong, feel free to guide me.
EDIT: ChatGPT is not of a big help either, here is its solution:
For short press, this is working somehow because, yes, I’m switching from off
to on
but I’d like it to not detect short press unless the off
is coming again within 1 second.
{{ trigger.to_state.state == 'on' and trigger.from_state.state == 'off' }}
For long press, this is not working, at all.
{{ trigger.to_state.state == 'on' and (as_timestamp(now()) - as_timestamp(trigger.from_state.last_changed)) >= 1 }}
Please help me
123
(Taras)
February 18, 2024, 6:40pm
2
Perhaps with a State Trigger with its for
option set to 1 second?
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
from: 'off'
to: 'on'
for:
seconds: 1
1 Like
Olivier1974
(Olivier Toussaint)
February 18, 2024, 7:05pm
3
Great start, thank you! I reworked it a bit
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button A0 - Long Press"
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
binary_sensor:
- name: "Niko ZGP Button A0 - Short Press"
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1
It is not bullet proof, I’ll have to add a check on short trigger that it did not change to a long trigger but it is working as expected, I can manage from here.
1 Like
TheFes
(The Fes)
February 18, 2024, 8:30pm
4
You can do something like this
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
from: "off"
to: "on"
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button A0 - Long Press"
state: "{{ true }}"
auto_off: 1
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
from: "on"
to: "off"
binary_sensor:
- name: "Niko ZGP Button A0 - Short Press"
state: "{{ trigger.to_state.last_changed - trigger.from_state.last_changed < timedelta(seconds=1) }}"
auto_off: 1
PS, I would advice to add unique_id
s to your binary sensors
Olivier1974
(Olivier Toussaint)
February 19, 2024, 5:08pm
5
This is what I did, removing the auto_off for long press because I want to continuously do something when this happen “until” it is off.
I always add unique_id but I posted only a part of the code, the full one is useless in my opinion because it is too long and everyone will give up
Here it is for future generations
mqtt sensors
mqtt:
- binary_sensor:
- name: "Niko ZGP - Button A0"
unique_id: 43f1df9d-287f-4af1-9b6e-43f0280fcb6e
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_1"
payload_off: "release_1"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
- name: "Niko ZGP - Button A1"
unique_id: be9a9003-1215-4e10-9d9f-023bf2eebd1d
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_2"
payload_off: "release_2"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
- name: "Niko ZGP - Button B0"
unique_id: d9eb7127-4360-439d-9ead-77b8df877af8
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_3"
payload_off: "release_3"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
- name: "Niko ZGP - Button B1"
unique_id: 4b38e7ba-3dc5-4c1f-8acf-50b83e59a185
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_4"
payload_off: "release_4"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
- name: "Niko ZGP - Button A0 and B0"
unique_id: 19de44ef-dce0-4b0d-9489-eee6f16fcc0d
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_1_and_3"
payload_off: "release_1_and_3"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
- name: "Niko ZGP - Button A1 and B1"
unique_id: f81c7ddc-825a-41e2-8fdd-ae3b25b270b0
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_2_and_4"
payload_off: "release_2_and_4"
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
- name: "Niko ZGP - Energy bar"
unique_id: 3bee94b4-6ae5-4f37-97ec-cd56b7f7ebbd
icon: mdi:grid-large
state_topic: "zigbee2mqtt/Niko ZGP/action"
payload_on: "press_energy_bar"
off_delay: 1
availability:
- topic: "zigbee2mqtt/Niko ZGP/availability"
payload_available: "online"
payload_not_available: "offline"
templating for short/long press
template:
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button A0 - Long Press"
icon: mdi:grid-large
unique_id: 191df587-1104-4193-98dd-f671aa51fb1f
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0
binary_sensor:
- name: "Niko ZGP Button A0 - Short Press"
icon: mdi:grid-large
unique_id: b8e8ffb6-518c-49af-9894-30139fe7ad46
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a1
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button A1 - Long Press"
icon: mdi:grid-large
unique_id: 41d3280d-751a-4d6e-a921-62895238cb9e
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a1
binary_sensor:
- name: "Niko ZGP Button A1 - Short Press"
icon: mdi:grid-large
unique_id: 03067789-77ef-4bc4-8a8f-0f21dfa752dc
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_b0
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button B0 - Long Press"
icon: mdi:grid-large
unique_id: 8f77581b-c6d7-4f35-bdab-e06232500aef
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_b0
binary_sensor:
- name: "Niko ZGP Button B0 - Short Press"
icon: mdi:grid-large
unique_id: aee9d976-0eb1-4f43-a66f-1c86466b744f
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_b1
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button B1 - Long Press"
icon: mdi:grid-large
unique_id: 9c1313e3-c930-4712-9d35-cafeada6f614
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_b1
binary_sensor:
- name: "Niko ZGP Button B1 - Short Press"
icon: mdi:grid-large
unique_id: 583cee62-56cf-45e6-8f02-4f060d58ece0
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0_and_b0
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button A0 and B0 - Long Press"
icon: mdi:grid-large
unique_id: 37a5abc0-1397-4493-9c6a-318b9c22c68a
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a0_and_b0
binary_sensor:
- name: "Niko ZGP Button A0 and B0 - Short Press"
icon: mdi:grid-large
unique_id: 0c9816a4-0fc3-423f-b74c-3f8a39c9ef35
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a1_and_b1
for:
seconds: 1
binary_sensor:
- name: "Niko ZGP Button A1 and B1 - Long Press"
icon: mdi:grid-large
unique_id: 54f4f0a4-267b-4792-a064-d155f592b5dc
state: "{{ trigger.to_state.state == 'on' }}"
- trigger:
- platform: state
entity_id: binary_sensor.niko_zgp_button_a1_and_b1
binary_sensor:
- name: "Niko ZGP Button A1 and B1 - Short Press"
icon: mdi:grid-large
unique_id: 1a93b68c-998e-4605-ba48-2ee38c14fc37
state: "{{ trigger.to_state.state == 'on' }}"
auto_off: 1