Template help - If an entity is not in the states

I use this template for a sensor that tracks Google Home alarms. But sometimes my alarms and timer entities are hidden/not shown in states instead of showing “unavailable”.

         {% if (states('sensor.bedroom_speaker_alarm')) != "unavailable" %}
                {{ (as_timestamp(now()) - 
                as_timestamp(strptime(states('sensor.bedroom_speaker_alarm'), '%Y-%m-%dT%H:%M:%S+01:00')))|int }}
            {% else %}
                0
            {% endif %}

When there’s no entity, I get logs with errors every minute. Is there any way to prevent the errors in case the entities go missing again?

P.S. entities sometimes go missing after restarting Hassio. When I set an alarm or timer, they show up in the states again.

This works in Dev Tools/templates

{% if states.sensor.bedroom_speaker_alarm %}
  sensor is available
{% else %}
  sensor unavailable
{% endif %}

Thanks, do you think that I can substitute (states('sensor.bedroom_speaker_alarm')) != "unavailable" and the sensor will still work? If an alarm is set, it shows how many seconds left until alarm triggers and if no alarm is set (=unavailable), it shows 0. I can test it (and set up an alarm) at the weekend.