mmcpro
July 31, 2020, 11:01am
1
Hi, I have the following python script, and it doesn’t work. It would display nothing in lovelace for the input_text.alarm_sensor.
sensor_name = 'AA'
time_now = datetime.datetime.now().strftime("%d %b %y, %I:%M:%S %p")
alarm_name = sensor_name + time_now
hass.states.set('input_text.alarm_sensor', alarm_name)
If I use the following, it would display the text correctly.
sensor_name = 'AA'
alarm_name = sensor_name
hass.states.set('input_text.alarm_sensor', alarm_name)
How could I add the datetime information to the string?
petro
(Petro)
July 31, 2020, 12:01pm
2
Can I ask what your end goal is with this script?
mmcpro
September 7, 2020, 3:33am
3
When an alarm is triggered via automation (PIR sensor), I want to display on lovelace which sensor and time of trigger.
mmcpro
September 18, 2020, 11:03am
4
In case someone is searching for solution to similar problem, this is what I found and fixed.
This didn’t work:
time_now = datetime.datetime.now().strftime("%d %b %y, %I:%M:%S %p")
Reason: strftime
cannot be used this way.
Correct way is to use it separately from datetime.datetime.now()
.
This works:
get_time = datetime.datetime.now()
time_now = get_time.strftime("%d %b %y, %I:%M:%S %p")
Hope this helps.
petro
(Petro)
September 18, 2020, 11:52am
5
Next time, when you reply, use the reply in the image and a responder will get a notification. If you use the reply at the bottom, the responder will not get a notification.
Also, you don’t need to do that if you wanted to keep a single line.
get_time = datetime.datetime.strftime(datetime.datetime.now(), "%d %b %y, %I:%M:%S %p")
Lastly, you could have just changed input_text.alarm_sensor without using a python script, which is why I was asking your end goal intent. I didn’t get a notification to your response because you used the ‘thread respond’ button instead of the ‘respond to person’ reply button.
- service: input_text.set_value
data_template:
entity_id: input_text.alarm_sensor
value: >
{{ trigger.to_state.name ~ ' ' ~ now().strftime("%d %b %y, %I:%M:%S %p") }}
mmcpro
September 18, 2020, 3:25pm
6
That single line code simplifies it even further. Thanks.
The sensor has an RF value like 0AEAD6
, and I change it to something like Front Gate
. There are over 20 sensors, so I use python script IF to check and assign name. Then I append current time.
Snippet of code:
sensor_state = hass.states.get('sensor.rf_bridge').state
sensor_time = datetime.datetime.strftime(datetime.datetime.now(), "%d %b %y, %I:%M:%S %p")
sensor_name = ''
if sensor_state == '0AEAD6':
sensor_name = 'Front Gate'
if sensor_state == 'FF8006':
sensor_name = 'Front Door'
if sensor_state == 'CA5006':
sensor_name = 'Rear Door'
if sensor_name:
hass.services.call('input_text', 'set_value', { 'entity_id': 'input_text.alarm_sensor', 'value': sensor_name + ' - ' + sensor_time })
petro
(Petro)
September 18, 2020, 3:31pm
7
Ah ok, this can still be done in templates:
Here’s a full automation
- alias: Set input text from PIR
trigger:
- platform: state
entity_id: sensor.rf_bridge
action:
- service: input_text.alarm_sensor
data_template:
entity_id: input_text.alarm_sensor
value: >
{% set sensor_state = trigger.to_state.state %}
{% set mapper = {
'0AEAD6': 'Front Gate',
'FF8006': 'Front Door',
'CA5006': 'Rear Door',
} %}
{{ mapper[sensor_state] if sensor_state in mapper else 'Error' }} {{ now().strftime("%d %b %y, %I:%M:%S %p") }}
Simply add to the 'xxx': 'Sensor Name',
dictionary and you’ll cover all your states.
1 Like
mmcpro
September 19, 2020, 5:20am
8
It’s almost perfect. Is there a way to not run (not change value) when sensor_state
not in mapper
?
In my python script, it has IF
that checks the matching and will only set value if it does.
I really hope I can get rid of this python script. This is the only python script in my HA setup.