arretx
(Jon Griffith)
December 4, 2022, 6:56pm
1
In the past, I’ve used a function node in NodeRED to associate a string (“November”) with the number 11, creating an array of months, etc.
This is to automatically shift my system into “Thanksgiving” mode or “Christmas Mode” etc., so associated audio automations would change based upon the holiday or the season.
I can create input selects for Seasons, Months, Holidays, etc., but I was wondering if there’s already a sensor that reports “November” as an attribute. Or “Sunday” as the current day…
I know that I can create template sensors…which is what I may end up doing.
Thoughts?
You just have to create your own with templates. Using strftime makes it relatively easy.
kartcon
(Art Davis)
December 5, 2022, 12:59am
3
Take what you need:
#############################################################
# Templates Sensors #
#############################################################
- sensor:
#############################################################
# Time & Date Templates #
#############################################################
############################
# Readable Time #
############################
- name: "Readable Time"
unique_id: readable_time
state: "{{ now().strftime('%I:%M %p') }}"
############################
# Readable Date Short #
############################
- name: "Readable Date Short"
unique_id: readable_date_short
state: "{{ as_timestamp(now(),0) | timestamp_custom('%x', True) }}"
############################
# Readable Date Long #
############################
- name: "Readable Date Long"
unique_id: readable_date_long
state: "{{ as_timestamp(now(),0) | timestamp_custom('%a %b %d, %Y', True) }}"
############################
# Readable Date #
############################
- name: "Readable Date"
unique_id: readable_full
state: "{{ now().strftime('%a %b %d, %Y | %I:%M %p') }}"
############################
# Time of Day #
############################
- name: "Time of Day"
unique_id: time_of_day
state: >
{% set current_hour = strptime(states('sensor.time'), "%H:%M").hour %}
{% if current_hour < 12 %}
Morning
{% elif 12 <= current_hour < 18 %}
Afternoon
{% else %}
Evening
{% endif %}
############################
# Day of the week #
############################
- name: "Day of the week"
unique_id: day_of_the_week
state: "{{ now().strftime('%A') }}"
# Returns: Sunday
############################
# Day of the Month #
############################
- name: "Day of the Month"
unique_id: day_of_the_month
state: >
{% set suffix = ['st', 'nd', 'rd'] %}
{% set day = now().day %}
{% set index = 3 if day // 10 == 1 or day % 10 == 0 else (day % 10) - 1 %}
{{ day~'th' if index > 2 else day~suffix[index] }}
# Returns: 18th
############################
# Month Name #
############################
- name: "Month Name"
unique_id: month_name
state: "{{ now().strftime('%B') }}"
# Returns: August
############################
# Two Weeks Ahead #
############################
# - name: "Two Weeks Ahead"
# unique_id: date_two_weeks_ahead:
# state: "{{ (as_timestamp(now(),0) + (14*24*3600)) | timestamp_custom('%Y-%m-%d', True) }}"
# # Returns: date two weeks ahead
############################
# Day number of the week #
############################
- name: "Day number of the week"
unique_id: day_num_of_the_week
state: "{{ now().strftime('%w') }}"
# Returns: Sunday
############################
# Week number of the Year #
############################
- name: "Week number of the Year"
unique_id: week_num_of_year
state: "{{ now().strftime('%U') }}"
# Returns: 33
############################
# Day number of the Month #
############################
- name: "Day number of the Month"
unique_id: day_num_of_the_month
state: "{{ now().day }}"
# Returns: 18
############################
# Day number of the Year #
############################
- name: "Day number of the Year"
unique_id: day_num_of_year
state: "{{ now().strftime('%j') }}"
# Returns: 230
############################
# Month number of the Year#
############################
- name: "Month number of the Year"
unique_id: month_number_of_year
state: "{{ now().strftime('%-m') }}"
# Returns: 12
############################
# Week Odd/Even #
############################
- name: "Week Odd/Even"
unique_id: week_odd_even
state: >-
{% set year = states.sensor.date_time.state.split(',')[0].split('-')[0] %}
{% set month = states.sensor.date_time.state.split(',')[0].split('-')[1] %}
{% set date = states.sensor.date_time.state.split(',')[0].split('-')[2] %}
{% set today = strptime(year ~ '-' ~ month ~ '-' ~ date , '%Y-%m-%d') %}
{%- if (as_timestamp(today) | timestamp_custom('%U', true) | int ) % 2 == 0 -%}
Even Week
{%- else -%}
Odd Week
{%- endif -%}
# Returns: Odd Week/Even Week
# Even Week (Week# {{ as_timestamp(today) | timestamp_custom('%U', true) }})
# Odd Week (Week# {{ as_timestamp(today) | timestamp_custom('%U', true) }})
############################
# Pay Day #
############################
- name: "Pay Day"
unique_id: payday
icon: mdi:cash-multiple
state: >-
{% if states.sensor.week_odd_even.state == "Odd Week" and states.sensor.day_of_the_week.state == "Friday" %}
True
{%- else -%}
False
{% endif %}
# Returns: True/False
#
1 Like