Not sure if you can use state_attr this way, I think it needs to be in the form state_attr('sensor.my_sensor', 'attr') so you then can do state_attr('sensor.my_sensor', 'attr') in ['Unknown', 'Unavailable', 'None']. In your case it might be more appropriate to use instead:
{% set val = value_json[0]['lastKnownState']['batteryChargeState'] %}
{% if val in ['Unknown', 'Unavailable', 'None']) %}
...
value_template: >
{% set val = value_json[0]['lastKnownState']['batteryChargeState'] %}
{% if val in ['Unknown', 'Unavailable', 'None'] %}
0
{% else %}
val
{% endif %}
i think i know what the problem is… when i start home assistant, until it does the first check, it doesnt display any value so it doesnt fulfil the condition
Even with this
value_template: >
{% set result = 0 %}
{% set val = value_json[0]['lastKnownState']['batteryChargeState'] %}
{% if val is number %}
{% set result = val %}
{% endif %}
{{result}}
Still returns unknown, so there should be a scenario i am not covering here. Have you seen that before?
Oh do I I have a template sensor that on reboot does return despite my best efforts to check for None, etc and out of regular range value which messes up my history plot which autoscales. I think my last suggestion to cast with a default value in case of failure should capture this for you, though.
But check also if it is not lower case unknown/unavailable that is returned
Already tried that, and I saw it doesn’t even get in the if. Would be good to be able to debug that code and see the real value, don’t know if it’s possible