snarky-snark / home-assistant-variables

A custom Home Assistant component for declaring and setting generic variable entities dynamically.
Apache License 2.0
278 stars 16 forks source link

Where to put the automation? #94

Closed leo4417 closed 11 months ago

leo4417 commented 1 year ago

I would like to write the value of sensor (total_current) to variable x at sunset and the value at sunrise to variable y.

The difference gives me the power consumption over night.

Your script should be a good idea, I installed your integration - but where do I put the code below? If I write an automation, it was the wrong configuration. Could you help me?

var:
  temp_sensor_battery:
    friendly_name: "Temp Sensor Battery"
    value_template: "{{ state.attr('sensor.temperature', 'battery') }}"

automation:
  - alias: "Update Temp Sensor Battery Var Every 5 Minutes"
    trigger:
      - platform: time_pattern
        minutes: '/5'
    action:
      - service: var.update
        data:
          entity_id: var.temp_sensor_battery
pdwonline commented 1 year ago

The var object is not an automation. However, it get's updated when the tracked_entity_id is triggered. In other words: if you add sensor.temperature to the tracked_entity_id, you don't need the automation and it will be updated when sensor.temperature state changes. This would do the trick:

var:
  temp_sensor_battery:
    friendly_name: "Temp Sensor Battery"
    value_template: "{{ state.attr('sensor.temperature', 'battery') }}"
    tracked_entity_id:
     - sensor.temperature