Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cell Voltage/ Resistance #170

Open
3 tasks done
Meli73 opened this issue Feb 3, 2025 · 8 comments
Open
3 tasks done

cell Voltage/ Resistance #170

Meli73 opened this issue Feb 3, 2025 · 8 comments
Labels
question Further information is requested

Comments

@Meli73
Copy link

Meli73 commented Feb 3, 2025

Checklist

  • I need support with using the integration.
  • My issue is not a feature request
  • I'm not avoiding to fill out the bug report form.

Describe the issue

I have a question to this Template od the Manual:

_template:

  • sensor:
    • name: cell_voltage_0
      state: >-
      {{ state_attr('sensor.smartbat_...delta_voltage', 'cell_voltages')[0] }}
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >-
      {{ has_value('sensor.smartbat
      ...delta_voltage') }}

is there a (Jinja) syntax doing a loop over all cell voltages ? (so I do not need to copy this block 48 times in my system )

And is the Cell resistance also avaliable? Or should I add a Feature request for it ?

@Meli73 Meli73 added the question Further information is requested label Feb 3, 2025
@patman15
Copy link
Owner

patman15 commented Feb 3, 2025

Hi!

is there a (Jinja) syntax doing a loop over all cell voltages ?

Yes, have a look at line 17 of the example template in the template editor of the developer tools.

And is the Cell resistance also avaliable?

No, they are not available, they do not fit the current UI architecture and are specific to the JK BMS, so I'm quite hesitant to implement and maintain those. You can still look them up in the app by temporarily disabling the integration.

Best regards,
Patrick

@Meli73
Copy link
Author

Meli73 commented Feb 17, 2025

got it to this point:

`
template:
{% for cell_voltage in sensor.sp18b2311300049_delta_voltage -%}

  • sensor:
    • name: cell_voltage_0
      state: >-
      {{ state_attr('sensor.sp18b2311300049_delta_voltage', 'cell_voltages')[0] }}
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >-
      {{ has_value('sensor.sp18b2311300049_delta_voltage') }}

{% endfor %}`

But how do I now replace the "0"'s in the loop body by some kind of index counter ?

@patman15
Copy link
Owner

If you install Mushroom you could use this:
Image
Does that help? I do not think you can generate individual sensors automatically.

@Meli73
Copy link
Author

Meli73 commented Feb 18, 2025

Hi .. thanks. I'll have a look at this later this evening.

But anyway --
After a few nudges in the right direction, I at least got that far.

template: >
  {%- set ns = namespace(count = 0) -%}
  {%- for attr in states.sensor.sp18b2311300049_delta_voltage.attributes.cell_voltages %}
  - sensor:
    - name: cell_voltage_{{ ns.count }}
      state: >-
        {{ state_attr('sensor.sp18b2311300049_delta_voltage', 'cell_voltages')[ns.count] }}
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >- 
        {{ has_value('sensor.sp18b2311300049_delta_voltage') }}
  {% set ns.count = ns.count + 1 %}       
  {% endfor %}

and the output of it in the Template editor looks like this:

template: >

  • sensor:

    • name: cell_voltage_0
      state: >-
      3.291
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >-
      True
  • sensor:

    • name: cell_voltage_1
      state: >-
      3.292
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >-
      True
      .......
      "check Configuration" was green with this ...

But after restart HA is Got this Error MSG:
Logger: homeassistant.config
Source: config.py:357
First occurred: 10:56:47 PM (1 occurrences)
Last logged: 10:56:47 PM

Invalid config for 'template' at configuration.yaml, line 28: expected a dictionary '', got "\n{%- set ns = namespace(count = 0) -%} {%- for attr in states.sensor.sp18b2311300049_delta_voltage.attributes.cell_voltages %} - sensor:\n - name: cell_voltage_{{ ns.count }}\n state: >-\n {{ state_attr('sensor.sp18b2311300049_delta_voltage', 'cell_voltages')[ns.count] }}\n unit_of_measurement: 'V'\n state_class: measurement\n device_class: voltage\n availability: >- \n {{ has_value('sensor.sp18b2311300049_delta_voltage') }}\n{% set ns.count = ns.count + 1 %} ...

Does this mean what you meant with it is not possible to autogenerate Sensors in the for loop ?

@patman15
Copy link
Owner

Does this mean what you meant with it is not possible to autogenerate Sensors in the for loop ?

Correct, the loop does not work to generate configuration entries in the YAML file. The loop (template) only works within state: or availability:, thus you need to have it somewhere you can put templates. One option is the Mushroom template card as shown above. Depends on what you want to achieve ...

@Meli73
Copy link
Author

Meli73 commented Feb 19, 2025

Image
The Mushroom Template I got Running also.

But I want also have access to this Values , for History plots and other Automations.

@Meli73
Copy link
Author

Meli73 commented Feb 20, 2025

template:  
{%- set ns = namespace(count = 0) -%}
{%- for attr in states.sensor.sp18b2311300049_delta_voltage.attributes.cell_voltages %}
  - sensor:
    - name: cell_voltage_{{ ns.count }}
      state: >-
        {% raw %}{{ state_attr('sensor.sp18b2311300049_delta_voltage', 'cell_voltages')[{% endraw %}{{ns.count}}{% raw %}] }}{% endraw %}
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >- 
        {% raw %}{{ has_value('sensor.sp18b2311300049_delta_voltage') }}{% endraw %}
{% set ns.count = ns.count + 1 %}       
{% endfor %}

modding the code with some "raw" lines I could at least use this script in the template Editor to generate the inlined Template code automatically ;-)

template:
  - sensor:
    - name: cell_voltage_0
      state: >-
        {{ state_attr('sensor.sp18b2311300049_delta_voltage', 'cell_voltages')[0] }}
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >- 
        {{ has_value('sensor.sp18b2311300049_delta_voltage') }}
       

  - sensor:
    - name: cell_voltage_1
      state: >-
        {{ state_attr('sensor.sp18b2311300049_delta_voltage', 'cell_voltages')[1] }}
      unit_of_measurement: 'V'
      state_class: measurement
      device_class: voltage
      availability: >- 
        {{ has_value('sensor.sp18b2311300049_delta_voltage') }}

But is it possible to "group" this sensors now into a Device ?

@patman15
Copy link
Owner

Clever idea. 😃
Just have a look at the HA community, for example this post. Also, did you check out https://github.com/gjohansson-ST/attribute_as_sensor ?
Best regards,
Patrick

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants