midea-ac-py icon indicating copy to clipboard operation
midea-ac-py copied to clipboard

Force sensor update to allow derivative helper to track current power draw.

Open tabascoz opened this issue 1 year ago • 4 comments

Hi there

I have some devices that does report Total Energy utilization but not Current Energy. So to be able to track real-time power, i created some derivative sensors and was able to get current power draw.

But the problem is that the derivative helper only updates it's value when source sensor updates itself. It looks like when i power off the AC the sensor does not update anymore and my derivative sensor is stuck with the last reading in watts.

Would it be possible to force sensor update within a given timeframe, let's say 1 min?

Cheers, Tabascoz

tabascoz avatar Dec 06 '24 20:12 tabascoz

Thanks for the suggestion. I'll have to think about this one. I know an entity can be configured to force updates to the state even if the value doesn't change, but I believe this has a side effect of causing it to take much more room in the database as each (identical) state is written.

And I'm only considering that as a potential problem as the state would be updated approx. every 15 seconds.

mill1000 avatar Dec 11 '24 23:12 mill1000

I haven't figured out if it's possible, but a solution might be to force the sensor updates only when the device powers on/off.

mill1000 avatar Dec 12 '24 22:12 mill1000

That would be a very good solution. My discharge sensor and the energy socket match quite well. But i can no longer select the time window in the sensor; this would lead to a smothered line, but then the problem described occurs.

grafik

LotharWoman avatar Dec 22 '24 19:12 LotharWoman

I have a couple workaround solutions that might work for this. Had a similar problem with a couple smart low voltage landscape lighting transformers that would only report wattage when the value changed, so I created a trigger sensor that pulled the source sensor's value every second, and used that trigger sensor for my integral to calculate kWh. I was trying to get from W to kWh, so this is the opposite of what you're trying to do, but the logic should be analogous.

template:
  - trigger:
      - platform: time_pattern
        hours: "*"
        minutes: "*"
        seconds: 1
      - platform: state
        entity_id: light.backyard_landscape_lighting_transformer_1
        attribute: watts
      - platform: state
        entity_id: light.front_yard_landscape_lighting_transformer_1
        attribute: watts
    sensor:
      - name: "Backyard Landscape Lighting Transformer Power"
        unique_id: 3612c3b2-647f-48a3-814d-0f10bd741939
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        state: >
          {% if state_attr('light.backyard_landscape_lighting_transformer_1', 'watts') | is_number %}
            {{ state_attr('light.backyard_landscape_lighting_transformer_1', 'watts') | float }}
          {% else %}
            {{ states('sensor.backyard_landscape_lighting_transformer_power') }}
          {% endif %}
        attributes:
          triggered_at: "{{ now() }}"
      - name: "Front Yard Landscape Lighting Transformer Power"
        unique_id: 471b96fc-bbf2-4868-9173-1cf3079c1052
        unit_of_measurement: "W"
        device_class: power
        state_class: measurement
        state: >
          {% if state_attr('light.front_yard_landscape_lighting_transformer_1', 'watts') | is_number %}
            {{ state_attr('light.front_yard_landscape_lighting_transformer_1', 'watts') | float }}
          {% else %}
            {{ states('sensor.front_yard_landscape_lighting_transformer_power') }}
          {% endif %}
        attributes:
          triggered_at: "{{ now() }}"

This next fix was for a related issue - after an update to the same integration mentioned above, the way HA was polling and receiving data from these two devices changed, and the only solution I could come up with was an automation to force the integration to reload (and thus update the underlying sensor values) when the lights turned from "on" to "off" or from "off" to "on" so it would correctly report the wattage each device used. The "entry_id" value on the second line from the bottom was the value for the integration that I pulled from config/.storage/core.config_entries. No way to look that up in the UI.

- id: '1737212217962'
  alias: Update Landscape Lighting Power Consumption when State Changes
  description: ''
  triggers:
  - trigger: state
    entity_id:
    - light.backyard_landscape_lighting_zone_1_2
    - light.front_yard_landscape_lighting_2
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - trigger: state
    entity_id:
    - light.backyard_landscape_lighting_zone_1_2
    - light.front_yard_landscape_lighting_2
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 1
      seconds: 0
  conditions: []
  actions:
  - action: homeassistant.reload_config_entry
    metadata: {}
    data:
      entry_id: 01J4FC52MSXPKCHASBACVB6KNN
  mode: single

bluecrystaldex avatar Jun 08 '25 15:06 bluecrystaldex