frelev
February 16, 2017, 2:41pm
1
I was trying to be efficient and avoid to create two automations.
Basically I want to trigger two different shell commands depending on trigger time.
I can´t get it to work however:
automation:
- alias: "Send Message to Kitchen"
trigger:
- platform: time
after: '08:00'
- platform: time
after: '20:00'
action:
- service_template: shell_command.{% if (trigger.now, '08:00') %}
play_morning_greeting_in_kitchen
{% else %}
play_evening_greeting_in_kitchen
{% endif %}
Anyone got a clue how to set this up?
PhyberApex
(Phyber Apex)
February 16, 2017, 2:58pm
2
I am not expierenced with datetime to give you a complete solution but it would be something like this.
service_template: >
{% if trigger.now [IS CLOSE TO 8.00] %}
shell_command.play_morning_greeting_in_kitchen
{% else %}
shell_command.play_evening_greeting_in_kitchen
{% endif %}
Please take a look here https://home-assistant.io/topics/templating/ and don’t forget you can try out templates in the dev-console on your HA website.
PS: If this stays open long enough I might give it a try to solve it completly.
~Cheers
frelev
February 16, 2017, 3:33pm
3
Thank you, I´ll experiment with this.
At first try I got an type error
unorderable types: datetime.datetime() < float()
Seems like trigger.now is datetime
EDIT: {% if trigger.now|float < 9.00 %} seems to work
EDIT2: spoke too soon. It seems that it always trigger the morning script
1 Like
frelev
February 16, 2017, 7:08pm
4
@PhyberApex : How can I see trigger.now in dev-console?
This is apparently true when time is 20:00:
trigger.now|float < 9.00
PhyberApex
(Phyber Apex)
February 16, 2017, 8:01pm
5
Just using now() should give you a current date time object.
~Cheers
rpitera
(Robert Pitera)
February 16, 2017, 8:20pm
6
If I float now() in the template tester, I get 0.0.
PhyberApex
(Phyber Apex)
February 16, 2017, 10:12pm
7
So this should do the trick for you…
service_template: >
{% if trigger.now.time().hour == 8 %}
shell_command.play_morning_greeting_in_kitchen
{% else %}
shell_command.play_evening_greeting_in_kitchen
{% endif %}
As you can see this makes the assumption that it will trigger within the hour which should be fine in your case.
If you wonder how I found out how to do this:
I went into the template tester in the dev console and produced a datetime object to test around with according to the templating guide “now()” produces a datetime for now. From there I tried a few template functions until realizing that datetime is a python object which has attributes so I decided to look it up and found the way to access the hour with “datetimeobject.time.hour”. I provided my way of finding the solution in case you need something similar in the future to provide you with a way of thinking
~Cheers
3 Likes
frelev
February 17, 2017, 9:22am
8
Thank you for digging into this, I´ll try your code
So trigger.now.time.hour is 24h clock then? Else 8 AM/PM would both show 8?
Do you mind sharing your code for testing in dev-console? Can be useful for me and others.
PhyberApex
(Phyber Apex)
February 17, 2017, 10:15am
9
Hey,
yes it is 24h as can be seen here (forgot to link): https://docs.python.org/2/library/datetime.html#datetime.datetime.hour
My code I had in the dev console was:
{% set d= now() %}
{{ d }}
{{ as_timestamp(d) }}
{{ d.time().hour }}
PS: I edited the solution as I forgot the parenthesis after time
~Cheers
frelev
February 17, 2017, 11:04am
10
Thank you, much appreciated.
I´ve marked your post as solution
1 Like