Is it possible, instead of using this automation which flicks on an input boolean, to use a template sensor? I think the latter option is more elegant because
a) it doesn’t rely on one chance for toggling the input boolean (e.g. if HA was being rebooted at 17:00, the boolean would remain off).
b) it would be much cleaner code wise, I wouldn’t need two automations and an input boolean, just one template sensor.
Thanks, I tried that before posting and I just get: Error rendering template: UndefinedError: 'str object' has no attribute 'timestamp'
Besides, and I wasn’t massively clear, here, I’m looking for a template sensor which would be true from 5pm the day before until say midday on the day.
It’s for bin collections you see.
Thanks so much for your hard work. I wonder if there might be some problem with my version of HA (quite old, now, 0.92.1) or my specific install because I still just get the “unknown error” warning.
Also I’m trying (separately) to use a few of tenpole21’s sensors which use now() and for some reason his sensors must work, whereas mine never update, e.g.
So maybe my hass is just screwy!?
forecast_3:
friendly_name: "Forecast 3 Day"
value_template: >-
{%- set date = as_timestamp(now()) + (3 * 86400 ) -%}
{{ date | timestamp_custom("%A") }}
The sensor you posted used now() this NEVER updates itself.
You either need to set an automation to do it say with a time_pattern - minutes: ‘/10’ (look it up in the docs)
Or you reformat it using sensor.time and ensure you have that integration set up (again, look it up)
Have you tried it in the template editor to see what is needed to fix it?
and for the second one as @Mutt was saying (kind of… ) is that sensors need an entity to watch for a change in to cause an update. “now()” isn’t an entity, it’s a function. So you need to give it an entity to watch for a change. the easiest is to add the time sensor which updates every minute.
forecast_3:
friendly_name: "Forecast 3 Day"
entity_id: sensor.time
value_template: >-
{%- set date = as_timestamp(now()) + (3 * 86400 ) -%}
{{ date | timestamp_custom("%A") }}
but that assumes you have the date & time sensor set up. If not see the integration docs.
Just for the benefit of anyone coming across this thread in the future, this works:-
binary_sensor:
- platform: template
sensors:
bins:
value_template: >
{% set d = (as_timestamp(strptime(states('sensor.bin'), '%d/%m/%y')) - 24*60*60) | timestamp_custom('%Y-%m-%d') %}
{{ states('sensor.date_time') == d ~ ', 17:00' }}
it briefly (for the minute of the time being exactly 17:00) flicks on that binary sensor. What I am still struggling with is how I would turn it on at 5 and then shut it off at midday the next day without having a pair of automations controlling an input boolean. I CAN do that but there’s probably a simpler way?!
Thanks
You have tested the start (nearly), so just change it from == to >= then add an ‘or’ to check its < ‘12:00’
All done in the same template in the same binary sensor
Sorry, I’m doing this on a phone.
Because you are doing this over 2 separate dates you need to ‘and’ each element with the day concerned.
So Monday and after 17:10
Or
Tuesday and before 12:00
So, a little more thought …
the problem was that your test is date specific and that’s a fairly unusual test (but it really shouldn’t be)
So I have two solutions (having issues with the second so will post that later) : -
Simply test the two bracket conditions but have before the before OR after the after, then invert it (so it becomes between) : -
binary_sensor:
- platform: template
sensors:
bins:
value_template: >
{% set d = (as_timestamp(strptime(states('sensor.bin'), '%d/%m/%y')) - 24*60*60) | timestamp_custom('%Y-%m-%d') %}
{% set z = as_timestamp(strptime(states('sensor.bin'), '%d/%m/%y')) | timestamp_custom('%Y-%m-%d') %}
{{ not (states('sensor.date_time') < d ~ ', 17:00' or states('sensor.date_time') > z ~ ', 12:00') }}