Custom Component: Generic Variable Entities

I haven’t personally tested alternative DB backends, but I think they should work. I could be wrong…

Let’s see if we can troubleshoot this.

You can test the tracked_entity_id trigger by removing the query and column and replacing them with something like value_template: "{{ 1 }}".

You don’t need the single quotes around the tracked_entity_id. For example,

tracked_entity_id: sensor.time

That works.

front_patio_max_temp:
  friendly_name: "Front Patio Max Temp"
  icon: mdi:temperature-fahrenheit
  value_template: "{{ 1 }}"
  tracked_entity_id: 'sensor.time'

Screen Shot 2021-03-02 at 5.38.40 PM

EDIT: That example was unclear if it was actually updating every minute. Ran this instead. sensor.time appears to be updating every minute.

front_patio_max_temp:
  friendly_name: "Front Patio Max Temp"
  icon: mdi:temperature-fahrenheit
  value_template: "{{ (states('var.front_patio_max_temp') | int) + 1 }}"
  tracked_entity_id: sensor.time

Hmm that is a tricky one. The event tracking appears to be working which would point to a problem around the DB query - either with the query itself or the var component.

You could try to enable debug logging for the var component. When the query runs you should see one of three messages:

  1. A warning message that no results were returned
  2. A debug message with the results returned
  3. An error message that the query failed
service: logger.set_level
data:
    custom_components.var: debug

Set the logger level to debug and let it sit for a few minutes and saw nothing. Called the service var.update to get it to run and magically…

2021-03-02 20:30:03 DEBUG (MainThread) [custom_components.var] result = [('max_value', '68.9')]

So for some reason the trigger is not happening when performing a query. Any ideas?

EDIT:
Tried changing the trigger to a Z-Wave JS light and it didn’t trigger. So it appears the trigger is not working with a query.

Well that is strange. It seems that this might be a bug, but I’m having trouble identifying the cause.

The trigger is most likely still running, but the state changed event for sensor.time is either not being recorded in the database or not being found in the database.

The way the event triggers work with a query is the var component listens for the specified events (in this case the state change event for sensor.time). When a tracked event occurs, the var component polls the database until it sees that the event has been written to the database (it has to wait in case the query involves that event). Once it sees the event in the database, then it updates itself, running and evaluating the query.

So if the query is not running, that means that the event is either not being written or for whatever reason it is not being found.

Can you check the logbook and see if the updates to sensor.time are present there?

Another thing you could try is to generate your own “minute elapsed” event and use that to trigger the variable update.

script:
  fire_minute_elapsed_event:
    alias: Fire Minute Elapsed Event
    sequence:
      - event: minute_elapsed_event
        event_data_template:
          time: "{{ now() }}"
automation:
  trigger:
    platform: time_pattern
    minutes: 1
  action:
    service: script.fire_minute_elapsed_event

Thanks for your help. Checked Logbook and sensor.time was not updated every minute. Sometimes it wouldn’t be for several minutes, others several hours. The interesting thing is History was showing that it updated every minute. So there might be a bug with sensor.time or it wont work for this situation.

Changed to using an automation to run var.update every minute and it appears to be working. Below is what I ended up.

alias: var - run every minute
description: >-
  Used by the home-assistant-variables component to update certain sensors every
  minute
trigger:
  - platform: time_pattern
    minutes: /1
condition: []
action:
  - service: var.update
    data: {}
    entity_id: var.front_patio_max_temp
mode: single
front_patio_max_temp:
  friendly_name: "Front Patio Max Temp"
  icon: mdi:temperature-fahrenheit
  query: "SELECT MAX(state) AS value FROM states WHERE created > CURRENT_DATE + INTERVAL 8 HOUR AND entity_id = 'sensor.front_patio_thgr810_temperature' AND state REGEXP '^[0-9].+$';"
  column: 'value'

That’s a puzzling issue with sensor.time.

Glad you found a solution!

Hi,

I have question regarding how to calculate a custom variable value and sensor value.
Is this possible?
I have put following code in my configuration.yam;

var:
  a:
    friendly_name: Stroommeter
    initial_value: 16676,8
    icon: mdi:flash-outline

This value is my last known value from my official energy flow meter.
The code above is working fine, i have now a new entity with this value! :slight_smile:

I also use the IAMMETER pluggin.

My question, I would like to calculate (SUM) the total energy used coming from the AIMMETER with the variable entity above. After this I have almost a up to date energy flow meter value!

I have tried to build a template with following code;

energy_total_teller:
      friendly_name: 'Stroommeter'
      entity_id:
        - var.b
        - sensor.energy_total_used
      value_template: "{{ (states('sensor.energy_total_export')|float + states('var.b'))|round(1) }}"
      unit_of_measurement: "kWh"

But I guess this can not work because my custom variable is not a sensor, correct?
Because the result of this code is the value coming from the sensor.energy_total_used (IAMMETER).

Any ideas?

regards,

 Jurgen

I’m having trouble following the snippets of yaml you posted, but one suggestion would be to use tracked_entity_id to update a variable whenever your sensor updates.

Something like this,

var:
  energy_total_teller:
    tracked_entity_id:
      - sensor.energy_total_used
    value_template: "{{ (states('sensor.energy_total_export')|float + states('var.b'))|round(1) }}"

Hi snarkysnark,

Thx for your input! But it is still not working.
Maybe i start over with a clean explanation! :slight_smile:

Picture below is my energy meter:

Here you can see the value 16676,8 I wan’t to use for calculation.
Now I need to create a variable to get this value in has.
After that I would like to update (sum) this value (16678,8) with my existing sensor.energy_total_used to get a kind of up to date virtual energy meter like my picture above!

Maybe someone can help me with this and tell me how to fix this? :slight_smile:

Regards,

 Jurgen

have trouble… worked on core-2021.6.2 ?