Template sensor instead of using a boolean?

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.

  - alias: Turn_on_bins_icon
    trigger:
      platform: template
      value_template: >
        {% set d = (strptime(states('sensor.bin'), '%d/%m/%y').timestamp() - 24*60*60)|timestamp_custom('%Y-%m-%d') %}{{ states('sensor.date_time') == d ~ ', 17:00' }}
    action:
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.bins

I can’t figure out how to turn this template into a different kind of template.

Thanks

binary_sensor:
  - platform: template
    sensors:
      bins:
        value_template: >
          {% set d = (strptime(states('sensor.bin'), '%d/%m/%y').timestamp() - 24*60*60)|timestamp_custom('%Y-%m-%d') %}{{ states('sensor.date_time') == d ~ ', 17:00' }}
1 Like

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.

assuming your sensor.bin can be converted to a valid datetime object using strptime the this should be the template:

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' }}

Thank you! I tried this, but the sensor remains ‘off’ and when I put it into the template editor it either says

Error rendering template: TemplateSyntaxError: unexpected '%'
or
Unknown error rendering template

sensor.bin looks like 24

try this to see how it works:

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

I don’t have your sensor.bin so I had to do some finagling in the template editor but that format worked for me:

Thanks so much for your hard work. :slight_smile: :slight_smile: 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. :frowning: :frowning:

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)

post what you have now.

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… :wink:) 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.

instead of sensor.time you can use sun.sun , this does not need an extra integration to be set up.

Assuming you have already set up the sun: integration. :stuck_out_tongue_winking_eye:

1 Like

Is that not setup using default_config: ?

Probably. But not everyone has this configured. I haven’t.

Hmmm !
Difficult to say which I’d do first as they are both vital to the way I control my lights

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

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') %}
          {{ states('sensor.date_time') >= d ~ ', 17:00' or states('sensor.date_time') < z ~ ', 12:00' }}

Note: your yaml spacing in the template is a bit off, less critical for templates but good habits should see yaml spacing in “two’s”

EDIT: The above will ALWAYS be true, see below

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

I’ll Re write this when I get home

1 Like

Wow, thank you!

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') }}