Template sensor instead of using a boolean?

Thanks for this! I’ve inserted a ‘(’ after the “set z =” but I still get
Error rendering template: TemplateSyntaxError: unexpected '}', expected ')'
Could it be my version of Home Assistant? It’s 0.92.1. Can’t update any further for some reason.

Okay,
in the template editor what do you get for : -

{{ strptime(states('sensor.bin') }}
{{ as_timestamp(strptime(states('sensor.bin'), '%d/%m/%y')) }}

There should not be a bracket error as : -

{% set d = (as_timestamp(strptime(states('sensor.bin'), '%d/%m/%y')) - 24*60*60) | timestamp_custom('%Y-%m-%d') %}

is what you had, and

{% set z = as_timestamp(strptime(states('sensor.bin'), '%d/%m/%y')) | timestamp_custom('%Y-%m-%d') %}

is basically the same thing but without subtracting the day ???
:puzzled:

1 Like

Don’t bother using strings to do the comparisons. Use the timestamp because it makes the logic easier.

- platform: template
  sensors:
    bins_icon:
      entity_id: sensor.date_time
      value_template: >
        {% set bin_date = strptime(states('sensor.bin'), '%d/%m/%y') %}
        {% set after = as_timestamp(bin_date.replace(hour=17)) - 24*60*60 %}
        {% set before = as_timestamp(bin_date.replace(hour=12)) %}
        {{ after < now().timestamp() < before }}

EDIT: Just to clarify everything:

entity_id: sensor.date_time

forces an update every minute on the template sensor.


{% set bin_date = strptime(states('sensor.bin'), '%d/%m/%y') %}

Converts the timestring into a datetime object with the date day/month,2-character year at midnight. I.e. 0 oclock.


{% set after = as_timestamp(bin_date.replace(hour=17)) - 24*60*60 %}

makes a timestamp at hour 17 on bin day, then subtract 24 hours (in seconds).


{% set before = as_timestamp(bin_date.replace(hour=12)) %}

makes a timestamp at hour 12 on bin day.


{{ after < now().timestamp() < before }}

return true if the time is between hour 17 of the night before and hour 12 of today.

2 Likes

just wanted to say huge thanks to muttley and petro!!!

I have to say that @petro 's solution was where I was heading (and having difficulty with).
Given that two of the entities ‘have’ to go through a timestamp phase and they are ‘by definition’ numerically sortable. It is absolutely the way to go.
Regardless, I would never have come up with the .replace(hour bit
(I’d have been fumbling with - 7x60x60 and +12x60x60)

I was trying to get the text format for the date right ie YYYY-MM-DD, HH:MM (to finagle a text sort) but why bother?
Your bin sensor coming back with d/m/y distracted me. I have learnt something here and thank you both.

Interesting - despite this evaluation TRUE in the templates dev tool, it doesn’t result in the sensor flicking to ‘on’.

  - platform: template
    sensors:
      bins:
        value_template: >
          {% set bin_date = strptime(states('sensor.testbin_d'), '%d/%m/%y') %}
          {% set after = as_timestamp(bin_date.replace(hour=17)) - 24*60*60 %}
          {% set before = as_timestamp(bin_date.replace(hour=12)) %}
          {{ after < now().timestamp() < before }}

11 41 06

Looking at your template, I’m not surprised.
Do you have time_date set up in your config ?
See : - https://www.home-assistant.io/integrations/time_date/
Then put
entity_id : sensor.date_time above (but in line with) value_template: >
(Though sensor.time would do)

Ah ! … looking at Petro’s - just like his.
There is nothing in your template that HA feels the need to update

1 Like

Eh ?

10 char min

:man_shrugging:

Hey !

How does that count as 10 characters ? :smirk:

Lol, it must count the characters :man_shrugging: == :man_shrugging:

1 Like