dbrunt
(Daniel)
February 25, 2021, 8:38am
1
I was using this but the message varies…
value_template: '{{ states("sensor.front_door_lock_logging").split(" Today ")[0] }}'
The current message is:
'Locked Manually on 2021-02-24 at 2:27:08 AM User Code=0'
but it can be:
'Unlocked Manually Today at 8:39:00 AM User Code=0'
or:
'Locked using Z-Wave Today at 8:40:59 AM User Code=0'
Here are all of the components:
frontdoor_lock_log_message:
friendly_name: "Message"
value_template: '{{ states("sensor.front_door_lock_logging").split(" Today ")[0] }}'
frontdoor_lock_log_user:
friendly_name: "User"
value_template: '{{ states("sensor.front_door_lock_logging").split("User=")[1] }}'
frontdoor_lock_log_date:
friendly_name: "Date"
value_template: '{{ as_timestamp(states.sensor.front_door_lock_logging.attributes["last_change"]) | timestamp_custom("%A %b %d") }}'
frontdoor_lock_log_time:
friendly_name: "Time"
value_template: '{{ as_timestamp(states.sensor.front_door_lock_logging.attributes["last_change"]) | timestamp_custom("%H:%M:%S") }}'
m0wlheld
(Christoph Dahlen)
February 25, 2021, 8:56am
2
Regular Expressions FTW!
frontdoor_lock_log_message:
friendly_name: "Message"
value_template: '{{ states("sensor.front_door_lock_logging") | regex_replace("\s{2,}.*$","") }}'
Removes everything after and including two (or more) spaces.
frontdoor_lock_log_user:
friendly_name: "User"
value_template: '{{ states("sensor.front_door_lock_logging") | regex_replace("^.*Code=","") }}'
Removes everything and including “Code=”, effectively leaving just the code inplace.
1 Like
Hellis81
(Hellis81)
February 25, 2021, 8:57am
3
Regex is a really good for this.
{% set str = 'Unlocked Manually Today at 8:39:00 AM User Code=0' %}
{{str}}
message: {{ str | regex_findall_index('(.*)\s\s') }}
User {{ str | regex_findall_index('User Code=(.*)') }}
Date {{ str | regex_findall_index('Today|\d{4}-\d{2}-\d{2}') }}
Time {{ str | regex_findall_index('at\s(.*)\sUser') }}
It works on all the test strings you presented.
Just replace str with the entity
1 Like
dbrunt
(Daniel)
February 25, 2021, 9:28am
4
m0wlheld:
Regular Expressions FTW!
frontdoor_lock_log_message:
friendly_name: "Message"
value_template: '{{ states("sensor.front_door_lock_logging") | regex_replace("\s{2,}.*$","") }}'
Removes everything after and including two (or more) spaces.
Thank you @m0wlheld and @Hellis81 ! You both targeted the double space which I missed!. Sometimes you can’t see the forest for the trees! I should be able to use this:
value_template: '{{ states("sensor.front_door_lock_logging").split(" ")[0] }}'
I will keep read up on regex
and keep in mind for future reference…
1 Like