Get value out of object dictionary - Sonoff SWV

Hi, so I have Sonoff SWV valve, and it has an atribute which doesn’t separete it’s values, but presents those as:

{‘current_count’: 0, ‘irrigation_capacity’: 0, ‘irrigation_interval’: 0, ‘total_number’: 0}

I would like to get a specific value out of that to show in UI.

I’ve tried “from_json”, tried playing with dictionaries etc etc … but I can’t get any value.

Attributes can be non-string data types, so from_json is likely unnecessary.

{{ state_attr('your_entitiy','your_attribute')['total_number'] }}

Note that templating is rarely supported in dashboard cards, so you will likely need to create a Template sensor to hold the value.

so:

{{state_attr('sensor.garden_drip_hose_valve', 'cyclic_quantitative_irrigation')}}

returns null on it’s own.

{{states('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation')}}

gives me:

{
  "current_count": 0,
  "irrigation_capacity": 0,
  "irrigation_interval": 0,
  "total_number": 0
}

But:

{{states('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation')['current_count']}}

gives:

'str object' has no attribute 'current_count'

and finally:

{{state_attr('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation', 'current_count') }}

returns:

null

Also, this is how the value presents it self in device page:

So actual atribute is a json. and I would like to get a value out of this json.

ps. I know what templating in ui entails, and my plan is to get a helper sensor just to get the values out. ATM I’m just trying to get the values in developer tools section just to get going.

The state is a json string… not one of the state attributes. So, you need to use the states() function followed by from_json

{{ (states('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation')|from_json)['current_count']}}

result:

ValueError: Template error: from_json got invalid input '{'current_count': 0, 'irrigation_capacity': 0, 'irrigation_interval': 0, 'total_number': 0}' when rendering template '{{ (states('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation')|from_json)['current_count']}}' but no default was specified

And before you think - I’m not picking on you, I’m just pulling my hair because i tried so many things and it doesn’t make sense.

It’s the single quotes… from_json requires double quotes in the json string. I didn’t notice them until you posted the ValueError text.

{% set x = states('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation').replace('\'','"') %}
{{ (x|from_json)['current_count']}}

Yep, that one does the job, I was actually looking into string replace when you typed the anwser. My one is a bit simpler:

{{ (states('sensor.garden_drip_hose_valve_cyclic_quantitative_irrigation').replace( '\'', '"' )|from_json)['current_count'] }}