sagilo
(Sagilo)
January 15, 2018, 6:29am
1
Hi
I get my timestamp from an event.
The timestamp arrives in UNIX time and I have no problem converting it to local time/date (example for printing it):
message: 'Alarm clock set {{ trigger.event.data.date_time_utc_sec | int | timestamp_local }}'
However, I want to set this date & time in input_datetime
object but I fail to do it
I’ve tried setting the timestamp
field and the date
field, both failed.
Here is how it looks now (and not working):
- service: input_datetime.set_datetime
entity_id: input_datetime.sagi_next_alarm_clock
data_template:
#timestamp: '{{ trigger.event.data.date_time_utc_sec | int }}' # not wroking
date: '{{ strptime((trigger.event.data.date_time_utc_sec | int | timestamp_local), "%Y-%m-%d") }}' # not working either
Log error:
2018-01-15 08:24:13 ERROR (MainThread) [homeassistant.core] Invalid service data for input_datetime.set_datetime: Could not parse date for dictionary value @ data['date']. Got '2018-01-15 07:33:10'
I understand that the strptime
isn’t working but I can’t figure out the correct syntax for this
Thanks !
sagilo
(Sagilo)
January 15, 2018, 8:09am
2
If anyone interested, got this to work:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.sagi_next_alarm_clock
date: >
{% set date_time = trigger.event.data.timestamp_unix | int %}
{{ date_time | timestamp_custom("%Y-%m-%d", true) }}
time: >
{% set date_time = trigger.event.data.timestamp_unix | int %}
{{ date_time | timestamp_custom("%H:%M:%S", true) }}
2 Likes
This helped me a lot, but I needed to make a few changes in order to get it to work:
In my configuration.yaml I have:
input_datetime:
sprinkler_last_runtime:
name: Sprinkler Stored Runtime
has_date: true
has_time: true
And my automation.yaml has this in it:
- alias: sprinkler run time
trigger:
platform: state
entity_id: input_boolean.sprinkler
from: 'off'
to: 'on'
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.sprinkler_last_runtime
time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
1 Like
Thank you all for sharing, it helps a lot, but I am stuck a bit.
I am trying to create an input_number (slider) that change an input_datetime hour part. The goal is to create a nice set timer in HADashboard. My thoughts are to use the slider (range 0-23) as on hour input and another slider (range 0-59) as minute input. I have been able with the code above to set the input_datetime to current time when changing the slider, however I do not understand how to change the value of the hour setting and use the input_number as input.
#from automation.yaml
- alias: slider_start_time
hide_entity: false
trigger:
platform: state
entity_id: input_number.start_hour
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.start_time
time: '{{(states.input_number.start_hour | timestamp_custom("%H", true)}}'
#time: '{{ (as_timestamp(now()) | timestamp_custom("%H:%M:%S", true)) }}'
date: '{{ (as_timestamp(now()) | timestamp_custom("%Y-%m-%d", true)) }}'
And this is obviously not working. Any hints regards how to set the input_datetime.start_time hour value to the same as the input_number.start_hour?
Came a bit longer. With this code it will change, but always to 1.00 AM independent on the value of the input_number.start_hour
time: '{{ (now().time().strftime("%H") == states.input_number.start_hour) | timestamp_custom("%H:%M:%S", true) }}'
I can see that the time stamp of the input_datetime.start_hour is set to 3600, so I tried to multiply with 3600 without success =(
time: '{{ (now().time().strftime("%H") == states.input_number.start_hour) | float * 3600 | timestamp_custom("%H:%M:%S", true) }}'
With some good help and custom_component variable I solved my problem. Probably not the easiest way to solve it but anyway working fine. Here is the code.
Configuration.yaml
input_number:
start_hour:
name: Start Timme
icon: mdi:timer
initial: 19
min: 0
max: 23
step: 1
start_min:
name: Start Minuter
icon: mdi:timer
initial: 0
min: 0
max: 55
step: 5
stop_hour:
name: Stopp Timme
icon: mdi:timer
initial: 20
min: 0
max: 23
step: 1
stop_min:
name: Stopp Minuter
icon: mdi:timer
initial: 0
min: 0
max: 55
step: 5
variable:
hour_calc_start:
value: 0
restore: false
min_calc_start:
value: 0
restore: false
time_calc_start:
value: 0
restore: false
hour_calc_stop:
value: 0
restore: false
min_calc_stop:
value: 0
restore: false
time_calc_stop:
value: 0
restore: false
input_datetime:
start_time:
name: Start
has_date: false
has_time: true
initial: '19:00'
stop_time:
name: Stopp
has_date: false
has_time: true
initial: '20:00'
Automation.yaml
- alias: slider_start_time
hide_entity: false
trigger:
- platform: state
entity_id: input_number.start_hour
- platform: state
entity_id: input_number.start_min
action:
- service: variable.set_variable
data:
variable: hour_calc_start
value_template: "{{ states('input_number.start_hour') | int * 3600 }}"
- service: variable.set_variable
data:
variable: min_calc_start
value_template: "{{ states('input_number.start_min') | int * 60 }}"
- service: variable.set_variable
data:
variable: time_calc_start
value_template: "{{((states('variable.hour_calc_start')|int) + (states('variable.min_calc_start')|int))}}"
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.start_time
date: "{{ now().date() }}"
time: "{{ states('variable.time_calc_start')|int|timestamp_custom('%H:%M:%S',False) }}"
- alias: slider_stop_time
hide_entity: false
trigger:
- platform: state
entity_id: input_number.stop_hour
- platform: state
entity_id: input_number.stop_min
action:
- service: variable.set_variable
data:
variable: hour_calc_stop
value_template: "{{ states('input_number.stop_hour') | int * 3600 }}"
- service: variable.set_variable
data:
variable: min_calc_stop
value_template: "{{ states('input_number.stop_min') | int * 60 }}"
- service: variable.set_variable
data:
variable: time_calc_stop
value_template: "{{((states('variable.hour_calc_stop')|int) + (states('variable.min_calc_stop')|int))}}"
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.stop_time
date: "{{ now().date() }}"
time: "{{ states('variable.time_calc_stop')|int|timestamp_custom('%H:%M:%S',False) }}"
polclota
(Pol Clota)
January 23, 2019, 9:39pm
8
This worked for me:
- alias: 'Calc timer trig time'
initial_state: 'on'
trigger:
- platform: state
entity_id: input_number.box1, input_number.box2, input_number.box3
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.time4
time: >
{% set n = as_timestamp(now()) %}
{% set b = states.input_number.box1.state|int*60*60|int+states.input_number.box2.state|int*60|int+states.input_number.box3.state|int %}
{% set r = n+b %}
{{ r|timestamp_custom("%H:%M:%S",True) }}
Hello i try to write the date of the day in an input_datetime.test every time an automatisation is triggered.
I try this:
service: input_datetime.set_datetime
data:
date: |
{ now() | timestamp_custom("%Y-%m-%d", true) }
entity_id: input_datetime.test
But don’t work.
Have anyone an idea why?
use data_template:
, instead of data:
second to that, use double {{ }}
versus the single you now use
thanks for your reply, but it don’t work.
I change it a little bit:
- service: input_datetime.set_datetime
entity_id: input_datetime.dernier_arrosage
data_template:
Date: >
{% set n = now().strftime('%Y-%m-%d') %}
{% set b = state('input_number.intervalle_arrosage').strftime('%d') %}
{{% n+b %}}
input_number.intervalle_arrosage is the number of day’s between two watering.
But don’t work
i make an step forward but i am stok again:
- id: '1597512639627'
alias: 4 - Arrosage auto
description: ''
trigger:
- entity_id: input_boolean.test
platform: state
condition: []
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.dernier_arrosage
date: >
{{ (now().timestamp() + (state('input_number.intervalle_arrosage') * 24 * 36001)) | timestamp_custom("%Y-%m-%d", true) }}
mode: single
without the :+ (state(‘input_number.intervalle_arrosage’)* 24 * 36001)) it give me the today date, but with it it doesn’t work.
What i want is to have the today date + the number in the input_number.intervalle_arrosage.
Can someone please help me?