Intial sensor value recording incorrectly after startup (help with code)

Hi Guys

So this is the code I’m working with.

  - platform: mqtt
    entity_namespace: Ultrasonic
    state_topic: "Ultrasonic/distance"
    name: "Current Ultrasonic Distance"
    unit_of_measurement: "cm"

  - platform: statistics
    entity_id: sensor.Ultrasonic_Current_Ultrasonic_distance
    name: Ultrasonic Distance
    force_update: true
    sampling_size: 100
  - platform: template
    sensors:
      ultrasonic_distance:
        value_template: '{% if (states.sensor.ultrasonic_distance_mean.state | float == 0) %}0.0{% else %}{{ ((35.0 - states.sensor.ultrasonic_distance_mean.state | float) | float / 15 * 100) | round(1) }}{% endif %}'
        unit_of_measurement: '%'
        friendly_name: 'Water Level'

So Initially I had the float value set to “float >= 35” that gave me an output on my history for the Water Level of 233.33, when I’ve changed it to “float == 0”, now it at least starts at 0 but ideally I’d like it to be delayed by say 30 seconds. An automation doesn’t seem to be the answer and because my coding knowledge is slim I can’t work out what I’d need to write.

You only need two float filters in that template, not three.

value_template: '{% if (states.sensor.ultrasonic_distance_mean.state | float == 0) %}0.0{% else %}{{ ((35.0 - states.sensor.ultrasonic_distance_mean.state | float) / 15 * 100) | round(1) }}{% endif %}'

You can’t delay sensors with HA templates. You might be able to do it with a python script or AppDaemon, but I have no idea how.

Thanks for the tip :slight_smile:

Mm not sure how to do that unfortunately.

Has anyone got some insight on a solution for me?

30 seconds after startup perform a homeassistant.update_entity. But in reality this is pointless because the sensor will update with a specific period. Once your system is fine tuned, you’ll never be restarting.

Doesn’t that just force the update of that specific entity rather than stop all updates of it. I.e I’ve got it to send out data every 10 seconds. Even if I had to do it every say 60 seconds the mean would still be 0 and then my water level would be calculated on that 0. Only the Current Ultrasonic Distance will be marked as (Unknown).

what you want is not possible without writing a custom component.

For anyone wondering I did manage to come up with a solution that gives me a nice graph.

The first part of the if statement would be used for any HC-SR04 sensor as it has a 20cm blind, the key was changing the 0.0 to unknown. :slight_smile:

  - platform: template
    sensors:
      ultrasonic_distance3:
        value_template: '{% if (states.sensor.ultrasonic_distance_mean.state | float <= 20) %}unknown{% else %}{{ ((35.0 - states.sensor.ultrasonic_distance_mean.state | float) / 15 * 100) | round(1) }}{% endif %}'
        unit_of_measurement: '%'
        friendly_name: 'Water Level 3'

If that was your only problem, what was all the nonsense about delaying the values?

I read on another thread that doing that would be a possible solution. It would’ve been another option if I could delay the read by 30 seconds, now it’s no longer a thought though with the above solution.

1 Like