HASS version 0.84.5 running and “in principle” everything runs fine but I’ve been having a hell of a time trying to turn switches on/off on a schedule (time based, every day). I’ve read the forum up and down and the following seems to have worked for most people:
If that doesn’t work, post the output of states('input_datetime.switch_on') from the template editor and I can help you build an equation that will work.
I have a similar question, but instead of a trigger I want to use the input time in a condition so that the automation does not fire before or after a certain time. Can I use something like
No because the 2 objects are strings. You need to convert them both into numerical values. The simplest way to get there would be this (without getting into crazy formatting).
But be warned. This will trigger every minute (on the minute) after the datetime has been reached. A safe way to approach the situation would be to add a timerange:
- platform: template
value_template: >
{% set trigger_time = states.input_datetime.time_off.attributes.timestamp | int %}
{% set offset = 30 %}
{{ trigger_time - 30 <= states.sensor.time.last_updated <= trigger_time + 30 }}"
This essentially creates a minute window (that starts on the 30 second mark) in which the event can occur. It should only fire once.
Thanks, @petro! As I want to limit my automation ot certain times, I will not use the time as trigger but only as a condition, so the problem with firing every minute should not occcur. Thanks for the example though!
I just changed it according to your first example and at least it throws no errors so far, I think there was still an unneeded “(” though.
One thing I noticed after asking (at least if I’m not mistaken): Conditions do not use “platform”.
So all in all it looks like this: - condition: template value_template: "{{ states.sensor.time.last_updated >= states.input_datetime.time_off.attributes.timestamp | int }}"
Yes conditions don’t use platform. For the most part, the word ‘platform’ is replaced with ‘condition’ for conditions. This works for most platform → condition. It does not work for all of them, so it’s typically best to consult the condition documentation.
I just tested it and it is not working (yet). Trying to narrow it down I did some testing and removed conditions/triggers, but it’s still not working in this simplified form. The trigger and the action both work in other automations, so I guess I still have an error in the conditions…
- id: coffee
trigger:
- entity_id: device_tracker.phone
platform: state
to: home
condition:
- condition: state
entity_id: 'input_boolean.coffee'
state: 'on'
- condition: state
entity_id: 'binary_sensor.workday_sensor'
state: 'on'
- condition: template
value_template: "{{ states.sensor.time.last_updated >= states.input_datetime.time_workday_on.attributes.timestamp | int }}"
- condition: template
value_template: "{{ states.sensor.time.last_updated < states.input_datetime.time_workday_off.attributes.timestamp | int }}"
action:
- service: switch.turn_on
entity_id: switch.coffee
Previously, I also had the conditions nested in an “and”, but according to the webinterface “condition: and” is not supported. Is that correct?
Also, if you are using this as a condition, you don’t even need to use the states.sensor.time. That’s only needed for triggers and template sensors. You can just use now()
Thank you so much! An example like this be in the docs (if it’s in there, it’s hidden quite well I would say ).
It is still not working though. Do you have an example what now().timestamp() looks like as a value? Is it given as UNIX timestamp? Looking at the entities I noticed that the inputs are specified as “seconds of the day” (I defined them to be w/o date). Would now().second give me the same “format” (e.g. would it be 18000 or 0 at exactly 5 am)?
It stops working when I add the second time. But if I change the comparison to “>=” as well instead of “<” (just for fun), the automation is triggered, even if “now” is 20:20 and time_workday_off is set to 22:00. So (just guessing) I would say “now” is UNIX time and my inputs are “seconds of the day”.
I don’t think this is “the way to go”, but I finally got it to work the way I wanted (not very handy though):
- platform: template
sensors:
vacation_sensor:
friendly_name: 'Vacation Sensor'
entity_id: sensor.time_date
value_template: >
{% if state_attr('input_datetime.vacation_departure', 'timestamp') | int <= now().timestamp() and state_attr('input_datetime.vacation_arrival', 'timestamp') | int > now().timestamp() -%}
On Vacation!
{% elif state_attr('input_datetime.vacation_arrival', 'timestamp') | int <= state_attr('input_datetime.vacation_departure', 'timestamp') | int -%}
Error!
{%- else -%}
Not On Vacation
{%- endif %}
icon_template: >
{% if state_attr('input_datetime.vacation_departure', 'timestamp') | int <= now().timestamp() and state_attr('input_datetime.vacation_arrival', 'timestamp') | int > now().timestamp() -%}
mdi:airplane
{% elif state_attr('input_datetime.vacation_arrival', 'timestamp') | int <= state_attr('input_datetime.vacation_departure', 'timestamp') | int -%}
mdi:alert-circle
{%- else -%}
mdi:home
{%- endif %}
Then use it in node red as a condition.
I also have it notify me every 2 minutes if there’s an error as the sensor gets updated every minute and I could miss it and reverse the dates accidentally:
First I want to say above looks great! Immediately started using it when I found it
I only wondered how you made the date / time notation on arrival and departure.
It looks very different in my lovelace view. (I have the text in Dutch)
Hey @klaasnicolaas
Glad you’re using it!
I’m not sure what you mean exactly. Do you mean how I got it to show both date and time?
If so, you have to make sure that has_date and has_time are both set to true: https://www.home-assistant.io/components/input_datetime/
Hope that helps.
I mean the date format, in yours the sensor is written out as mm-dd-yyyy
In my overview the format is: yyyy-mm-dd I wondered how I can get this adjusted.