State Voltage code not sure how to write it

Hi all, I need your help…

I found a code here on another post that I want to use for my HA, and I want to make it work for my entity. But my entity it is not something with a ON/OFF State, my entity’s state is a value that go from 0 to 30 Ex: 12.12 or 14.49 ( the value is a voltage that I monitor) so I want to be able to know how many hours, and minutes my entity was over 10 volts.

How to make it work ? How to turn the state: to start the counter when my value is over 10 ? And stop under 9.99 ?

  sensors:
- platform: history_stats
  name: Time counter
  entity_id: sensor.moniteur_de_voltage
  state: "on"
  type: time
  start: '{{ now().replace(day=1, hour=0, minute=0, second=0) }}'
  end: "{{ now() }}"

Thank you all :slight_smile:

The entity names looks invalid as there are two periods in it.

I’d start by creating a binary sensor template that is on if the voltage is above the threshold.

The template for the binary sensor would be something like this

{{ states("sensor.voltage")|float(0) > 9.99 }}

You can test this in developer tools / templates.

From there it’ll be easier to create the history stats sensor.

1 Like

My entity is already working just fine and yeah i correct my paste it was an underscore I test that I’ll let you know :slight_smile:

@PeteRage

what is the correct way to make a Binary sensor ? I tried 2-3 way and always error in my code… and because of this i Got Error in my other template what im missing here ?

I am YAML-challenged, but I don’t think you can nest double quotes.

state: "{{ states("sensor.voltage")|float(0) > 9.99 }}"

should be

state: "{{ states('sensor.voltage')|float(0) > 9.99 }}"

1 Like

@stevemann

okay i Got it
image

I will test it tomorrow, I have no acces to my voltage sensor right now, i’ll let you know thanks guys

Do let me know if I got one right.

@stevemann @PeteRage

Ok now it’s working, but how I can see real minutes and not decimal of an hour ?

and how to reset the counter to Zero to every new input after the Voltage drop to zero ?

- platform: template
  sensors:
    high_low:
      friendly_name: "High Low ON"
      value_template: "{{ states('sensor.timer_voltage_drop')|float(0) > 9.99 }}"

- platform: history_stats
  name: Compteur de temps
  entity_id: sensor.high_low
  state: "True"
  type: time
  start: '{{ now().replace(day=1, hour=0, minute=0, second=0) }}'
  end: "{{ now() }}"

You can create a template sensor to multiply that stats sensor by 60. The template would be something like this:

{{ states("sensor.compteur_de_temps")|float(0) * 60.0 }}
1 Like

You just want to measure the duration of the last on time?

@PeteRage Yes I just want to be able to see the last recording time, everytime the sensor get over 10Volts a new countdown start

@PeteRage thank you its working for the converting time, now the only thing to end this is to find a way to reset the value to 0

@PeteRage do you have any idea how to make it work ?

Apologies, been off battling some issues for a bit.

We will need to change approaches. Here’s a package I use on the basement sump pump. It does the opposite of what you want - it calculates the off duration (e.g. time between cycles) - however you can use this technique for your use case and also calculate the on duration.

input_number:
  cd_sump_on_threshold:
    min: 0.0
    max: 30.0
    step: 0.1
    mode: box
    unit_of_measurement: A

binary_sensor:
  - platform: template
    sensors:
      cd_sump_running:
        device_class: running
        value_template: >-
          {{ states("sensor.sump") | float(0) > states("input_number.cd_sump_on_threshold") | float(0) }}
template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.cd_sump_running
        from: "off"
        to: "on"
    sensor:
      - name: "cd_sump_last_start_time"
        state: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
  - trigger:
      - platform: state
        entity_id: sensor.cd_sump_last_start_time
    sensor:
      - name: "cd_sump_off_duration"
        state: "{{ as_timestamp(now())|int(0) - as_timestamp(trigger.from_state.state)|int(0) }}"
        unit_of_measurement: sec
  - trigger:
      - platform: state
        entity_id: binary_sensor.cd_sump_running
        from: "off"
        to: "on"
    sensor:
      - name: "cd_sump_cycle_count"
        state: "{{ states('sensor.cd_sump_cycle_count')|int(0) + 1 }}"
        unit_of_measurement: cycles


recorder:
  include:
    entities:
      - input_number.cd_sump_on_threshold
      - binary_sensor.cd_sump_running
      - sensor.cd_sump_last_start_time
      - sensor.cd_sump_off_duration
      - sensor.cd_sump_off_duration_average
      - sensor.cd_sump_cycle_count