Why doesn't my template code work?

I’m using this to get the value for entity “propane_tank_neevo_371”

command_line:
  - sensor:
      command: "curl -u <user>:<password> https://telematics.otodatanetwork.com:4432/v1.5/DataService.svc/GetAllDisplayPropaneDevices --header Content-Type:application/json"
      name: "Propane Tank Neevo 371"
      unique_id: propane_tank_371
      value_template: "{{ value_json[0].Level }}"
      scan_interval: 14400
      state_class: measurement

And this to convert it from percent of tank full to gallons:

  - sensor:
      - name: "Propane 371 Gallons"
        unit_of_measurement: "Gallons"
        state_class: measurement
        state: "{{ (states('sensor.propane_tank_neevo_371') |float(0)) * 2.40}}"

This works fine except that several times each day the curl command results in the entity’s value being 0 (I don’t know why).

So, I wanted to create some code that ignored any 0 value, and tried this:

template:
  - sensor:
      - name: "Propane 371 Gallons Test"
        unit_of_measurement: "Gallons"
        state_class: measurement
        state: >
          {% set s = states("sensor.propane_tank_neevo_371")|float(0) * 2.4 %}
          {% if  s = 0 %}
            100    
          {% else %}
            s

I just used “100” as a placeholder but even this code doesn’t work.

Can someone suggest a way to ignore any 0 percentage results from the curl command (better to ignore it at that stage than at the gallons entity creation stage?

Thank you.

Maybe something like?:

{{ value_json[0].Level if value_json[0].Level | float > 0 else states('sensor.propane_tank_371') }}

Wow! That looks great.

Do I understand correctly that this tests for value_json[0].level being greater than 0 (in which case, the value of sensor.propane_tank_neevo_371 gets the value of json[0], otherwise (else) it gets the value of sensor.propane_tank_371?

If so, where does sensor.propane_tank_371 come from?

I have the last recorded value but in gallons (not in percentage, which sensor.propane_tank_371 uses) in sensor.propane_371_gallons

Thank you.

You can do all this in the command line sensor. No need for other templates.

command_line:
  - sensor:
      command: "curl -u <user>:<password> https://telematics.otodatanetwork.com:4432/v1.5/DataService.svc/GetAllDisplayPropaneDevices --header Content-Type:application/json"
      name: "Propane Tank Neevo 371"
      unique_id: propane_tank_371
      value_template: "{{ value_json[0].Level|float(0) * 2.4 if value_json[0].Level is defined else this.state }}"
      scan_interval: 14400
      state_class: measurement
      unit_of_measurement: "Gallons"

It’s a recursive value lookup based on the last state of the sensor (states('sensor.propane_tank_371')).

tom_l’s suggestion is more complete and uses the this variable, which is safer for recursive lookups.

That is truly fabulous!

I tried to recreate this with a multiscrape-gotten sensor, but I don’t think it’s working.

Using this for multiscape:

multiscrape:
  # /homeassistant/multiscrape.yaml
  - resource: "https://www.myamerigas.com/eSign/[email protected]"
    name: Amerigas Scraper
    scan_interval: 43200
    timeout: 60
    headers:
      User-Agent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
      referer: "https://www.myamerigas.com/"
      origin: "https://www.myamerigas.com"
      host: "www.myamerigas.com"
    form_submit:
      submit_once: True
      resubmit_on_error: False
      resource: "https://www.myamerigas.com/Login/Login"
      select: "form[name='Login']"
      input:
        EmailAddress: "<USERNAME>"
        Password: "<PASSWORD>"
    sensor:
      - select: ".EstimatedTankDiv .lblvalue-Estimatedtank"
        name: Amerigas Tank Percentage Raw
        unique_id: multiscrape_amerigas_tank_percentage_raw_3420398

And this to create a gallons sensor:

template:
  -sensor:
      - name: "630 Amerigas Tank Percentage Gallons"
        state: >
          {% set value = states('sensor.multiscrape_amerigas_tank_percentage_raw_3420398') * 1.2 if sensor.multiscrape_amerigas_tank_percentage_raw_3420398 is defined else this.state  %}
          {{ value | replace('%', '') | int }}

Would you mind?

Thanks!

Sorry I don;t know anything about the multiscrape integration.

What is the value of the sensor after you restart HA? I’m using the same strategy to default to this.state but my sensor is set to unknown after restarting.