The ask: Which card ought one to select to provide a user-selectable datetime
input into a script which calls a python_script
to set states of a tod binary_sensor
?
Background: I have a blueprint (for automation) which, based on mmWave presence sensors, triggers automations controlling lights, switches, temperatures/thermostats and such. Based on the boss’s (Mrs) request, i added a do not disturb
(DND) feature to the blueprint based on a time of day binary_sensor
from the helper
section of lovelace UI. Such that, if DND is on
then presence sensors’ state changes don’t trigger automations; else it does. Blueprint/automation worked like a charm.
Then the boss requested an ability to make distinction between weekdays and weekends. No problem. I created a script (below) to do so. Went above and beyond (or so i thought) to accommodate holidays as well. It involved my first foray into python_script
. Easy peasy courtesy of many other well-seasoned HA folks.
But, boss’s latest request stumped me a little bit. She wants to be able to change this DND behavior from her (non-admin) dashboard view. I have yet to find an equivalent of an input card which allows manipulating datetime
object which can then be passed an input to the above script.
alias: set_dnd
description: >
Used to set DND mode which controls zzz_mode sensor. The idea is:
- Set default values for variables; change based on weekeday or
weekend/holiday
- If no user input is provided, then set DND based on default times; else use
user input
variables:
def_state: "on"
def_weekday_before: "07:00"
def_weekday_after_time: "21:30"
def_weekend_before: "08:00"
def_weekend_after_time: "22:00"
fields:
i_state:
name: State
description: Whether we want to set DND to ON or OFF
selector:
state:
entity_id: binary_sensor.zzz_mode
required: false
default: "on"
example: On, Off
i_weekday_after:
name: After for weekday
description: Time during weekday after which DND is activated
selector:
datetime: null
required: true
i_weekend_after:
name: After for weekend
description: Time during weekend after which DND is activated
selector:
datetime: null
required: false
sequence:
- service: python_script.set_state
data:
entity_id: binary_sensor.zzz_mode
state: |
{% if i_state is none %}
{{ def_state }}
{% else %}
{{ i_state }}
{% endif %}
after: >
{% set n = now () %} {% set def_weekday_after = (((n|string)[:11] ~
def_weekday_after_time) | as_datetime|as_local).isoformat() %} {% set
def_weekend_after = (((n|string)[:11] ~ def_weekend_after_time) |
as_datetime|as_local).isoformat() %} {% if
is_state('binary_sensor.workday', 'on') and i_weekday_after is not none
%}
{{ ((i_weekday_after|string) | as_datetime | as_local).isoformat() }}
{% elif is_state('binary_sensor.workday', 'on') and i_weekday_after is
none %}
{{ def_weekday_after }}
{% elif is_state('binary_sensor.workday', 'off') and i_weekend_after is
not none %}
{{ ((i_weekend_after|string) | as_datetime | as_local).isoformat() }}
{% else %}
{{ def_weekend_after }}
{% endif %}
mode: single