Hello everyone,
I’ve created a input_select that allows me to select an amount of time (this is used to disable security cameras temporarily). The amount of time selection kicks off a timer for the duration of the selected amount of time. When the timer expires, it resets the input_select and that turns the cameras back on.
I spent a good portion of the weekend trying to accomplish this. I found several examples that were close to what I want to do on the forum but nothing covered everything I needed/wanted. So, I took bits and pieces from the different examples I found and this is what I came up with. I’m not proud of it, it feels very hacky. I’m probably doing it wrong but I couldn’t figure out a better way to do what I wanted to do.
I tried for hours today to set a “re-activate timestamp”. This timestamp would be the future timestamp that the cameras would be enabled again. I tried to do this using an input_datetime entity but I was unsuccessful. Mostly, I’m interested if anyone has a better way to do this or knows a way I can set the timestamp based on the timer selection.
From configuration.yaml
:
switch:
- platform: zoneminder
command_on: Modect
command_off: Monitor
- platform: command_line
switches:
zm_disable_switch:
command_on: "/home/spikeygg/toggle_camera.py Weather"
command_off: "/home/spikeygg/toggle_camera.py Normal"
command_state: "/home/spikeygg/toggle_camera.py fetch"
value_template: '{{ value == "Weather" }}'
friendly_name: ZoneMinder Disable
#input_datetime:
# zm_camera_reenabled:
# name: 'Cameras will turn on at'
# has_date: false
# has_time: true
input_select:
zm_disable_select:
name: Temporarily Disable ZoneMinder
options:
- Cameras on
- 5 minutes
- 15 minutes
- 30 minutes
- 1 hour
- 2 hours
- 4 hours
- 5 hours
initial: Cameras on
icon: mdi:timer
timer:
zm_camera_timer:
name: 'Timer for ZM disable'
icon: mdi:timer
From automations.yaml
:
# Process a change to the input_select, kick off timer, etc.
- id: '1524950593024'
alias: Temporarily turn off the cameras
hide_entity: True
trigger:
- platform: state
entity_id: input_select.zm_disable_select
to: '5 minutes'
- platform: state
entity_id: input_select.zm_disable_select
to: '15 minutes'
- platform: state
entity_id: input_select.zm_disable_select
to: '30 minutes'
- platform: state
entity_id: input_select.zm_disable_select
to: '1 hour'
- platform: state
entity_id: input_select.zm_disable_select
to: '2 hours'
- platform: state
entity_id: input_select.zm_disable_select
to: '4 hours'
- platform: state
entity_id: input_select.zm_disable_select
to: '5 hours'
action:
- service: timer.start
data_template:
entity_id: timer.zm_camera_timer
duration: >
{% if is_state("input_select.zm_disable_select", "5 minutes") %}
00:05:00
{%-elif is_state("input_select.zm_disable_select", "15 minutes") %}
00:15:00
{%-elif is_state("input_select.zm_disable_select", "30 minutes") %}
00:30:00
{%-elif is_state("input_select.zm_disable_select", "1 hour") %}
01:00:00
{%-elif is_state("input_select.zm_disable_select", "2 hours") %}
02:00:00
{%-elif is_state("input_select.zm_disable_select", "4 hours") %}
04:00:00
{%-elif is_state("input_select.zm_disable_select", "5 hours") %}
05:00:00
{% else %}
00:00:30
{% endif %}
- service: switch.turn_on
data:
entity_id: switch.zm_disable_switch
# This stuff is just things I tried...
# - delay: '00:00:05'
# - service: input_datetime.set_datetime
# data_template:
# entity_id: input_datetime.zm_camera_reenabled
# time: '{{ ( as_timestamp(now()) + (states("timer.zm_camera_timer.remaining") | int) * 3600) | timestamp_custom("%H:%M:%S") }}'
# date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
# time: '{{ ( as_timestamp(now()) + (states("timer.zm_camera_timer.duration") | int) * 60) | timestamp_custom("%H:%M:%S") }}'
# When the timer expires it should reset the selection box to "Cameras on", and that will trigger the re-enable.
- id: '1524950593025'
alias: Timer expired, reset the select box
hide_entity: True
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.zm_camera_timer
action:
- service: input_select.select_option
data:
entity_id: input_select.zm_disable_select
option: 'Cameras on'
- id: '1524950593026'
alias: Turn the cameras back on
hide_entity: True
trigger:
- platform: state
entity_id: input_select.zm_disable_select
to: 'Cameras on'
action:
- service: switch.turn_off
data:
entity_id: switch.zm_disable_switch
- service: timer.cancel
data:
entity_id: timer.zm_camera_timer
I found recently that I can go into the Developer Tools->Templates and type in the coded structure stuff to see how it comes out and with a timer object, I could not figure out a way to extract the remaining time nor duration.
Any help would be appreciated.
Thanks,
-Greg