Elapsed timer automation

I’m trying to implement an elapsed timer of sorts. I searched the forums and the internerd and tried using what I found.
It it works manually, but my automation says it works, but doesn’t. Basically, I want to keep track of how long my inverter is receiving an input from an AC feed.

# sensors
sensor:
  # AC Input Amps
  - platform: mqtt
    name: magnum_inverter_AACin
    state_topic: "powerpi/inverter"
    value_template: "{{ value_json.data.AACin }}"
    json_attributes_topic: "powerpi/inverter"
    json_attributes_template: "{{ value_json.data.AACin | tojson }}" 
    unit_of_measurement: A

  - platform: template
    sensors:
      # Elapsed Time [HACK]
      magnum_inverter_ac_input_on_time:
        value_template: >-
          {{ (as_timestamp(now()) - state_attr('input_datetime.magnum_inverter_ac_input', 'timestamp'))| timestamp_custom('%H:%M:%S', false) }}

# input boolean
input_boolean:
  # Aux AC Input
  magnum_inverter_ac_input:
    name: Aux AC Input
    initial: false

# input datetime
input_datetime:
  # AC Input Elapsed Timer
  magnum_inverter_ac_input:
    has_date: true
    has_time: true

# automation
## AC Input ##################################################
  ## ON
  - alias: Start AC Input Boolean
    trigger:
      - platform: numeric_state
        entity_id: sensor.magnum_inverter_aacin
        above: 0
    action:
      service: input_boolean.turn_on
      data:
        entity_id: input_boolean.magnum_inverter_ac_input
  ## OFF
  - alias: Deavtivate AC Input Boolean
    trigger:
      - platform: numeric_state
        entity_id: sensor.magnum_inverter_aacin
        below: 0.1
    action:
      service: input_boolean.turn_off
      data:
        entity_id: input_boolean.magnum_inverter_ac_input
  ## Start Elapsed Timer
  - alias: Start AC Input Elapsed Timer
    trigger:
      platform: state
      entity_id: input_boolean.magnum_inverter_ac_input
      to: 'on'
    action:
      service: input_datetime.set_datetime
      data:
        entity_id: input_datetime.magnum_inverter_ac_input
        datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
  ## Update Elapsed Timer
  - alias: Update Magnum Elapsed Timer
    trigger:
      platform: time_pattern
      seconds: '*'
    condition:
      condition: state
      entity_id: input_boolean.magnum_inverter_ac_input
      state: 'on'
    action:
      service: homeassistant.update_entity
      entity_id:  sensor.magnum_inverter_ac_input_on_time

it all acts like it works, but even when I run it manually the input_datetime doesn’t seem to actually get updated so my elapsed timer increments from whenever I manually set the state

automation.deavtivate_ac_input_boolean on last_triggered: 2020-04-12T23:06:54.039309+00:00 friendly_name: Deavtivate AC Input Boolean
automation.start_ac_input_boolean on last_triggered: 2020-04-13T09:08:23.003727+00:00 friendly_name: Start AC Input Boolean
automation.start_ac_input_elapsed_timer on last_triggered: 2020-04-13T12:59:31.964568+00:00 friendly_name: Start AC Input Elapsed Timer
input_boolean.magnum_inverter_ac_input on editable: false friendly_name: Aux AC Input
input_datetime.magnum_inverter_ac_input 2020-04-10 04:18:20 editable: false has_date: true has_time: true year: 2020 month: 4 day: 10 hour: 4 minute: 18 second: 20 timestamp: 1586506700
sensor.magnum_inverter_ac_input_on_time 05:18:27 friendly_name: magnum_inverter_ac_input_on_time icon: mdi:power-socket-us

I don’t understand that.
And for your magnum_inverter_ac_input_on_time to work properly you need to add an entity so HA evaluates the template regularly, here’s more.

my sensor.magnum_inverter_ac_input_on_time sensor is working correctly, incrementing every second via the automation.update_magnum_elapsed_timer. What is not, is when the AC input is activated, last-triggered attribute for the automation.start_ac_input_elaspsed_timer says that it is triggered at that time, but the input_datetime.magnum_inverter_ac_input timestamp (and datetime) attributes do not seem to reflect that time. It only seems to “stick” when I set the timestamp state manually from the dev tools panel for input_datetime.magnum_inverter_ac_input.

in your automation.start_ac_input_elaspsed_timer replace data with data_template as you use templates, here’s more.

This solved it. thank you. It’s always th small things that get me

1 Like