Template binary_sensor not working

I’m scratching my head with this one and can’t make sense of why it’s not working.
I have a template binary_sensor that I use for an alert in case I stop receiving data from one of my sensors.
For some reason the sensor’s status is always off. Here is the sensor

- platform: template
  sensors:
    currentcost_lost:
      value_template: >
        {%- if (as_timestamp(now()) - (as_timestamp(states.sensor.currentcost_power.last_updated) | int)) > 300 -%}on{%- else -%}off{% endif %}
      friendly_name: CurrentCost Lost

Now if I enter that value_template {%- if (as_timestamp(now()) - (as_timestamp(states.sensor.currentcost_power.last_updated) | int)) > 300 -%}on{%- else -%}off{% endif %} in the dev tools, I get the value on yet the binary_sensor returns the value off.

What is it that I’m missing?

Missing a dash

value_template: >-

Thanks for the quick reply, but that doesn’t appear to be it:

    currentcost_lost:
      value_template: >-
        {%- if (as_timestamp(now()) - (as_timestamp(states.sensor.currentcost_power.last_updated) | int)) > 300 -%}on{%- else -%}off{% endif %}
      friendly_name: CurrentCost Lost

I’ve also tried this, same issue:

    currentcost_lost:
      value_template: '{%- if (as_timestamp(now()) - (as_timestamp(states.sensor.currentcost_power.last_updated) | int)) > 300 -%}on{%- else -%}off{% endif %}'
      friendly_name: CurrentCost Lost

Any further ideas?

Try

        value_template: >-
           {%- if (as_timestamp(now()) - as_timestamp(states.sensor.currentcost_power.last_updated)) | int > 300 -%}
             on
           {%- else -%}
             off
           {%- endif -%}

Thanks, but no, that still doesn’t work :frowning:

What do you mean? What is the output that you obtain in dev tools or any errors?

binary_sensor shows state as off


yet dev tools shows the correct value:

OK…that is a different issue. The template is not rendered on every update and hence the binary_sensor stays at off. You should use an input_boolean along with an automation to set it on/off based on the template.

Thanks, though I’m a bit confused.
I’m trying to generate an alert if a sensor has not been updated in >300 sec.
As I can’t use templates in alerts, I’ve created a template binary sensor to circumvent this, as per the docs.
you mean to say I need to create an input_boolean on top of this?

couldn’t you use the timing template in the automation? I have something similar but placed the trigger in the automation instead of the sensor.

- alias: 'Notify - Critical Device Offline'
  id: '1511602478031'
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: 'binary_sensor.cv_garage,sensor.cv_garage_state,
                binary_sensor.cv_stookhok, sensor.cv_stookhok_state,
                binary_sensor.cv_zolder,sensor.cv_zolder_state,
                binary_sensor.zonneboiler_zolder, sensor.zonneboiler_zolder_state,
                binary_sensor.freezer_bijkeuken, sensor.freezer_bijkeuken_state,
                binary_sensor.asus_router, binary_sensor.solaredge'
    from: 'on'
    to: 'off'
    for:
      minutes: 5
  condition:
    condition: template
    value_template: >
      {% if states.automation.notify__critical_device_offline.last_triggered is not none %}
        {% if as_timestamp(now()) | int - as_timestamp(states.automation.notify__critical_device_offline.attributes.last_triggered) | int > 100 %} true {% else %} false
        {% endif %}
        {% else %}
        false
        {% endif %}
  action:
    - service: notify.marijn
      data_template:
        title: "System Message On->Off!"
        message: '{{ as_timestamp(now()) | timestamp_custom("%X") }} : {{ trigger.to_state.attributes.friendly_name }} has gone offline. Check status to restore functionality.'
    - service: persistent_notification.create
      data_template:
        message: 
          'System Message: {{ as_timestamp(now()) | timestamp_custom("%X") }}: {{ trigger.to_state.attributes.friendly_name }} has gone offline. Check status to restore functionality.'

What I mean is that binary_sensor will not work for what you are trying to accomplish.

Actually, if you just want to create an alert if the sensor is not updated for 300 seconds, you don’t even need a sensor. Just use an automation directly

Thanks but that won’t work. The sensor sends power consumption data so I just need to check if I no longer receive data.

Thanks, so create a trigger based on time then? say run it every minute and add a condition to turn an alert on?

Yes…that will work.

Alternately, you can also try creating a template trigger (untested):

  trigger:
    platform: template
    value_template: "{{(as_timestamp(now()) - as_timestamp(states.sensor.currentcost_power.last_updated)) | int > 300}}"
1 Like

Thanks, I’ll try that tomorrow. Time to go to bed now :slight_smile:
Thanks for your help :+1: