Been beating my head against the wall over this and now I am going to have to repaint the wall.
I am trying to get a variable value to be saved so it can survive a restart of HA.
Using variable.py, it is working, I can create a variable, I can config the variable attributes based on the docs/examples with variable.py. I am unclear if a variable can be saved so it is restored to the value that HA had prior to a restart. To test I created two variables, one with restore set to true and one set to false. I created an automation that increases the value by one every time it is triggered. I triggered it four times so each variable contains 4. Then I restarted HA and both variables are back to being unknown. Just to be sure I retriggered the automation post restart and the values are back to starting from 0. I would like for the value at this stage to be 5 (the four I triggered before restart and 1 post restart).
I am running HA 86.4
Is it not possible to save the value of a variable beyond a restart of HA?
Is there a different way to save a variable?
I have considered using a input_boolean sensor and just turning it on/off every time I want to increase a count and then using record and history to get the count but that seems pretty lame.
variables.yaml -
gene_next_oil_change:
# value: 0
attributes:
friendly_name: Next Oil Change_RT
icon: mdi:alarm
restore: true
gene_hrs_on_oil:
# value: 0
attributes:
friendly_name: Hours on oil_RF
icon: mdi:alarm
restore: false
I can access the variables as the below automation increases their values by one each time it is triggered.
- id: ‘1550969256948’
alias: Adding Time to Gene
trigger:- hours: ‘’
minutes: /2
platform: time_pattern
seconds: ‘’
condition: - condition: state
entity_id: light.cabin_night_light
state: ‘on’
action: - data:
value_template: ‘{{ (variable.state | int) + 1 }}’
variable: gene_hrs_on_oil
service: variable.set_variable - data:
value_template: ‘{{ (variable.state | int) + 1 }}’
variable: gene_next_oil_change
service: variable.set_variable
- hours: ‘’
Using STATES I see the variables have increased to 4 (triggered 4x). I restart HA and they are both back to UNKNOWN and I would expect one of the two to be at 4.