#I just ran a couple tests:

1 messages · Page 1 of 1 (latest)

dreamy lion
#

I mean, that IS the problem

#

The result isn't defined, so when I try to reference that data, it can't

worn flint
#

so the proper way to do it is with an if/then statement checking if value_json is defined but you said that doesn't work. Based on what I'm seeing, it should work.

dreamy lion
#

I completely agree

#

This is the main one I tested:
value_template: "{{ value_json.packs[0]['c3'] | replace('*','') if value_json is defined else states('sensor.ldpp_p1c3') }}"

worn flint
#

What I'm not able to replicate is a sensor that goes unavailable. Before you added the if/then statement, you are saying the sensor went unavailable when the server didn't respond?

dreamy lion
#

Let me make a pastebin real quick

#

So you can see the different things I'm trying

#

This is me pulling data from some batteries and then serving that up via a flask server running on a pi

worn flint
#

are your sensors resulting in unknown or do they become unavailable

dreamy lion
#

Unknown

worn flint
#

ah

#

might try this just to see if it avoids the 'unknown' problem:

value_template: "{{ value_json.packs[0]['c3'] | replace('*','') if value_json is defined else 1 }}"
dreamy lion
#

Why would it, though?

worn flint
#

I'm wondering if querying the state object of the sensor during the rendering of the template is causing an issue.

dreamy lion
#

It's the if value_json is defined that doesn't seem to do anything

#

Hmm

worn flint
#

that's what I'm not getting because it is absolutely doing something in my testing. Unless you are having a different issue than what I am replicating

dreamy lion
#

Well, I threw something in there to test that theory. Unfortunately, I can't get it to malfunction on command

worn flint
#

do you know what the server is doing when it malfunctions? Is it responding with data that is not JSON, or is it not responding at all?

dreamy lion
#

500 error I think

#

Sometimes it seems like it'll just get part of the data, so it's incomplete and can't parse it.

#

Not really able to find a way to gracefully handle that, either

worn flint
#

I think the issue you are having is value_json being populated but it doesn't have a packs key with a list as its value

#

I would recommend creating two temporary sensors so you can see what the value and value_json is being populated with. Then, when your real sensor goes unknown you can see what the value of these two sensors are, and you can debug your template

#
  - name: "LDPP_test_rest_value"
    unique_id: test_rest_sensor_value
    value_template: "{{ value }}"
  - name: "LDPP_test_rest_value_json"
    unique_id: test_rest_sensor_value_json
    value_template: "{{ value_json }}"
#

I've got to log off, but once you have these sensor values recorded during a time of your other sensor being "unknown" post this and it should be easy to debug from that point

dreamy lion
#

Those test values just show up as "unknown"

worn flint
#

All the time?

dreamy lion
#

No, just when the json isn't properly retrieved

worn flint
#

I can't replicate any url that makes the sensor go into an "unknown" state (even using the httpstat.us service to return 500 or 404 or other error codes)

#

it either returns a value or "unavailable"

worn flint
#

look in your logs to see what error shows up, you should see
ERROR (MainThread) [homeassistant.helpers.entity] Failed to set state for sensor. ...

worn flint
#

try this:

  - name: "LDPP_test_rest_value"
    unique_id: test_rest_sensor_value
    value_template: "{{ value[:255] }}"
#

that will take the first 255 characters if the value returned is too long

dreamy lion
#

Oh yeah, I see this for one of the tests:

Source: helpers/entity.py:1129
First occurred: February 1, 2024 at 6:40:20 AM (44611 occurrences)
Last logged: 10:17:24 AM

Failed to set state for sensor.ldpp_test_rest_value, fall back to unknown
Failed to set state for sensor.ldpp_test_rest_value_json, fall back to unknown
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1129, in _async_write_ha_state
    hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1807, in async_set
    state = State(
            ^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 1384, in __init__
    validate_state(state)
  File "/usr/src/homeassistant/homeassistant/core.py", line 204, in validate_state
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state with length 264. State max length is 255 characters.
#

I really just get a lot of these though: ```Logger: homeassistant.components.rest.util
Source: components/rest/util.py:37
Integration: RESTful (documentation, issues)
First occurred: January 31, 2024 at 8:07:41 PM (1388 occurrences)
Last logged: 10:15:25 AM

REST result could not be parsed as JSON```

#
Source: helpers/template.py:2318
First occurred: January 31, 2024 at 8:08:41 PM (16333 occurrences)
Last logged: 10:17:28 AM

Template variable error: 'value_json' is undefined when rendering '{{ value_json.packs[0]['c1'] | replace('*','') if value_json not in ['unavailable', 'unknown', 'none'] else states('sensor.ldpp_p1c1') }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.packs[0]['c2'] | replace('*','') if value_json not in ['unavailable', 'unknown', 'none'] else states('sensor.ldpp_p1c2') }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.packs[0]['c3'] | replace('*','') if value_json not in ['unavailable', 'unknown', 'none'] else states('sensor.ldpp_p1c3') }}'
Template variable warning: 'dict object' has no attribute 'state' when rendering '{{ value_json.state if value_json is defined else states('sensor.fwtp_battery_monitor') }}'
Template variable warning: 'value_json' is undefined when rendering '{{ value_json }}'```
worn flint
#

For the 'value_json' is undefined error, you need to add something to catch it when it isn't defined, for example:

{{ value_json.packs[0]['c1'] | replace('*','') if value_json is defined and value_json not in ['unavailable', 'unknown', 'none'] else states('sensor.ldpp_p1c1') }}'
#

you could change the test to this in order to see better what is happening:
{{ value_json[:255] if value_json is defined else "not defined" }}

dreamy lion
#

Making those changes now. Thanks for all the help. Sorry I've been so slow to reply. I didn't realize I didn't have notifications turned on for these side chats by default

worn flint
#

no worries, I didn't realize you could turn on notifications for threads, just saw that now 😅

dreamy lion
#

At one point I really wanted to use yaml anchors and create all these entities dynamically to cut down on all the lines of yaml

#

If you're curious as to what this actually all is

worn flint
#

yeah once you get it working you can do that

dreamy lion
worn flint
#

is this your solar/battery setup?

dreamy lion
#

Yeah. It's all off-grid for now

#

Weather is kinda garbage today, but we've got plenty of stored power

worn flint
#

that's pretty awesome. I'd like to do that some day. But having those batteries sitting out with minimal guarding makes me clench up a bit 😬 . I'm quite familiar with what large li-ion battery packs are capable of

dreamy lion
#

As long as you dont touch the red and black ends together things are generally safe

#

I did some unintentional arc welding once with a wrench

worn flint
#

lol

#

I am an engineer and am responsible for the design of the electric drivetrain (including battery) of some mining machines. We take a ton of precautions in our labs (and on our machines) when it comes to batteries. Have blown the steel lab doors off once during a test.

#

back to your json issues, one other error you showed was:

Template variable warning: 'dict object' has no attribute 'state' when rendering '{{ value_json.state if value_json is defined else states('sensor.fwtp_battery_monitor') }}'

to handle the error where value_json is populated but doesn't have the json key you expect, you need to use the get() function. Something like:
{{ value_json.get('state',['some default value'])[0] }}

dreamy lion
#

Oh I bet there's a ton I could learn from you

worn flint
#

my team is pretty large, so I unfortunately don't get to dive into the details of many aspects that I would otherwise want to get into. Like the code of our BMS. So when it comes to electric drive I'm more of a jack of all trades, master of none

dreamy lion
#

My BMS is a fork off an open source project that I modified to balance between 12 cell groups at a time

#

Just wish I could port the code to run on my pi natively instead of an arduino due

worn flint
#

what's wrong with running it off the arduino?

dreamy lion
#

Nothing-- it gets the job done. It's just one more piece of hardware I need to have running

worn flint
#

gotha

dreamy lion
#

Kinda would like more data than that

#

Seems to retrieve the data without much of an issue. Occasionally there is a 500 error

#

I shouldn't be serving this up with a flask server

worn flint
#

You can do the same with value and see if that is also not defined. It might be that the server is returning something but it can’t be parsed into json, so that value is defined but value_json is not

dreamy lion
#

Seems like no matter what it returns not defined

worn flint
#

You could change the logging to debug and see if the extra detail in the logs is helpful

dreamy lion
#

I may have to do that. It seems as if upon reboot if it isn't able to get a value then it just doesn't try again

worn flint
dreamy lion
#

I solved the 500 error and that has simplified things

worn flint
#

Nice- did you solve it on the server side?