Days since last bill template sensor

I’m having problems setting up a template sensor to give me value for the days since I last had a power bill. This value will fit in with some other calculations to give me a constantly updating Export Power and Import Power daily average.

I did some research here on the forum and found all I needed to construct the template and have come up with this:

{{ (as_timestamp(now()) | int - as_timestamp(states('input_datetime.only_date13')) | int ) // (60 * 60 * 24) }}

The above gives me a correct value in the /developer-tools/template editor (12) but when I add it to my config like so:

- platform: template
  sensors:
    days_since_last_bill:
    value_template: "{{ (as_timestamp(now()) | int - as_timestamp(states('input_datetime.only_date13')) | int ) // (60 * 60 * 24) }}"
 

it causes the config to return errors.

Errors are:

2020-11-29 01:25:57 ERROR (SyncWorker_0) [homeassistant.util.yaml.loader] while parsing a block mapping

in “/config/configuration.yaml”, line 5, column 1

expected , but found ‘-’

in “/config/configuration.yaml”, line 1224, column 1

2020-11-29 01:25:57 ERROR (MainThread) [homeassistant.bootstrap] Failed to parse configuration.yaml: while parsing a block mapping

in “/config/configuration.yaml”, line 5, column 1

expected , but found ‘-’

in “/config/configuration.yaml”, line 1224, column 1. Activating safe mode

Anyone able to help put me on the right track?

If that is your indentation level, you need to indent the value_template to be a child of days_since_last_bill.

At the current level, it’s a new sensor named “value_template”.

- platform: template
  sensors:
    days_since_last_bill:
      value_template: "{{ (as_timestamp(now()) | int - as_timestamp(states('input_datetime.only_date13')) | int ) // (60 * 60 * 24) }}"
1 Like

@jocnnor…First…thanks very much for the prompt reply!

I did mess the indentation up in my config…cheers for noticing that! I felt sure that would be the solution…
However after correcting the same error keeps coming up.

Double checking everything and to confirm in the editor the below works a treat:

{{ (as_timestamp(now()) | int - as_timestamp(states('input_datetime.only_date13')) | int ) // (60 * 60 * 24) }}

While the corrected template as per your suggestion:

- platform: template
  sensors:
    days_since_last_bill:
      value_template: "{{ (as_timestamp(now()) | int - as_timestamp(states('input_datetime.only_date13')) | int ) // (60 * 60 * 24) }}"

Still brings up the same error…weird!

Tried reversing the brackets in the template editor like:

{{ (as_timestamp(now()) | int - as_timestamp(states("input_datetime.only_date13")) | int ) // (60 * 60 * 24) }}

That doesn’t give the same result (ie: 18595 instead of 12) but it does give a result. BUT it still fails when I edit the config to become:

- platform: template
  sensors:
    days_since_last_bill:
      value_template: '{{ (as_timestamp(now()) | int - as_timestamp(states("input_datetime.only_date13")) | int ) // (60 * 60 * 24) }}'

I’m at a total loss to explain this result any further atm.
Cheers again!

I do have to ask, where exactly is this in your config?

It needs to be in the sensor section.

sensor:
  - platform: template
    sensors:
      days_since_last_bill:
        value_template: '{{ (as_timestamp(now()) | int - as_timestamp(states("input_datetime.only_date13")) | int ) // (60 * 60 * 24) }}'

And, do make sure you only have one template section in there.

1 Like

Well I just this very minute fixed the final problem…been a shocker of a day!
Yes its in the correct area along with all the rest of my template sensors…the problem was again my very poor indentation and my lack of checking the obvious even after you pointed out my first mistake.
The second was that I failed to have two spaces before the hyphen in front of ‘platform’.

  - platform: template

Haven’t been doing a lot on my config for probably 6 or more months and made some terrible rookie mistakes in my troubleshooting.

Thanks again for bearing with me and anyone else out there - double check your indentation!

1 Like

I highly recommend using VSCode for writing the yaml. Either use the built-in addon, or externally.

There is a VSCode plugin that will connect to your server and gather all info for you which allows auto completion, etc.

Even if you don’t connect it to your server, it will highlight indentation mistakes and stuff for you.

Don’t worry about rooky mistakes. We were all rookies at one time. And even if you didn’t make mistakes, 6 months is a long time, and things that used to work can suddenly not work due to the ever changing nature of this platform!

1 Like

Haha…Even with the luxury of being a VSCode user I can still stuff it up. LOL

If I ever see this error again I reckon I will know whats happened:

line 5, column 1 expected , but found ‘-’

Thanks again for your valuable help and keep up the great work!