Timestamp display problem

My device returns mqtt JSON that looks like this:

{"time":1551290020, "house":"on", "boiler":"on", "delaytil":1551290500}
{"time":1551290500, "house":"on", "boiler":"on", "delaytil":0}

Note the “delaytil” value is either a time-stamp or zero.

I have defined the following sensor which displays perfectly when “delaytil” is a timestamp. Unfortunately, it displays a bogus value when the timestamp is zero.

How can I modify the value_template so that it displays either either nothing or fixed text like “n/a” when the time stamp is zero?

######### Boiler DelayTil
  - platform: mqtt
    state_topic: "faraday/power"
    name: boiler_delaytil
    value_template: "{{ value_json.delaytil | timestamp_custom('%I:%M%p') }}"

here is an example of an if statement https://www.home-assistant.io/components/sensor.template/#change-the-icon
with this it should be possible to catch the 0 case

value_template: >
  {% if value_json.delaytil != 0 %}  
    {{ value_json.delaytil | timestamp_custom('%I:%M%p') }}
  {% else %}
    n/a
  {% endif %}

That should work if the value_json.delaytil is a real number. If not the add " | int" after it in the first if statement:

{% if value_json.delaytil | int != 0 %}

1 Like

That worked like a charm … delaytil was an unsigned long, so I did not need the second part for this solution, but I appreciate it because I think it will help me solve another problem.

Thanks a TON.